<template>
  <el-dialog :title="props.title" width="1200px" v-model="dialogVisible" :before-close="onClose">
    <el-form :model="info" :rules="rules" label-width="220px" label-position="right" label-suffix=":"
      :disabled="props.title.includes('查看')" ref="ruleFormRef" style="width: 100% !important">
      <MOSTY.Assort title="基础信息" />
      <el-row>
        <el-col :span="12">
          <el-form-item label="隐患排查开始日期" prop="investigationStartDate">
            <el-date-picker v-model="info.investigationStartDate" type="date" value-format="YYYY-MM-DD"
              placeholder="隐患排查开始日期" style="width: 350px" :disabled-date="disabledDate" @change='changetime' />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患排查结束日期" prop="investigationEndDate">
            <el-date-picker v-model="info.investigationEndDate" type="date" value-format="YYYY-MM-DD"
              placeholder="隐患排查结束日期" style="width: 350px" :disabled-date="disablednowDate" @change='changetime2' />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="隐患排查方式" prop="investigationMethodCode">
            <MOSTY.DictRidioAndSelect :dictData="D_BZ_YHPCFS" v-model="info.investigationMethodCode"
              style="width: 350px" selectType="select" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业主要负责人参加检查标识" prop="riskEnterpriseHeadFlag">
            <el-select v-model="info.riskEnterpriseHeadFlag" style="width: 350px">
              <el-option label="是" :value="1" />
              <el-option label="否" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="隐患类别" prop="categoryCode">
            <MOSTY.DictTree v-model="info.categoryCode" :zdbh="'D_TB_SBYHLB'" :placeholder="'请选择应对风险类别'" :type="'array'"
              style="width: 350px" @nameChange="onChange3" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患级别" prop="levelCode">
            <MOSTY.DictRidioAndSelect :dictData="NYHDJ" v-model="info.levelCode" style="width: 350px"
              selectType="select" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="隐患整改状态" prop="rectificationStatusCode">
            <MOSTY.DictRidioAndSelect :dictData="D_BZ_YHZGZT" v-model="info.rectificationStatusCode"
              style="width: 350px" selectType="select" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患所在组织机构" prop="hdExistOrgCode">
            <el-select v-model="info.hdExistOrgCode" clearable style="width: 350px" v-if="companyEnums.length > 0">
              <el-option v-for="item in companyEnums" :key="item.orgId" :label="item.orgNmae"
                :value="item.orgId"></el-option>
            </el-select>
            <el-input v-else v-model="info.hdExistOrgName" type="text" disabled style="width: 350px" />
            <!-- <MOSTY.Department style="width: 350px" :searchType="'01'" placeholder="隐患所在组织机构" clearable :deptType="'01'" v-model="info.hdExistOrgCode" @depitem="onChange" /> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <!-- <el-col :span="12">
          <el-form-item label="继承标识" prop="extendFlag">
            <el-select v-model="info.extendFlag" style="width: 350px">
              <el-option label="被继承的隐患" :value="'1'" />
              <el-option label="手动继承隐患" :value="'2-1'" />
              <el-option label="自动继承隐患" :value="'2-2'" />
              <el-option label="当前闭环的隐患" :value="'3'" />
            </el-select>
          </el-form-item>
        </el-col> -->

      </el-row>
      <el-row>
        <el-col :span="12" v-if="info.rectificationStatusCode == '3'">
          <el-form-item label="整改完成日期" :prop="'completionDate'">
            <el-date-picker v-model="info.completionDate" style="width: 350px" type="date" format="YYYY-MM-DD"
              value-format="YYYY-MM-DD" placeholder="请选择整改完成日期" :disabled-date="disablednowDate2" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患整改期限" prop="rectificationDeadline">
            <el-date-picker v-model="info.rectificationDeadline" type="date" value-format="YYYY-MM-DD"
              placeholder="隐患整改期限" style="width: 350px" :disabled-date="disablednowDate2" />
            <!-- <el-input v-model="info.rectificationDeadline" type="number" min="0" style="width: 350px" placeholder="隐患整改期限" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最新隐患整改标识" prop="newestRectificationFlag">
            <el-select v-model="info.newestRectificationFlag" style="width: 350px">
              <el-option label="未整改" :value="'1'" />
              <el-option label="正在整改" :value="'2'" />
              <el-option label="已整改" :value="'3'" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="隐患描述" prop="describe">
            <el-input v-model="info.describe" type="textarea" style="width: 350px" placeholder="隐患描述" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" style="margin-right: 16px" v-loading="loadingShow" @click="onSave"
          v-if="!props.title.includes('查看')"> 保存 </el-button>
        <el-button @click="onClose">关闭</el-button>

      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref, reactive, getCurrentInstance, computed } from "vue"
import * as MOSTY from "@/components/MyComponents/index"
import { getApi, postApi } from "@/api/tobAcco_api.js"
import { ElMessage } from "element-plus"
import * as rule from "@/utils/rules.js"
const { proxy } = getCurrentInstance()
const { D_BZ_YHPCFS, NYHDJ, D_BZ_YHZGZT } = proxy.$dict("D_BZ_YHPCFS", "NYHDJ", "D_BZ_YHZGZT")
const disabledDate = time => {
  return time.getTime() > Date.now()
}
const props = defineProps({
  title: String,
  id: String,
  isDetail: {
    type: Boolean,
    default: false
  },
  rowItem: Object,
  query: Object
})
const emits = defineEmits(["closeDetail"])
const dialogVisible = ref(true)
const loadingShow = ref(false)
const ruleFormRef = ref()
const info = ref({
  hdExistOrgCode: ""
})
const rules = ref({
  // ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "personnelQty"),
  // bbEohPersonnelCategoryName: [{ required: true, message: "请选择", trigger: "change" }],
  // factorName: [{ required: true, message: "请选择", trigger: "change" }],
  investigationStartDate: [{ required: true, message: "请选择隐患排查开始日期", trigger: "change" }],
  investigationEndDate: [{ required: true, message: "请选择隐患排查结束日期", trigger: "change" }],
  investigationMethodCode: [{ required: true, message: "请选择隐患排查方式", trigger: "change" }],
  riskEnterpriseHeadFlag: [{ required: true, message: "请选择企业主要负责人参加检查标识", trigger: "change" }],
  categoryCode: [{ required: true, message: "请选择隐患类别", trigger: "change" }],
  levelCode: [{ required: true, message: "请选择隐患级别", trigger: "change" }],
  rectificationStatusCode: [{ required: true, message: "请选择隐患整改状态", trigger: "change" }],
  hdExistOrgCode: [{ required: true, message: "请选择隐患所在组织机构" }],
  extendFlag: [{ required: true, message: "请选择继承标识", trigger: "change" }],
  operationType: [{ required: true, message: "请选择操作标识符", trigger: "change" }],
  newestRectificationFlag: [{ required: true, message: "请选择最新隐患整改标识", trigger: "change" }],
  describe: [{ required: true, message: "请输入隐患描述", trigger: "blur" }],
  rectificationDeadline: [{ required: true, message: "请输入隐患整改期限", trigger: "blur" }],
  completionDate: [{ required: true, message: "请选择整改完成日期", trigger: "change" }]
})
const onClose = () => {
  if(!ruleFormRef.value) return
  if (!props.title.includes('查看')) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits("closeDetail")
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits("closeDetail")
  }

}
function disablednowDate(time) {
  return time.getTime() + 24 * 60 * 60 * 1000 < new Date(info.value.investigationStartDate).getTime()
}
function disablednowDate2(time) {
  return time.getTime() + 24 * 60 * 60 * 1000 < new Date(info.value.investigationEndDate).getTime()

}
function changetime() {
  info.value.investigationEndDate = ''
  info.value.rectificationDeadline = ''
  info.value.completionDate = ''
}
function changetime2() {
  info.value.completionDate = ''
  info.value.rectificationDeadline = ''
}
const onSave = async () => {
  loadingShow.value = true
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      info.value.levelName = NYHDJ.value.find(v => v.value == info.value.levelCode).label
      info.value.rectificationStatusName = D_BZ_YHZGZT.value.find(v => v.value == info.value.rectificationStatusCode).label
      info.value.investigationMethodName = D_BZ_YHPCFS.value.find(v => v.value == info.value.investigationMethodCode).label
      if (props.rowItem.orgCode.includes('XN')) {
        info.value.hdExistOrgName = companyEnums.value.find(v => v.orgId == info.value.hdExistOrgCode).orgNmae
      }

      if (info.value.rectificationStatusCode == "3" && !info.value.completionDate) {
        ElMessage({
          message: "请输入整改完成日期",
          type: "warning"
        })
        loadingShow.value = false
        return
      }
      if (props.title.includes("新增")) {
        info.value.orgCode = props.rowItem.orgCode
        info.value.reportingYear = props.query.year
        info.value.reportCycleCode = props.query.jtsj

        postApi(info.value, "/mosty-jcgl/data/hazardInvestigation/add")
          .then(res => {
            ElMessage({
              message: "新增成功",
              type: "success"
            })
            emits("closeDetail", true)
          })
          .finally(() => {
            loadingShow.value = false
          })
      }
      if (props.title.includes("编辑")) {
        postApi(info.value, "/mosty-jcgl/data/hazardInvestigation/update")
          .then(res => {
            ElMessage({
              message: "保存成功",
              type: "success"
            })
            emits("closeDetail", true)
          })
          .finally(() => {
            loadingShow.value = false
          })
      }
    } else {
      ElMessage.warning("请输入必填项！！！")
      loadingShow.value = false
    }
  })
}
const getDetail = () => {
  let _url = props.isDetail ? "getDetailById" : "getLsjlDetail"
  getApi({ id: props.id }, `/mosty-jcgl/data/hazardInvestigation/${_url}`).then(res => {
    info.value = res
  })
}
const sbsslist = ref([])
const sbsslist2 = ref([])
const companyEnums = ref([])


const onChange3 = val => {
  info.value.categoryName = val
}

onMounted(() => {
  if (props.id) {
    getDetail()
  }
  if (props.rowItem.orgCode.includes('XN')) {
    getApi({}, "/mosty-jcgl/dataCommon/companyEnum").then(res => {
      if (res) {
        companyEnums.value = res
      } else {
        info.value.hdExistOrgName = props.rowItem.baOrgName
        info.value.hdExistOrgCode = props.rowItem.orgCode
      }
    })
  } else {
    info.value.hdExistOrgName = props.rowItem.baOrgName
    info.value.hdExistOrgCode = props.rowItem.orgCode
    companyEnums.value = []
  }
  info.value.baOrgName = props.rowItem.baOrgName



})
</script>

<style lang="scss" scoped>
.content {
  .topper {
    display: flex;

    .sub-le {
      flex: 1;
      padding: 15px 15px 5px 15px;
      border-right: 1px solid #ebeef5;
    }
  }

  .bottom {
    padding: 0 15px 10px 15px;
  }
}
</style>
