<template>
  <el-dialog :title="props.title" v-model="isShowDetail" width="1000px" :before-close="closed">
    <div class="config">
      <el-form ref="Fromrefs" label-width="auto" :model="info" label-suffix="：" :rules="rules" :disabled="!props.isEdit"
        label-position="right" style="display: flex; justify-content: start; flex-wrap: wrap;width: 100% !important;">
        <el-form-item label="统计周期" prop="reportingYear">
          <el-input v-model="info.yearStr" style="width: 250px;" disabled />
        </el-form-item>
        <el-form-item label="专职安全员培训人次" prop="ftsoTrainPersonQty">
          <el-input-number style="width: 250px;" v-model="info.ftsoTrainPersonQty" :min="0" />
        </el-form-item>
        <el-form-item label="专职安全员年度培训总学时" prop="ftsoAnnyalTotalHour">
          <el-input-number style="width:250px;" v-model="info.ftsoAnnyalTotalHour" :precision="1" :min="0" />
        </el-form-item>
        <el-form-item label="新员工三级安全教育人数" prop="neEducationQty">
          <el-input-number style="width: 250px;" v-model="info.neEducationQty" :min="0" />
        </el-form-item>
        <el-form-item label="新员工年度培训总学时" prop="neAnnualTotalHour">
          <el-input-number style="width: 250px;" v-model="info.neAnnualTotalHour" :precision="1" :min="0" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">

        <el-button type="primary" v-loading="loadingShow" @click="handleSave(Fromrefs)" v-if="props.isEdit"> <el-icon>
            <DocumentChecked />
          </el-icon>
          <span>保存</span> </el-button>
        <el-button @click="closed"> <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span></el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from 'vue'
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
import * as rule from "@/utils/rules.js";
const { proxy } = getCurrentInstance();
const Fromrefs = ref()
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  detailId: {
    type: String,
    default: ''
  },
  id: {
    type: String,
    default: ''
  },
  isEdit: {
    type: Boolean,
    default: true
  },
})
const emits = defineEmits(["close"]);
const isShowDetail = ref(true)
const loadingShow = ref(false)
const rules = reactive({
  ...rule.numbernineRule({ validator: true, require: true, iszs: true, firstNum: 8, lastNum: 1 }, "ftsoAnnyalTotalHour"),
  ...rule.numbernineRule({ validator: true, require: true, iszs: true, firstNum: 8, lastNum: 1 }, "neAnnualTotalHour"),
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "ftsoTrainPersonQty"),
  ...rule.numberzzsRule({ validator: true, require: true, num: 99999 }, "neEducationQty"),
})
const info = ref({})
const closed = () => {
  if (!Fromrefs.value) return
  if (props.isEdit) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        Fromrefs.value.clearValidate();
        emits("close");
      })
      .catch(() => {
        // catch error
      })
  } else {
    Fromrefs.value.clearValidate();
    emits("close");
  }

};
// 获取详情
const getDetail = () => {
  let _url = props.id ? '/mosty-jcgl/data/safetyTrainStaff/getLsjlDetail' : '/mosty-jcgl/data/safetyTrainStaff/getDetailById'
  let id = props.id ? props.id : props.detailId
  getApi({ id }, _url).then((res) => {
    info.value = res || {}
    let _bn = info.value.reportCycleCode == 2 ? '下半年' : '上半年'
    info.value.yearStr = (info.value.reportingYear|| '') + '年-' + _bn
  })
}
//保存
const handleSave = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      loadingShow.value = true
      if (info.value.yearStr) {
        delete info.value.yearStr
      }
      postApi(info.value, '/mosty-jcgl/data/safetyTrainStaff/updateAll').then(res => {
        ElMessage({
          message: '保存成功',
          type: 'success',
        })
        emits("close", true);
      }).finally(() => {
        loadingShow.value = false
      });
    } else {
      ElMessage.warning('请输入必填项！！！')
    }
  })
}


onMounted(() => {
  getDetail()
})
</script>
<style scoped lang="scss">
::v-deep .el-form-item--default {
  margin-left: 10px;
}
</style>
