<template>
  <el-dialog :title="props.title" v-model="isShowDetail" width="1120px" :before-close="closed">
    <div class="config">
      <el-form ref="ruleFormRef" label-width="auto" :model="info" label-suffix="：" :rules="rules"
        :disabled="props.title.includes('详情')" label-position="left">
        <el-row>
          <el-col :span="11">
            <el-form-item label="公司名称">
              <el-input v-model="info.orgName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="统计年度">
              <el-select disabled v-model="info.reportingYear" placeholder="请选择统计年度" style="width: 350px;">
                <el-option v-for="item in filterYear(7)" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="上报周期">
              <!-- <el-input v-model="info.reportCycleCode" disabled /> -->
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_HALF_YEAR" v-model="info.reportCycleCode" selectType="select"
                disabled />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="风险单元名称" prop="riskUnitName">
              <el-input v-model="info.riskUnitName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="风险评价类型" prop="riskEvaluateTypeCode">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_FXLX" v-model="info.riskEvaluateTypeCode" selectType="select" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="风险单元等级" prop="riskUnitLevelCode">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_FXDJ" :disabledItem="['1', '2']"
                v-model="info.riskUnitLevelCode" selectType="select" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="onSave" v-if="!props.title.includes('详情')" style="margin-right: 10px">
          <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, onMounted, getCurrentInstance, computed } from "vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import * as rule from "@/utils/rules.js";
import { filterYear } from "@/utils/auth.js"

const { proxy } = getCurrentInstance();
const { D_BZ_FXLX, D_BZ_FXDJ, D_BZ_HALF_YEAR } = proxy.$dict("D_BZ_FXLX", "D_BZ_FXDJ", "D_BZ_HALF_YEAR");
const props = defineProps({
  title: {
    type: String,
    default: ""
  },
  detail: {
    type: Object,
    default: () => { }
  },
  type: {
    type: String,
    default: ""
  },
  rowItem: {
    type: Object,
    default: () => ({})
  }
});
const emit = defineEmits(["close"]);
const isShowDetail = ref(true);
const ruleFormRef = ref();
const isAdd = computed(() => {
  return props.title.includes('新增')
})
const rules = reactive({
  'riskUnitName': [{ required: true, message: '风险单元名称', trigger: 'blur' }],
  'riskEvaluateTypeCode': [{ required: true, message: '风险评价类型', trigger: 'blur' }],
  'riskUnitLevelCode': [{ required: true, message: '风险单元等级', trigger: 'blur' }],
});
const info = ref({});
onMounted(() => {


  if (isAdd.value) {
    info.value = {
      orgCode: props.rowItem.orgCode,
      orgName: props.rowItem.orgName,
      statId: props.rowItem.id,
      reportingYear: filterYear(7)[0].value, // 默认当年
      reportCycleCode: isFirstHalfOfYear() // 默认当年
    }
  } else if (props.type == 'report') {
    info.value = props.detail

  } else {
    if (props.detail.id) {
      getApi({}, `/mosty-jcgl/data/riskUnit/selectOne/${props.detail.id}`).then((res) => {
        info.value = res;
        info.value.reportCycleCode = res.reportCycleCode.toString();
      });
    } else {
      info.value = {
        orgCode: props.rowItem.orgCode,
        orgName: props.rowItem.orgName,
        statId: props.rowItem.id,
        reportingYear: filterYear(7)[0].value, // 默认当年
        reportCycleCode: isFirstHalfOfYear() // 默认当年
      }
    }



  }
});
const isFirstHalfOfYear = () => {
  const now = new Date(); // 获取当前日期
  const month = now.getMonth() + 1; // 获取当前月份（0-11）  1
  if (month <= 6) {
    return "1";
  } else {
    return "2";
  }
}
const onSave = async () => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      info.value.riskEvaluateTypeName = D_BZ_FXLX.value.find(v => v.value == info.value.riskEvaluateTypeCode).label
      info.value.riskUnitLevelName = D_BZ_FXDJ.value.find(v => v.value == info.value.riskUnitLevelCode).label
      const params = {
        ...info.value,
      }
      const url = isAdd.value ? "/mosty-jcgl/data/riskUnit/saveRiskUnit" : "/mosty-jcgl/data/riskUnit/editRiskUnit"
      postApi(params, url)
        .then(() => {
          ElMessage.success("保存成功");
          emit("close", true);
        })
        .finally(() => {
        });
    }
  });
};

const closed = () => {
  if(!ruleFormRef.value) return
  if (!props.title.includes('详情')) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emit("close");
      })
      .catch(() => {
        // catch error
      })
  } else {
    emit("close");
  }

};
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  height: 100% !important;
}

.config {
  height: 55vh;
}

::v-deep .el-form-item--default {
  margin-left: 10px;
}
</style>
