<template>
  <el-container>
    <el-aside>
      <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=" overflow: auto" :style="{height: getWindowSize.h / 1.2 + 'px'}">
        <div v-show="chaZhaoLeiXing === 1">
          <JyJcTree yjyc="jy" @nodeClick="nodeClick"/>
        </div>
        <div v-show="chaZhaoLeiXing !== 1">
          <jy-jc-template-tree :data="templateData"
                               @node-click="tempClick"
                               @del-click="clickDeleteTemplate"/>
        </div>
      </div>
    </el-aside>
    <el-main>
      <el-button @click="dianJiBaoCunShenQing">保存</el-button>
      <el-button @click="clickSaveTemplate">存为模板</el-button>
      <cuo-wu-xin-xi/>
      <!--   这里是检查的数据信息     -->
      <el-table :data="xuanZhongJianCha.data"
                :header-cell-class-name="starAdd"
                :height="windowSize.h / 1.4">
        <el-table-column label="操作" width="90" fixed="left">
          <template #default="scope">
            <el-button-group>
              <el-button icon="Edit" type="primary" @click="dianJiBianJi(scope.row, scope.$index)"></el-button>
              <el-button icon="Delete" type="danger" @click="deleteInspectionCheck(scope.$index)"></el-button>
            </el-button-group>
          </template>
        </el-table-column>
        <el-table-column label="项目编码" prop="orderCode"></el-table-column>
        <el-table-column label="项目名称" prop="orderName" show-overflow-tooltip></el-table-column>
        <el-table-column label="检查时间" prop="startTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="执行科室" prop="execDeptName"></el-table-column>
        <el-table-column label="急诊标识">
          <template #default="scope">
            <span v-if="scope.row.jzFlag === 1">√</span>
          </template>
        </el-table-column>
        <el-table-column label="自费">
          <template #default="scope">
            <span v-if="scope.row.ybSelfFlag === 1">√</span>
          </template>
        </el-table-column>
        <el-table-column v-if="!jianCha" label="标本" prop="inspectStuffName" show-overflow-tooltip></el-table-column>
        <el-table-column :label="jianCha ? '病室摘要' : '申请说明'" prop="reqComment"
                         show-overflow-tooltip></el-table-column>
        <el-table-column v-if="jianCha" label="体征信息" prop="reqTzComment" show-overflow-tooltip></el-table-column>
        <el-table-column v-if="jianCha" label="辅检结果" prop="reqOtherResult" show-overflow-tooltip></el-table-column>
        <el-table-column v-if="jianCha" label="临床诊断" prop="diagText" show-overflow-tooltip></el-table-column>
      </el-table>
    </el-main>
  </el-container>

  <el-dialog v-model="baoCunMuBan.dialog" center title="保存检查模板" width="30%">
    <el-form ref="baoCunMuBanRef" :model="baoCunMuBan" :rules="baoCunMuBanJiaoYan" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="模板类型" prop="orderType">
            <el-radio-group v-model="baoCunMuBan.orderType">
              <el-radio-button :label="1">个人</el-radio-button>
              <el-radio-button :label="2">科室</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="名称" prop="tcName">
            <el-input v-model="baoCunMuBan.tcName" ref="tcNameRef" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序码" prop="sortNo">
            <el-input-number v-model="baoCunMuBan.sortNo" :max="9999" :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="录入方式" prop="tcFlag">
            <el-radio-group v-model="baoCunMuBan.tcFlag">
              <el-radio-button :label="0">自动选择</el-radio-button>
              <el-radio-button :label="1">手工录入</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-button @click="dianJiBaoCunMuBan(baoCunMuBanRef)">提交</el-button>
          <el-button>关闭</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>

  <BianJiJianYan ref="bianJiRef"
                 :jian-cha="jianCha"
                 @fanYe="bianJiFanYe"/>
</template>

<script setup name="TianJiaJianChaJianYan">
import {computed, onMounted, ref} from 'vue'
import {
  baoCunJianChaJianYanMuBan,
  baoCunJianYanJianCha,
  getTemplate,
  jianChaJianYanMuBanMing,
  shanShuJianChaMuBan,
} from '@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing'
import store from '@/store'
import {ElMessage, ElMessageBox} from 'element-plus'
import {stringNotBlank} from '@/utils/blank-utils'
import BianJiJianYan
  from '@/views/hospitalization/zhu-yuan-yi-sheng/jian-cha-jian-yan-shen-qing/EditInspection.vue'
import {
  cuoWuXinXi,
  huanZheXinXi,
  youWuXuanZheHuanZhe,
  xuanZhongJianCha
} from '@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng'
import {getServerDateApi, yaoPinXiangMuPiPeiYiBao} from '@/api/public-api'
import CuoWuXinXi from '@/components/zhu-yuan-yi-sheng/CuoWuXinXi.vue'
import Sleep from '@/utils/sleep'
import {BizException, ExceptionEnum} from "@/utils/BizException";
import JyJcTree from "@/components/zhu-yuan-yi-sheng/yi-zhu-lu-ru/jy-jc-tree/JyJcTree.vue";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import JyJcTemplateTree
  from "@/views/hospitalization/zhu-yuan-yi-sheng/jian-cha-jian-yan-shen-qing/JyJcTemplateTree.vue";
import {getWindowSize} from '@/utils/window-size'


const props = defineProps({
  jianCha: {
    type: Boolean,
    default: true,
  },
})
const chaZhaoLeiXing = ref(1)

const windowSize = computed(() => {
  return store.state.app.windowSize
})

const bianJiRef = ref(null)
const baoCunMuBanRef = ref()
const tcNameRef = $ref(null)
const baoCunMuBan = ref({
  dialog: false,
  tcName: '',
  orderType: 1,
  sortNo: 0,
  tcFlag: 1,
  fuGaiYuanShuJu: false,
  chongFuMing: false,
})

const templateData = ref()
const dianJiLeiXingChaXun = () => {
  getTemplate('2', chaZhaoLeiXing.value).then((res) => {
    templateData.value = res
  })
}

const tempClick = async (data) => {
  if (data.isTheParentNode === '2') {
    if (xuanZhongJianCha.value.weiBianMa.indexOf(data.orderCode) > -1) {
      BizException(ExceptionEnum.MESSAGE_ERROR, '请勿重复添加。')
    }
    data.startTime = await getServerDateApi()
    xuanZhongJianCha.value.weiBianMa.push(data.orderCode)
    xuanZhongJianCha.value.data.push(data)
  }
}

const dianJiBianJi = (row, index) => {
  bianJiRef.value.daKaiBenDuiHua(row, index, xuanZhongJianCha.value.data.length)
}

const dianJiBaoCunShenQing = () => {
  if (xuanZhongJianCha.value.data.length < 0) {
    ElMessage.error('请选择数据')
    return
  }
  if (youWuXuanZheHuanZhe()) {
    clickSaveTemplate()
    return
  }
  let listCode = []
  xuanZhongJianCha.value.data.forEach((item) => {
    listCode.push(item.orderCode + '-00')
  })
  yaoPinXiangMuPiPeiYiBao(listCode).then((res) => {
    if (stringNotBlank(res)) {
      cuoWuXinXi.value = res
      ElMessageBox.confirm(res, '提示', {
        type: 'warning',
        dangerouslyUseHTMLString: true,
      }).then(() => {
        baoCunShuJu()
      }).catch(() => {
      })
    } else {
      baoCunShuJu()
    }
  })
}

function baoCunShuJu() {
  ElMessageBox.confirm('确定要保存这些数据吗？', '提示', {
    type: 'warning',
  }).then(() => {
    let data = huanZheXinXi.value
    data.list = xuanZhongJianCha.value.data
    data.reqType = props.jianCha ? 3 : 2
    baoCunJianYanJianCha(data)
        .then((res) => {
          xuanZhongJianCha.value.weiBianMa = []
          xuanZhongJianCha.value.data = []
        })
        .catch((e) => {
          cuoWuXinXi.value = e
        })
  }).catch(() => {
  })
}

const clickDeleteTemplate = (data) => {
  ElMessageBox.confirm('是否要删除该模板', '提示', {
    type: 'warning',
  })
      .then(() => {
        shanShuJianChaMuBan(data.code, store.state.user.info.deptCode).then(() => {
          dianJiLeiXingChaXun()
        })
      })
      .catch(() => {
      })
}

const baoCunMuBanJiaoYan = ref({
  tcName: [
    {required: true, message: '名称不能为空', trigger: 'blur'},
    {min: 1, max: 30, message: '长度在 1 到 30 个字', trigger: 'blur'},
  ],
  orderType: [{required: true, message: '名称不能为空', trigger: 'blur'}],
  sortNo: [{required: true, message: '名称不能为空', trigger: 'blur'}],
  tcFlag: [{required: true, message: '名称不能为空', trigger: 'blur'}],
})

const dianJiBaoCunMuBan = async (form) => {
  if (!form) return
  try {
    await form.validate()
    jianChaJianYanMuBanMing(baoCunMuBan.value.tcName, props.jianCha ? '3' : '2').then((res) => {
      if (res) {
        ElMessageBox.confirm('已经存在相同的模板名称，是否覆盖数据', '温馨提示', {
          type: 'warning',
          distinguishCancelAndClose: true,
          confirmButtonText: '覆盖',
          cancelButtonText: '取消',
        })
            .then(() => {
              faSongBaoCunMuBan()
            })
            .catch((e) => {
            })
      } else {
        faSongBaoCunMuBan()
      }
    })
  } catch (e) {
    console.log(e)
  }
}

const faSongBaoCunMuBan = () => {
  baoCunMuBan.value.reqType = props.jianCha ? 3 : 2
  baoCunMuBan.value.list = xuanZhongJianCha.value.data
  baoCunMuBan.value.tcExec = store.state.user.info.deptCode
  baoCunJianChaJianYanMuBan(baoCunMuBan.value).then((res) => {
    dianJiLeiXingChaXun()
    baoCunMuBan.value.dialog = false
  })
}

const bianJiFanYe = (val) => {
  let index = val.val - 1
  bianJiRef.value.daKaiBenDuiHua(xuanZhongJianCha.value.data[index], index, xuanZhongJianCha.value.data.length)
}

const clickSaveTemplate = async () => {
  if (xuanZhongJianCha.value.data.length > 0) {
    baoCunMuBan.value.dialog = true
    await Sleep(200)
    tcNameRef.focus()
  } else {
    ElMessage.error('请选择数据')
  }
}

const starAdd = (obj) => {
  let index = [0, 1, 2, 3, 6, 7, 8, 9]
  if (index.indexOf(obj.columnIndex) > -1) {
    return 'star'
  }
}


const nodeClick = async (val) => {
  if (val.code.length < 5) {
    BizException(ExceptionEnum.LOGICAL_ERROR, '无法添加目录')
  }
  let temp = {
    orderType: "",
    execDept: val.execUnit,
    execDeptName: val.execUnitName,
    inspectStuff: val.inspectStuff,
    orderCode: val.code,
    startTime: await getServerDateApi(),
    inspectStuffName: val.inspectStuffName,
    orderName: val.name,
    checkFlag: "0",
    jzFlag: 0,
    ybSelfFlag: 0
  }
  if (xuanZhongJianCha.value.weiBianMa.indexOf(temp.orderCode) > -1) {
    xcMessage.error('请勿重复添加。')
  } else {
    xuanZhongJianCha.value.weiBianMa.push(temp.orderCode)
    xuanZhongJianCha.value.data.push(temp)
  }
}

const deleteInspectionCheck = (index) => {
  xuanZhongJianCha.value.data.splice(index, 1)
  xuanZhongJianCha.value.weiBianMa.splice(index, 1)
}

onMounted(() => {
  dianJiLeiXingChaXun()
})

</script>

<style scoped>
:deep(.el-table .children-row) {
  background: rgba(145, 247, 145, 0.5);
}

:deep(.el-table) {
  --el-table-row-hover-background-color: #85dbfd7a;
}
</style>
