<template>
  <el-dialog :title="props.title" width="1200px" v-model="dialogVisible" :before-close="onClose">
    <el-form :model="info" :rules="rules" label-width="200px" 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="企业名称">
            <el-input v-model="info.orgName" disabled style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="场所名称" prop="placeEquipmentName">
            <el-input v-model="info.placeEquipmentName" style="width: 300px" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="熏蒸杀虫作业周期" prop="fumigationOpsCycleCode">
            <MOSTY.DictRidioAndSelect :dictData="D_BZ_XSCZYZQ" v-model="info.fumigationOpsCycleCode"
              style="width: 300px" selectType="select" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作业人员数量" prop="fumigationOpsPersonQty">
            <el-input-number v-model="info.fumigationOpsPersonQty" :precision="0" style="width: 300px;" :min="1" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="作业相关方名称" prop="name">
            <el-input v-model="info.name" :precision="0" style="width: 300px" :min="1" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="熏蒸杀虫作业熏蒸方式" prop="fumigationOpsModeCode">
            <MOSTY.DictRidioAndSelect :dictData="D_BZ_XZFS" v-model="info.fumigationOpsModeCode" style="width: 300px"
              selectType="select" />
          </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"
// import { query } from "express"
const { proxy } = getCurrentInstance()
const { D_BZ_XSCZYZQ, D_BZ_XZFS } = proxy.$dict("D_BZ_XSCZYZQ", "D_BZ_XZFS")
const props = defineProps({
  title: String,
  detail: {
    type: Object,
    default: () => { }
  },
  rowItem: Object,
  query: Object
})
const emits = defineEmits(["close"])
const dialogVisible = ref(true)
const loadingShow = ref(false)
const ruleFormRef = ref()
const info = ref({})
const sbsslist = ref([])
const rules = ref({
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "dsyxkjCount"),
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "dxyxkjCount"),
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "fumigationOpsPersonQty"),
  // ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "hazardFactor"),
  placeEquipmentName: [{ required: true, message: "场所（设备）名称不能为空", trigger: "blur" }],
  // fumigationOpsPersonQty: [{ required: true, message: "作业人员数量不能为空", trigger: "blur" }],
  hazardFactor: [{ required: true, message: "有限空间危害因素不能为空", trigger: "blur" }],
  fumigationOpsCycleCode: [{ required: true, message: "熏蒸杀虫作业周期不能为空", trigger: "blur" }],
  fumigationOpsModeCode: [{ required: true, message: "熏蒸杀虫作业熏蒸方式不能为空", trigger: "blur" }],
  name: [{ required: true, message: "作业相关方名称不能为空", trigger: "blur" }]
  // factorName: [{ required: true, message: "请选择", trigger: "change" }]
})
const onClose = () => {
  if(!ruleFormRef.value) return
  if (!props.title.includes('详情')) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits("close")
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits("close")
  }

}
const onChange3 = val => {
  // formData.value.emergencyPlanCategoryCode = val.value
  info.value.accidentCategoryName = val.toString()
}
const onSave = async () => {
  loadingShow.value = true
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      info.value.fumigationOpsCycleName = D_BZ_XSCZYZQ.value.find(v => v.value == info.value.fumigationOpsCycleCode).label
      info.value.fumigationOpsModeName = D_BZ_XZFS.value.find(v => v.value == info.value.fumigationOpsModeCode).label
      if (props.title.includes("新增")) {
        info.value.orgCode = props.rowItem.orgCode
        info.value.reportingYear = props.query.year
        info.value.reportCycleCode = props.query.jd
        postApi(info.value, "/mosty-jcgl/data/fumigationOpsSite/add")
          .then(res => {
            ElMessage({
              message: "新增成功",
              type: "success"
            })
            emits("close", true)
          })
          .finally(() => {
            loadingShow.value = false
          })
      }
      if (props.title.includes("编辑")) {
        postApi(info.value, "/mosty-jcgl/data/fumigationOpsSite/edit")
          .then(res => {
            ElMessage({
              message: "保存成功",
              type: "success"
            })
            emits("close", true)
          })
          .finally(() => {
            loadingShow.value = false
          })
      }
    } else {
      ElMessage.warning("请输入必填项！！！")
      loadingShow.value = false
    }
  })
}

defineExpose({ info })

onMounted(() => {
  info.value = props.detail ? props.detail : {}

  if (props.title.includes("新增")) {
    info.value.baOrgName = props.rowItem.orgName
    info.value.baOrgCode = props.rowItem.baOrgCode
    info.value.orgName = props.rowItem.orgName
    info.value.orgCode = props.rowItem.orgCode
  }
})
</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>
