<template>
  <el-dialog :title="props.title" width="1200px" v-model="dialogVisible" :before-close="onClose">
    <el-form :model="info" :rules="rules" 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.baOrgName" disabled style="width: 300px;" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="职业病确诊人数" prop="occupationalDiseaseQty">
            <el-input-number v-model="info.occupationalDiseaseQty" :precision="0" style="width: 300px;" />
          </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-icon>
            <DocumentChecked />
          </el-icon>
          <span>保存</span> </el-button>
        <el-button @click="onClose"> <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span></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 props = defineProps({
  title: String,
  id: String,
  isDetail: {
    type: Boolean,
    default: false
  }
});
const emits = defineEmits(["close"]);
const dialogVisible = ref(true);
const loadingShow = ref(false)
const ruleFormRef = ref()
const info = ref({})
const rules = ref({
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "occupationalDiseaseQty"),
});
const onClose = () => {
  if(!ruleFormRef.value) return
  if (props.title.includes('编辑')) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits('close')
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits('close')
  }

};
const onSave = async () => {
  loadingShow.value = true
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      postApi(info.value, "/mosty-jcgl/data/occupational/updateAll")
        .then((res) => {
          ElMessage({
            message: "保存成功",
            type: "success"
          });
          emits("close", 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/occupational/${_url}`)
    .then((res) => {
      info.value = res
    })
}
onMounted(() => {
  if (props.id) {
    getDetail()
  }
});
</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>
