<template>
  <div class="main-content">
    <!-- 中间组套列表 -->
    <div class="section-card-left">
      <div class="section-header">
        <div class="actions">
          <span class="descriptions-item-label">组套名称：</span>
          <el-input
            v-model="queryParams.searchKey"
            placeholder="请输入组套名称"
            clearable
            class="search-input"
            @keydown.enter="getOrderGroupList"
          >
            <template #append>
              <el-button icon="Search" @click="getOrderGroupList" />
            </template>
          </el-input>
          <span class="descriptions-item-label">使用范围：</span>
          <el-select
            v-model="queryParams.rangeCode"
            placeholder="请选择使用范围"
            prefix-icon="el-icon-search"
            clearable
            @change="getOrderGroupList"
            class="search-input"
          >
            <el-option
              v-for="item in use_range"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <el-table
        :data="orderGroupList"
        highlight-current-row
        @current-change="handleTemplateSelect"
        style="width: 100%"
        v-loading="templateLoading"
        border
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="name" label="组套名称" min-width="180" header-align="center" />
        <el-table-column prop="rangeCode_dictText" label="使用范围" width="100" align="center" />
        <el-table-column prop="createdAt" label="创建时间" width="160" align="center">
          <template #default="{ row }">
            <!-- {{ formatDate(row.createdAt) }} -->
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right" align="center">
          <template #default="{ row }">
            <el-button type="text" @click="editTemplate(row)">编辑</el-button>
            <el-button type="text" @click="deleteTemplate(row.id)" style="color: #f56c6c">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 右侧组套详情 -->
    <div draggable="true" class="section-card-right">
      <div class="template-detail">
        <!-- 基本信息卡片 -->
        <div class="info-card">
          <div class="card-header">
            <span>基本信息</span>
            <el-button type="primary" @click="addTemplate">新建组套</el-button>
            <el-button type="primary" @click="addTemplate">保存组套</el-button>
          </div>
          <el-form :model="currentTemplate" label-width="100px" class="info-form" :inline="true">
            <el-form-item label="组套名称">
              <el-input v-model="currentTemplate.name" ref="name" placeholder="请输入组套名称" />
            </el-form-item>
            <el-form-item label="组套类型">
              <el-select
                v-model="currentTemplate.typeEnum"
                placeholder="请选择组套类型"
                class="search-input"
              >
                <el-option
                  v-for="item in typeEnum"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="使用范围">
              <el-select
                v-model="currentTemplate.rangeCode"
                placeholder="请选择使用范围"
                class="search-input"
              >
                <el-option
                  v-for="item in use_range"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <div class="info-card">
          <div class="card-header">
            <span>项目列表</span>
          </div>
          <div style="padding: 10px 10px">
            <Prescriptionlist :comtination="comtination" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import Prescriptionlist from './components/prescriptionlist.vue';
import { getOrderGroup, deleteOrderGroup } from './components/api.js';

const { proxy } = getCurrentInstance();
const { use_range } = proxy.useDict('use_range');

const currentTemplate = ref({});
// 查询参数
const queryParams = ref({});
// 组套列表
const orderGroupList = ref([]);
const typeEnum = ref([
  {
    value: '1',
    label: '医嘱组套',
  },
  {
    value: '2',
    label: '诊疗组套',
  },
]);
const comtination = ref('');
const templateLoading = ref(false);

getOrderGroupList();
function getOrderGroupList() {
  getOrderGroup(queryParams.value).then((res) => {
    orderGroupList.value = res.data.records;
  });
}

// 处理组套选择
const handleTemplateSelect = (row) => {
  currentTemplate.value = row;
  comtination.value = row;
};

// 添加组套
const addTemplate = () => {
  currentTemplate.value = {};
  proxy.$refs.name.focus();
};

// 编辑组套
const editTemplate = () => {};

// 删除组套
const deleteTemplate = (id) => {
  deleteOrderGroup({ id: id }).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess('删除成功');
    }
  });
};
</script>

<style scoped>
.main-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 30px;
  width: 100%;
  height: calc(100% - 80px);
}

.section-card-left {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.06);
  width: 35%;
  height: 100%;
  overflow: hidden;
  padding: 10px;
  border: 1px solid #ebeef5;
}

.section-card-right {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.06);
  width: 64%;
  height: 100%;
  overflow: hidden;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 10px;
  background: #f8fafc;
  border-bottom: 1px solid #ebeef5;
}

.search-area {
  padding: 10px 15px;
  border-bottom: 1px solid #ebeef5;
  background: #f8fafc;
}

.search-input {
  width: 200px;
}

.template-detail {
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-y: auto;
  height: 100%;
}

.info-card {
  background: #ffffff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #ebeef5;
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background: #f8fafc;
  border-bottom: 1px solid #ebeef5;
  font-weight: 600;
  color: #555;
}

.card-header i {
  margin-right: 10px;
  color: #409eff;
  font-size: 18px;
}

.card-header span {
  flex: 1;
}

.add-btn {
  padding: 5px 10px;
}

.info-form {
  padding: 20px 15px 10px;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding: 5px 0;
}

.custom-tree-node i {
  margin-right: 8px;
  color: #909399;
}

.node-label {
  flex: 1;
}

.count-badge {
  background-color: #e6f7ff;
  color: #1890ff;
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 14px;
  margin-left: 10px;
}

.tree-actions {
  visibility: hidden;
  margin-left: 10px;
}

.el-tree-node__content:hover .tree-actions {
  visibility: visible;
}

:deep(.el-tree-node.is-current > .el-tree-node__content) {
  background-color: #ecf5ff;
  font-weight: 500;
}

:deep(.el-tree-node__content) {
  height: 40px;
}

:deep(.el-table) {
  flex: 1;
}

:deep(.el-table__header) {
  font-weight: 600;
}

:deep(.el-table__row) {
  transition: background-color 0.3s;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa !important;
}

.el-button--primary {
  background: linear-gradient(135deg, #409eff, #337ecc);
  border: none;
}

.el-button--primary:hover {
  opacity: 0.9;
}

.cost {
  font-weight: 600;
  color: #e53935;
}

:deep(.el-tag) {
  border-radius: 12px;
  padding: 0 10px;
}

:deep(.el-input__inner) {
  border-radius: 8px;
}
</style>