<template>
  <div>
    <el-dialog v-model="props.diaShow" title="工作总结生成条件" :before-close="handleClose" label-width="120px" width="800px"
      draggable>
      <el-form ref="ruleFormRefs" :model="dialogForm" :rules="rules">
        <el-form-item label="标题" prop="titleName">
          <el-input placeholder="请输入标题" disabled v-model="dialogForm.titleName" clearable></el-input>
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-select v-model="dialogForm.year" filterable @change="changeData">
            <el-option v-for="item in filterYear(3)" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="层级" prop="type">
          <MOSTY.DictRidioAndSelect :dictData="props.dict.D_BZ_WORKTYPE" v-model="dialogForm.type" selectType="select"
            style='width:824px;' @getselect="changeData" :disabledItem="dialogForm.isBm ? ['1'] : []" />
        </el-form-item>
        <el-form-item label="类型" prop="yearType">
          <el-select v-model="dialogForm.yearType" filterable @change="changeData">
            <el-option v-for="item in props.dict.D_BZ_WORD_YEAR_TYPE" :key="item.value" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleSave(ruleFormRefs)" :loading="loading">
            <el-icon>
              <DocumentChecked />
            </el-icon>
            <span>保存</span>
          </el-button>
          <el-button type="primary" @click="handleClose">
            <el-icon>
              <DocumentDelete />
            </el-icon>
            <span>关闭</span>
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, reactive, watch, getCurrentInstance } from "vue";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import { filterYear } from "@/utils/auth.js";
import { getItem } from "@/utils/storage";
import { postApi } from "@/api/tobAcco_api.js";
const { proxy } = getCurrentInstance();
const rules = reactive({
  year: [{ required: true, message: "年份不能为空", trigger: "blur" }],
  titleName: [{ required: true, message: "标题不能为空", trigger: "blur" }],
  type: [{ trigger: "blur", required: true, message: "层级不能为空", }],
  yearType: [{ trigger: "blur", required: true, message: "类型不能为空", }],
});
const loading = ref(false); //新增保存按钮截流
const emits = defineEmits(["close"], ["save"], ["saveItem"]);
const ruleFormRefs = ref()
const dialogForm = ref({
  type: '',
});
const props = defineProps({
  diaShow: {
    type: Boolean,
    default: false
  },
  dict: Array
});
// 切换各种选项
const changeData = () => {
  let yearTypeName = dialogForm.value.yearType == '1' ? '上半年' : '年';
  let typeName = dialogForm.value.type == '1' ? '' : '（本级）';
  let orgBizType = getItem('deptId').orgBizType
  if (!['04', '52', '02'].includes(orgBizType)) {
    dialogForm.value.isBm = true
    typeName = ''
    dialogForm.value.titleName = `${dialogForm.value.orgname}${dialogForm.value.deptname}${typeName}${dialogForm.value.year}${yearTypeName}安全生产工作总结（报告）`
  } else {
    dialogForm.value.titleName = `${dialogForm.value.orgname}${typeName}${dialogForm.value.year}${yearTypeName}安全生产工作总结（报告）`
  }
}
watch(
  () => props.diaShow,
  (val) => {
    if (val) {
      dialogForm.value.year = new Date().getFullYear()
      dialogForm.value.fileName = null
      dialogForm.value.orgcode = getItem('deptId').ssdwid
      dialogForm.value.orgname = getItem('deptId').ssdwmc
      dialogForm.value.deptid = getItem('deptId').ssbmid
      dialogForm.value.deptname = getItem('deptId').ssbmmc
      let orgBizType = getItem('deptId').orgBizType
      dialogForm.value.titleName = `${dialogForm.value.orgname}${dialogForm.value.year}年安全生产工作总结（报告）`
      if (!['04', '52', '02'].includes(orgBizType)) {
        dialogForm.value.isBm = true
        dialogForm.value.titleName = `${dialogForm.value.orgname}${dialogForm.value.deptname}${dialogForm.value.year}年安全生产工作总结（报告）`
      }
      dialogForm.value.type = ''
      dialogForm.value.yearType = ''
    }
  }
)

// 保存
const handleSave = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true;
      postApi(dialogForm.value, "/mosty-jcgl/worksummary/createWorkSummary").then(() => {
        ElMessage.success("新增成功");
        loading.value = false;
        emits("close", false);
      }).catch(() => {
        loading.value = false;
      })
    } else {
      ElMessage.warning('请输入必填项！！！')
      loading.value = false
    }
  })
};
// 关闭
const handleClose = () => {
  proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？').then(() => {
    emits("close", false);
  })
};
</script>

<style lang="scss" scoped>
.el-form-item {
  width: 100%;
}

.el-form {
  flex-wrap: wrap;
}

.el-range-editor.el-input__inner {
  width: 100%;
}

::v-deep .el-dialog__body {
  height: 30vh !important;
}
</style>