<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="压力测试ID" v-bind="validateInfos.testId">
	          <a-input-number v-model:value="formData.testId" placeholder="请输入压力测试ID" style="width: 100%" />
          </a-form-item>
        </a-col>
        -->
        
        <a-col :span="12">
          <a-form-item label="业务不可容忍的财务损失中断时长" v-bind="validateInfos.shuthhFina">
	          <a-input-number v-model:value="formData.shuthhFina" :min="0" :max="99999999" :step="0.01" :precision="2" placeholder="请输入业务不可容忍的财务损失中断时长" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务不可容忍的声誉损失中断时长" v-bind="validateInfos.shuthhRepu">
	          <a-input-number v-model:value="formData.shuthhRepu" :min="0" :max="99999999" :step="0.01" :precision="2" placeholder="请输入业务不可容忍的声誉损失中断时长" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务不可容忍的法律法规损失中断时长" v-bind="validateInfos.shuthhLaw">
	          <a-input-number v-model:value="formData.shuthhLaw" :min="0" :max="99999999" :step="0.01" :precision="2" placeholder="请输入业务不可容忍的法律法规损失中断时长" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务不可容忍的客户损失中断时长" v-bind="validateInfos.shuthhBusi">
	          <a-input-number v-model:value="formData.shuthhBusi" :min="0" :max="99999999" :step="0.01" :precision="2" placeholder="请输入业务不可容忍的客户损失中断时长" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="数据损毁率(%)" v-bind="validateInfos.rateDatabad">
	          <a-input-number v-model:value="formData.rateDatabad" :min="0" :max="100" :step="0.01" :precision="2" placeholder="请输入数据损毁率(%)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="系统修复时长(H)" v-bind="validateInfos.sysRepdhh">
	          <a-input-number v-model:value="formData.sysRepdhh" :min="0" :max="99999999" :step="0.01" :precision="2" placeholder="请输入系统修复时长(H)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
       
        <a-col :span="12">
          <a-form-item label="不可容忍的非经济损失中断时长(H)" v-bind="validateInfos.shuthhNoeco">
	          <a-input-number v-model:value="formData.shuthhNoeco" placeholder="请输入不可容忍的非经济损失中断时长(H)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="不可容忍的经济损失中断时长(H)" v-bind="validateInfos.shuthhEco">
	          <a-input-number v-model:value="formData.shuthhEco" placeholder="请输入不可容忍的经济损失中断时长(H)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="监管处罚风险" v-bind="validateInfos.riskReg">
            <a-input v-model:value="formData.riskReg" placeholder="请输入监管处罚风险" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="商誉损失风险" v-bind="validateInfos.riskBusi">
            <a-input v-model:value="formData.riskBusi" placeholder="请输入商誉损失风险" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        
        <a-col :span="10">
        </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 { getValueType } from '/@/utils';
  import { saveOrUpdate,queryByTestId } from '../BcsStreTestexe.api';
  import { Form } from 'ant-design-vue';
  
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true },
    testId: { type: Number, default: 0 }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    testId: undefined,
    shuthhFina: undefined,
    shuthhRepu: undefined,
    shuthhLaw: undefined,
    shuthhBusi: undefined,
    rateDatabad: undefined,
    sysRepdhh: undefined,
    shuthhNoeco: undefined,
    shuthhEco: undefined,
    riskReg: '',   
    riskBusi: '',   
  });
  const { createMessage,createConfirm } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 12 } });
  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 testId = ref<number>(0);

  // 表单禁用
  const disabled = computed(()=>{
    testId.value=props.testId;

   // alert(props.testId);
    //alert(testId.value);

     queryByTestId({testId:testId.value}).then((res) => {
     //console.log("res ",res);
    
     formData.testId=res.testId;
    formData.shuthhFina=res.shuthhFina;
    formData.shuthhRepu=res.shuthhRepu;
    formData.shuthhLaw=res.shuthhLaw;
    formData.shuthhBusi=res.shuthhBusi;
    formData.rateDatabad=res.rateDatabad;
    formData.sysRepdhh=res.sysRepdhh;
    formData.shuthhNoeco=res.shuthhNoeco;
    formData.shuthhEco=res.shuthhEco;
    formData.riskReg=res.riskReg;   
    formData.riskBusi=res.riskBusi;   
    });
   

    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  

  function addcs(testId) {
   
  }

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

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

  
   function openJsTitle() {

    let model = formData;

   let sjmc="";
    //业务不可容忍的财务损失中断时长
    let shuthhFina=model.shuthhFina;
    if(shuthhFina==undefined){
      createMessage.warning("业务不可容忍的财务损失中断时长不能为空");
      return false;
    }
    //业务不可容忍的声誉损失中断时长
    let shuthhRepu=model.shuthhRepu;
    if(shuthhRepu==undefined){
      createMessage.warning("业务不可容忍的声誉损失中断时长不能为空");
      return false;
    }
    //业务不可容忍的法律法规损失中断时长
    let shuthhLaw=model.shuthhLaw;
    if(shuthhLaw==undefined){
      createMessage.warning("业务不可容忍的法律法规损失中断时长不能为空");
      return false;
    }
    //业务不可容忍的客户损失中断时长
    let shuthhBusi=model.shuthhBusi;
    if(shuthhBusi==undefined){
      createMessage.warning("业务不可容忍的客户损失中断时长不能为空");
      return false;
    }
    let shuthhNoeco=shuthhRepu+shuthhLaw+shuthhBusi;
    let riskReg = "";
    if(shuthhLaw<2){
    riskReg="高";
    }else if(shuthhLaw>=2&&shuthhLaw<12){
    riskReg="中";
    }else if(shuthhLaw>=12&&shuthhLaw<24){
    riskReg="较低";
    }else {
    riskReg="低";
    }


    let riskBusi = "";
    let riskBusival=shuthhRepu;
    if(shuthhRepu>shuthhBusi){
     riskBusival=shuthhBusi;
    }

    if(riskBusival<2){
    riskBusi="高";
    }else if(riskBusival>=2&&riskBusival<12){
    riskBusi="中";
    }else if(riskBusival>=12&&riskBusival<24){
    riskBusi="较低";
    }else {
    riskBusi="低";
    }
    formData.shuthhNoeco=shuthhNoeco;
    formData.shuthhEco=shuthhFina;
    formData.riskReg=riskReg;
    formData.riskBusi=riskBusi;

     const strHtml = `<ul>
                <li>不可容忍的非经济损失中断时长(H):${shuthhNoeco}</li>
                <li>不可容忍的经济损失中断时长(H):${shuthhFina}</li>
                <li>监管处罚风险:${riskReg}</li>
                <li>商誉损失风险:${riskBusi}</li>
            </ul>`;

     createConfirm({
            centered:true,
            iconType: 'warning',
            title: '计算结果',
            content: strHtml,
            okText: '认可',
            cancelText: '取消',
            onOk: () => {
             // emit('addHandle',res.result);
             submitForm();
            },
            onCancel() {
             // emit('ok');
            },

          });



    }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    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(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  /**
   * 取消按钮回调事件
   */
  function handleClose() {
    emit('ok');
  }


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

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

 

</style>
