<template>
  <el-dialog v-model="shouShuDialog" title="修改或添加手术" @closed="emit('close')">
    <el-form ref="formShouShuRef" :model="formShouShu" :rules="shouShuRules" label-width="100px" size="small">
      <el-form-item label="手术名称" prop="ssmc">
        <el-input v-model="formShouShu.ssmc" disabled></el-input>
      </el-form-item>
      <el-form-item label="手术编码" prop="ssbm">
        <input-and-table
            v-model="formShouShu.ssbm"
            :data="shouShuData"
            :table-header="current"
            valueId="code"
            @currentChange="shouShuCurrentPage"
            @fetchData="queryShouShuClick"
            @rowClick="rowClick"
        />
      </el-form-item>

      <el-form-item label="科室" prop="deptCode">
        <xc-select v-model="formShouShu"
                   :name="['deptCode','deptCodeName']"
                   :data=" props.deptList">
        </xc-select>
      </el-form-item>

      <el-form-item label="手术日期" prop="ssrq">
        <el-date-picker v-model="formShouShu.ssrq" type="datetime"></el-date-picker>
      </el-form-item>
      <el-form-item label="手术时间" prop="opStartDate">
        <el-date-picker
            v-model="formShouShu.opDate"
            :shortcuts="shortcuts"
            end-placeholder="结束日期"
            placeholder="选择日期"
            range-separator="至"
            size="small"
            start-placeholder="开始日期"
            style="width: 400px"
            type="datetimerange"
            @change="surgeryDateChanges"/>
      </el-form-item>
      <el-form-item label="麻醉时间" prop="anstDate">
        <el-date-picker
            v-model="formShouShu.anstDate"
            :shortcuts="shortcuts"
            end-placeholder="结束日期"
            placeholder="选择日期"
            range-separator="至"
            size="small"
            start-placeholder="开始日期"
            style="width: 400px"
            type="datetimerange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="手术级别" prop="ssjb">
        <el-select v-model="formShouShu.ssjb">
          <el-option :key="1" :label="1" :value="1"></el-option>
          <el-option :key="2" :label="2" :value="2"></el-option>
          <el-option :key="3" :label="3" :value="3"></el-option>
          <el-option :key="4" :label="4" :value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="术者" prop="ssysName">
        <SelectStaffCode v-model="formShouShu" :name="['ssys', 'ssysName']"></SelectStaffCode>
      </el-form-item>
      <el-form-item label="I助" prop="sszsName1">
        <SelectStaffCode v-model="formShouShu" :name="['sszs1', 'sszsName1']"></SelectStaffCode>
      </el-form-item>
      <el-form-item label="II助" prop="sszsName2">
        <SelectStaffCode v-model="formShouShu" :name="['sszs2', 'sszsName2']"></SelectStaffCode>
      </el-form-item>
      <el-form-item label="切口愈合等级" prop="qkName">
        <el-select v-model="formShouShu.qkName" @change="formQieKou">
          <el-option v-for="item in qks" :key="item.code" :label="item.name" :value="item.code">
            <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="麻醉方式" prop="mzff">
        <el-select v-model="formShouShu.mzff">
          <el-option v-for="item in maZuiFangShis" :key="item.code" :label="item.name" :value="item.code">
            <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="麻醉医生" prop="mzys">
        <SelectStaffCode v-model="formShouShu" :name="['mzys', 'mzysName']"></SelectStaffCode>
      </el-form-item>
    </el-form>
    <el-button @click="addShouShuClick">保存</el-button>
  </el-dialog>
</template>

<script name="JieSuanDanXiuGaiShouShu" setup>
import SelectStaffCode from '@/components/SelectStaffCode.vue'
import {maZuiFangShis, qks} from '../jieSuanDanXiuGai'
import {shouShuBianMa,validateYbCode} from '@/api/medical-insurance/si-setl-upload'
import {current} from '@/data/input-table-header'
import {shortcuts} from '@/data/shortcuts.js'
import {getDateRangeFormatDateTime} from '@/utils/date'
import XcComboGrid from "@/components/xiao-chan/combo-grid/XcComboGrid";
import InputAndTable from "@/components/input-table-query/InputAndTable";
import XcSelect from "@/components/xiao-chan/select/XcSelect";
import {stringIsBlank, stringNotBlank} from "../../../utils/blank-utils";
import {ElMessage, ElMessageBox} from 'element-plus'
const props = defineProps({
  data: {
    type: Object,
  },
  deptList: {
    type: Array
  }
})
const emit = defineEmits(['addShouShuClick', 'close'])

let shouShuDialog = $ref(true)
const formShouShuRef = ref(null)
let formShouShu = $ref({
  id: 0,
  ssxh: 0,
  ssmc: '',
  ssrq: '',
  ssjb: '',
  ssbm: '',
  ssys: '',
  ssysName: '',
  sszs1: '',
  sszsName1: '',
  sszs2: '',
  sszsName2: '',
  qkjb: '',
  yhqk: '',
  qkName: '',
  mzff: '',
  mzys: '',
  mzysName: '',
  opStartDate: '',
  opEndDate: '',
  anstStartDate: '',
  anstEndDate: '',
  opDate: [],
  anstDate: [],
})

let shouShuData = $ref({
  data: [],
  total: 0,
  currentPage: 1,
})

const surgeryDateChanges = (date) => {
  let opdateS = getDateRangeFormatDateTime(date)
  formShouShu.ssrq = opdateS.startTime
}

// 限制
const shouShuRules = ref({
  ssmc: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  ssrq: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  ssjb: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  ssbm: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  ssysName: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  qkName: [{required: true, message: '该项不能为空', trigger: 'blur'}],
  opStartDate: [{required: true, message: '该项不能为空', trigger: 'blur'}],
})

const formQieKou = (val) => {
  formShouShu.qkjb = val.split('/')[0]
  formShouShu.yhqk = val.split('/')[1]
}

const shouShuCurrentPage = (inputData, val) => {
  shouShuData.currentPage = val
  shouShuBianMa(inputData, val).then((res) => {
    shouShuData.data = res.records
    shouShuData.total = res.total
  })
}

const queryShouShuClick = (val) => {
  shouShuBianMa(val, 1).then((res) => {
    shouShuData.data = res.records
    shouShuData.total = res.total
  })
}

const rowClick = (row) => {
  formShouShu.ssmc = row.name
}

const addShouShuClick = async () => {
  let form = unref(formShouShuRef)
  let opdateS = getDateRangeFormatDateTime(formShouShu.opDate) //3389
  formShouShu.opStartDate = opdateS.startTime
  formShouShu.opEndDate = opdateS.endTime

  let anstDateS = getDateRangeFormatDateTime(formShouShu.anstDate)
  formShouShu.anstStartDate = anstDateS.startTime
  formShouShu.anstEndDate = anstDateS.endTime

  if (!form) return
  try {
    await form.validate()
    let flag = true
    if(stringNotBlank(formShouShu.ssys)){
        await  validateYbCode(formShouShu.ssys).then((res) => {
          if(stringNotBlank(res)){
            flag = false
            return ElMessage.error(res)
          }
        })
    }
    if(stringNotBlank(formShouShu.mzys)){
        await  validateYbCode(formShouShu.mzys).then((res) => {
          if(stringNotBlank(res)){
            flag = false
            return ElMessage.error(res)
          }
        })
    }
    if(flag){
      emit('addShouShuClick', formShouShu)
    }
  } catch (e) {
    console.log(e)
  }
}

 const  jianchaysCode= async  (code) => {
  let message = '';
  await  validateYbCode(code).then((res) => {
    message = res
   })

   return message
 }


onMounted(() => {
  if (props.data) {
    formShouShu = props.data
    formShouShu.qkName = formShouShu.qkjb + '/' + formShouShu.yhqk
    formShouShu.opDate = [formShouShu.opStartDate, formShouShu.opEndDate]
    formShouShu.anstDate = [formShouShu.anstStartDate, formShouShu.anstEndDate]
  }

})
</script>

<style scoped></style>
