<template>
  <el-radio-group v-model="chaZhaoLeiXing" @change="dianJiLeiXingChaXun">
    <el-radio-button :label="1">新申请</el-radio-button>
    <el-radio-button :label="2">个人模板</el-radio-button>
    <el-radio-button :label="3">科室模板</el-radio-button>
  </el-radio-group>
  <div style="height: 700px;overflow: auto">
    <div v-show="chaZhaoLeiXing === 1">
      <JyJcTree yjyc="jc" @node-click="nodeClick"/>
    </div>
    <div v-show="chaZhaoLeiXing !== 1">
      <xc-table
          @rowClick="rowClick"
          :local-data="templateData"
          :height="500"
          layout='total, prev, pager, next'>
        <el-table-column label="名称" prop="name"/>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click.stop="deleteTemplate(scope.row,scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </xc-table>
    </div>
  </div>
</template>

<script setup name='NewApplication'>

import {ref} from "vue";
import {
  chaKanFenLeiXiangQing,
  getCheckNewApplication,
  getTemplate, shanShuJianChaMuBan
} from "@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing";
import XcTable from "@/components/xiao-chan/xc-table/XcTable";
import JyJcTree from "@/components/zhu-yuan-yi-sheng/yi-zhu-lu-ru/jy-jc-tree/JyJcTree.vue";
import {getServerDateApi} from "@/api/public-api";
import {ElMessageBox} from "element-plus";
import store from "@/store";

const props = defineProps({
  returnData: Function,
  returnItem: Function
})

const config = {value: 'code', label: 'name', children: 'children'}

const chaZhaoLeiXing = ref(1)

const newApplication = ref([])
const templateData = ref([])


const rowClick = ({code, bwCode}) => {
  chaKanFenLeiXiangQing(code, chaZhaoLeiXing.value === 1 ? bwCode : 'mb', true).then((res) => {
    props.returnData(res)
  })
}

const dianJiLeiXingChaXun = () => {
  if (chaZhaoLeiXing.value !== 1) {
    getTemplate('3', chaZhaoLeiXing.value).then((res) => {
      templateData.value = res
    })
  }
}

// 树状图点击
// {
// "code": "003726",
//     "name": "术中经食管超声心动图",
//     "classes": "03",
//     "type": 0,
//     "execUnit": "2020300",
//     "execUnitName": null,
//     "bw": "心脏彩超",
//     "children": null
// }
const nodeClick = async (data) => {
  let temp = {
    execDept: data.execUnit,
    execDeptName: data.execUnitName,
    orderCode: data.code,
    orderName: data.name,
    orderType: data.classes,
    startTime: await getServerDateApi()
  }
  props.returnItem(temp, true)
}

const deleteTemplate = (val, index) => {
  ElMessageBox.confirm('是否要删除该模板', '提示', {
    type: 'warning',
  })
      .then(() => {
        shanShuJianChaMuBan(val.code, store.state.user.info.deptCode).then(() => {
          templateData.value.splice(index, 1)
        })
      })
      .catch(() => {
      })
}

onMounted(() => {
  getCheckNewApplication().then((res) => {
    newApplication.value = res
  })
})

</script>

<style scoped lang="scss">

</style>
