<template>
  <el-dialog v-model="dialog" :close-on-click-modal="false" :close-on-press-escape="false"
             :show-close="false"
             :title="jianCha ? '添加检查' : '添加检验'"
             top="15%" width="100%">
    <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>
        <el-table :data="jianChaFenLeiShuJu.data" :height="windowSize.h / 1.6" highlight-current-row stripe>
          <el-table-column label="名称" prop="name"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button size="small" split-button type="primary"
                         @click="ceBianLanDianJi('查看',scope.row)">查看
              </el-button>
              <el-button v-if="chaZhaoLeiXing !== 1" type="danger" @click="ceBianLanDianJi('删除',scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            :current-page="jianChaFenLeiShuJu.currentPage"
            :page-size="20"
            :pager-count="5"
            :total="jianChaFenLeiShuJu.total"
            layout="prev,pager,next,total"
            small
            @current-change="jianChaFenLeiFenYe">
        </el-pagination>
      </el-aside>
      <el-main>
        <el-button @click="closeDialog">关闭</el-button>
        <el-button :disabled="xuanZhongJianCha.data.length === 0" @click="dianJiBaoCunShenQing">保存</el-button>
        <el-button :disabled="xuanZhongJianCha.data.length === 0" @click="baoCunMuBan.dialog = true">存为模板</el-button>
        <!--   这里是检查的数据信息     -->
        <el-table :data="xuanZhongJianCha.data" :height="windowSize.h / 1.9">
          <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="急诊标识" prop="jzFlag">
            <template #default="scope">
              <span v-if="scope.row.jzFlag === 1">√</span>
            </template>
          </el-table-column>
          <el-table-column label="自费" prop="ybSelfFlag">
            <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-column label="编辑" width="120">
            <template #default="scope">
              <el-button icon="el-icon-edit" round type="primary"
                         @click="dianJiBianJi(scope.row,scope.$index)"></el-button>
              <el-button icon="el-icon-delete" round type="danger"
                         @click="xuanZhongJianCha.data.splice(scope.$index,1);
                         xuanZhongJianCha.weiBianMa.splice(scope.$index,1)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
    <el-dialog v-model="xiangXiShuJu.dialog">
      <el-button @click="dianJiQueRenXuanZhong">确定</el-button>
      <el-button @click="xiangXiShuJu.dialog = false">取消</el-button>
      <el-table :data="xiangXiShuJu.data" :height="windowSize.h / 2" @selection-change="xuanZhong">
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="项目名称" prop="orderName"></el-table-column>
        <el-table-column label="说明" prop="ybComment"></el-table-column>
      </el-table>
    </el-dialog>
  </el-dialog>
  <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" 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>
  <bian-ji-jian-cha-shen-qing ref="bianJiRef" :jian-cha="jianCha" @fanYe="bianJiFanYe"></bian-ji-jian-cha-shen-qing>
</template>

<script>
import {computed, onMounted, ref} from "vue";
import {
  baoCunJianChaJianYanMuBan,
  baoCunJianYanJianCha,
  chaKanFenLeiXiangQing,
  jianChaJianYanFenLei,
  jianChaJianYanMuBanMing,
  shanShuJianChaMuBan
} from "@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing";
import store from "@/store";
import {clone} from "@/utils/clone";
import {ElMessage, ElMessageBox} from "element-plus";
import {listNotBlank} from "@/utils/blank-utils";
import BianJiJianChaShenQing from "@/views/hospitalization/zhu-yuan-yi-sheng/jian-cha-shen-qing/BianJiJianChaJianYanShenQing.vue";
import {huanHangXianShi} from "@/utils/date";
import {huanZheXinXi, youWuXuanZheHuanZhe} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";

export default {
  name: "TianJiaJianCha",
  components: {BianJiJianChaShenQing},
  props: {
    jianCha: {
      type: Boolean,
      default: true,
    }
  },
  setup(props) {
    const dialog = ref(false)
    const chaZhaoLeiXing = ref(1)
    const xuanZhongJianCha = ref({
      data: [],
      weiBianMa: [],
      daiXuanZhong: []
    })
    const biaoBenShuJu = ref([])

    const windowSize = computed(() => {
      return store.state.app.windowSize
    })
    const xiangXiShuJu = ref({
      dialog: false,
      data: [],
    })
    const jianChaFenLeiShuJu = ref({
      currentPage: 1,
      total: 0,
      data: []
    })
    const bianJiRef = ref(null)
    const baoCunMuBanRef = ref()
    const baoCunMuBan = ref({
      dialog: false,
      tcName: '',
      orderType: 1,
      sortNo: 0,
      tcFlag: 1,
      fuGaiYuanShuJu: false,
      chongFuMing: false
    })

    const dianJiLeiXingChaXun = () => {
      jianChaFenLeiShuJu.value.total = 0
      jianChaFenLeiShuJu.value.currentPage = 1
      jianChaJianYanFenLei(0, 1, chaZhaoLeiXing.value, props.jianCha).then((res) => {
        jianChaFenLeiShuJu.value.data = res.records
        jianChaFenLeiShuJu.value.total = res.total
      })
    }

    const jianChaFenLeiFenYe = (val) => {
      jianChaFenLeiShuJu.value.currentPage = val
      jianChaJianYanFenLei(jianChaFenLeiShuJu.value.total, val, chaZhaoLeiXing.value, props.jianCha).then((res) => {
        jianChaFenLeiShuJu.value.data = res.records
      })

    }

    const xuanZhong = (val) => {
      xuanZhongJianCha.value.daiXuanZhong = val
    }

    const dianJiQueRenXuanZhong = () => {
      let tiShi = false
      xuanZhongJianCha.value.daiXuanZhong.forEach((item) => {
        if (xuanZhongJianCha.value.weiBianMa.indexOf(item.orderCode) > -1) {
          tiShi = true
        } else {
          xuanZhongJianCha.value.data.push(clone(item))
          xuanZhongJianCha.value.weiBianMa.push(clone(item.orderCode))
        }
      })
      if (tiShi) {
        ElMessage.error('请勿重复添加。')
      }
      xiangXiShuJu.value.dialog = false
    }

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

    const closeDialog = () => {
      dialog.value = false
    }

    const dianJiBaoCunShenQing = () => {
      if (youWuXuanZheHuanZhe()) return
      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 = []
      })
    }


    const ceBianLanDianJi = (name, {code, bwCode}) => {
      if (!props.jianCha) {
        bwCode = ''
      }
      if (name === '查看') {
        chaKanFenLeiXiangQing(code, chaZhaoLeiXing.value === 1 ? bwCode : 'mb', props.jianCha).then((res) => {
          if (listNotBlank(res)) {
            xiangXiShuJu.value.data = res
            xiangXiShuJu.value.dialog = true
          } else {
            ElMessage.error('没有找到对应的数据')
          }
        })
      } else if (name === '删除') {
        ElMessageBox.confirm('是否要删除该检查模板', '提示', {
          type: "warning"
        }).then(() => {
          shanShuJianChaMuBan(code, store.state.user.info.deptCode)
        }).catch((e) => {

        })
      }
    }

    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) => {
            })
          }
        })
      } catch (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()
      })
    }

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

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

    return {
      dialog,
      jianChaFenLeiShuJu,
      jianChaFenLeiFenYe,
      windowSize,
      xiangXiShuJu,
      xuanZhong,
      dianJiQueRenXuanZhong,
      xuanZhongJianCha,
      closeDialog,
      chaZhaoLeiXing,
      dianJiLeiXingChaXun,
      dianJiBianJi,
      bianJiRef,
      huanHangXianShi,
      dianJiBaoCunShenQing,
      ceBianLanDianJi,
      baoCunMuBan,
      baoCunMuBanRef,
      dianJiBaoCunMuBan,
      baoCunMuBanJiaoYan,
      bianJiFanYe,
      jianCha: props.jianCha,
      biaoBenShuJu
    }
  }
}
</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>
