<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row >
      基本信息
        <a-col :span="24">
          <a-form-item label="应急事件名称" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" placeholder="请输入应急事件名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="事件类型" v-bind="validateInfos.tp">
	          <j-dict-select-tag v-model:value="formData.tp" dictCode="event_tp" placeholder="请选择事件类型" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <!--
        <a-col :span="24">
          <a-form-item label="事件等级" v-bind="validateInfos.level">
	          <j-dict-select-tag v-model:value="formData.level" dictCode="level" placeholder="请选择事件等级" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        -->
        <a-col :span="24">
          <a-form-item label="牵头处置部门" v-bind="validateInfos.handleDepart">
            <a-input v-model:value="formData.handleDepart" placeholder="请输入牵头处置部门" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="参与部门" v-bind="validateInfos.supportDepart">
            <a-input v-model:value="formData.supportDepart" placeholder="请输入参与部门" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="事件发生时间" v-bind="validateInfos.sttime">
		        <a-date-picker placeholder="请选择事件发生时间" v-model:value="formData.sttime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <!--
        <a-col :span="24">
          <a-form-item label="结束时间" v-bind="validateInfos.endtime">
		        <a-date-picker placeholder="请选择结束时间" v-model:value="formData.endtime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="处置结果" v-bind="validateInfos.ret">
            <a-input v-model:value="formData.ret" placeholder="请输入处置结果" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="状态" v-bind="validateInfos.eventSta">
	          <a-input-number v-model:value="formData.eventSta" placeholder="请输入状态" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        -->
        <a-col :span="24">
          <a-form-item label="说明" v-bind="validateInfos.des">
            <a-input v-model:value="formData.des" placeholder="请输入说明" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        事件等级判断
        <a-col :span="24">
          <a-form-item label="影响的分支机构数(省级)" v-bind="validateInfos.affectedBranchCount">
	          <a-input-number v-model:value="formData.affectedBranchCount" placeholder="请输入影响的分支机构数(省级)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="当前中断时长（分钟）" v-bind="validateInfos.outageDuration">
	          <a-input-number v-model:value="formData.outageDuration" placeholder="请输入当前中断时长（分钟）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../BcsRiskEvent.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['addHandle', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    name: '',   
    tp: undefined,
    level: undefined,
    handleDepart: '',   
    supportDepart: '',   
    sttime: '',   
    endtime: '',   
    ret: '',   
    eventSta: undefined,
    des: '',   
    affectedBranchCount: undefined,
    outageDuration: undefined,
  });
  const { createMessage,createConfirm } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }else {
        model.eventSta=0;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }

    let sjmc="";
    let outageDuration=model.outageDuration;
    if(outageDuration==undefined){
      createMessage.warning("当前中断时长（分钟）不能为空");
      return false;
    }else if(outageDuration>360){

       model.level=1;
       sjmc="I级事件中断事件";
    }else if(outageDuration<180){

       model.level=3;
       sjmc="III级事件中断事件";
    }else {

       model.level=2;
       sjmc="II级事件中断事件";
    }

    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          //createMessage.success(res.message);
          createConfirm({
            centered:true,
            iconType: 'warning',
            title: '注意',
            content: '经判断当前已达到'+sjmc+'，是否立即填写应急处置方案？',
            okText: '立即填报',
            cancelText: '稍后填报',
            onOk: () => {
              emit('addHandle',res.result);
            },
            onCancel() {
              emit('ok');
            },

          });
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
