<template>
  <div class="emr-template-container">
    <!-- <div class="search-box">
      <el-input placeholder="病历名称搜索..." v-model="queryParams.searchKey">
        <template #append>
          <el-button @click="queryList">查询</el-button>
        </template>
      </el-input>
    </div> -->
    <el-scrollbar class="emr-template-scrollbar-container" style="width: 100%">
      <div v-for="item in templateData" :key="item.id" class="scrollbar-item">
        <el-tooltip effect="dark" :content="`${item.name}`" placement="bottom">
          <el-text class="2" truncated @click="handleNodeClick(item)">
            <div class="template-item">
              {{ item.name }}
              <el-space>
                <el-icon><Edit @click="handleEdit(item)" /></el-icon>
                <el-icon><Delete @click="handleDelete(item)" /></el-icon>
              </el-space>
            </div>
          </el-text>
        </el-tooltip>
      </div>
    </el-scrollbar>
  </div>
</template>

<script setup>
import {defineEmits, ref, unref} from 'vue';
import {getListByDefinitionId} from '../api';
import {ElMessage, ElMessageBox} from 'element-plus';
import {Edit} from '@element-plus/icons-vue';

const emits = defineEmits(['templateClick', 'edit']);
const props = defineProps({
  definitionId: {
    type: String,
    default: '',
  },
});
const definitionId = defineModel('definitionId', {
  type: String,
  default: '',
});
const defaultProps = {
  children: 'children',
  label: 'name',
};
const queryParams = ref({
  searchKey: '',
  isPage: 0,
});
const templateData = ref([]);
const queryList = async () => {
  try {
    if (unref(definitionId) && unref(definitionId) !== '') {
      const res = await getListByDefinitionId(unref(definitionId));
      templateData.value = res.data || [];
    } else {
      templateData.value = [];
    }
  } catch (error) {
    ElMessage.error('获取模板失败');
    templateData.value = [];
  }
};
const handleNodeClick = (data) => {
  emits('templateClick', data);
};
const handleEdit = (data) => {
  emits('edit', data);
};

// 删除模板
const handleDelete = async (item) => {
  try {
    ElMessageBox.confirm('确定要删除该模板吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      await deleteTemplate(item.id);
      ElMessage.success('删除成功');
      queryList();
    });
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败');
    }
  }
};

const currentSelectTemplate = ref({});
defineExpose({ queryList });
</script>

<style lang="scss" scoped>
.emr-template-container {
  height: 100%;
  //  padding: 8px;

  .emr-template-scrollbar-container {
    padding: 8px;
    height: 100%;
    .scrollbar-item {
      height: 40px;
      line-height: 40px;
      border-radius: 4px;
      cursor: pointer;
      background: var(--el-color-primary-light-9);
      & + .scrollbar-item {
        margin-top: 8px;
      }
      transition: all 0.3s ease;
      .el-text {
        width: calc(100% - 0px);
        display: flex;
        justify-content: space-between;
        padding: 0 8px;
      }
      .template-item {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
