<template>
  <ele-form-dialog
    v-bind="formConfig"
    v-model="formFieldsData"
    v-dialogDrag
    :title="title"
    :request-fn="handleFormSubmit"
    :visible.sync="DialogVisible"
    custom-class="abow_dialog"
    label-width="100px"
    label-position="left"
    :dialogAttrs="{ 'close-on-click-modal': false,'top':'8vh'}"
     v-if="fresh"
  />
</template>

<script>
import { array } from 'jszip/lib/support';
export default {
  props: ["formModelVisible", "title"],
  data() {
    // var validateMaxPressure = (rule, value, callback) => {
    //     if ( (value !== '') && (this.formFieldsData.min_pressure !=='')) {
    //         var min_pressure = this.formFieldsData.min_pressure - 0;
    //         var max_pressure = value - 0;
    //         if (min_pressure > max_pressure) {
    //             callback(new Error('不能小于最小压力'))
    //         } else {
    //             callback()
    //         }
    //     } else {
    //        callback()
    //     }
    // };
    // var validateMinPressure = (rule, value, callback) => {
    //     if ( (value !== '') && (this.formFieldsData.max_pressure !=='')) {
    //         var max_pressure = this.formFieldsData.max_pressure - 0;
    //         var min_pressure = value - 0;
    //         if (min_pressure > max_pressure) {
    //             callback(new Error('不能大于最大压力'))
    //         } else {
    //             callback()
    //         }
    //     } else {
    //        callback()
    //     }
    // };
    return {
      formData: {},
      fresh:true,
      deviceTypes:[],
      formFieldsData: {
        id:'',
        equipment_type:"",
        equipment_model:"",
        material_number: "",
        name: "",
        issue_unit:"",
        fixed_asset_number:"",
        fixed_asset_number2:'',
        serial_number:"",
        factory_number:"",
        check_last_time:"",
        check_next_time:"",
        check_status:"",
        status:"",
        list:"",
      },
      url: "hydEquipment",
      formConfig: {
        formDesc: {
          equipment_type:{
            layout: 12,
            type:"select",
            label:'设备类型',
            required:true,
            options:[],
            disabled (data) {
             if(data.id)
             {
              return true;
             }
            return false;
          }
          },
          equipment_model: {
            layout: 12,
            type: "input",
            label: "型号",
          },
           name: {
             layout: 12,
            type: "input",
            label: "名称",
            required:true
          },
          material_number: {
            layout: 12,
            type: "input",
            label: "物料号",
            required:true,
          },
           issue_unit: {
            layout: 12,
            type: "input",
            label: "发放单位",
          },
          fixed_asset_number:
          {
            layout: 12,
            type: "input",
            label: "固定资产编号",
          },
          fixed_asset_number2:
          {
            layout: 12,
            type: "input",
            label: "类固资产编号",
          },
          serial_number:{
            layout: 12,
            type: "input",
            label: "序列号",

          },
          factory_number:{
            layout: 12,
            type: "input",
            label: "出厂编号"
          },
          check_last_time:{
            layout: 12,
            type: "date",
            label: "上次校验时间"
          },
          check_next_time:{
            layout: 12,
            type: "date",
            label: "下次校验时间"
          },
          check_status:{
            layout: 12,
            type: "select",
            label: "校验状态",
            options:[],

          },
          status:{
            layout: 12,
            type: "select",
            label: "状态",
            options:[],

          },

          department_id: {
            layout: 12,
            type: "cascader",
            label: "所属部门",
            isOptions: true,
            options: [],
            required: true,
            attrs: {
              props: {
                label: "department_name",
                value: "id",
                emitPath: false,
                checkStrictly: true
              }
            },
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type<5)
              {
                return true;
              }
              return false;
            }
          },
          number:{
            layout: 12,
            type: "input",
            label: "编号",
            required:true,
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type<5)
              {
                return true;
              }
              return false;
            }
          },
          imei: {
            layout: 12,
            type: "input",
            label: "IMEI号",
            required:true,
             vif(data)
            {
              if(data.equipment_type&&data.equipment_type==2)
              {
                return true;
              }
              return false;
            }

          },
          angle_sensor:{
            type: 'switch',
            label: "角度传感器",
            vif(data)
            {
              if(data.equipment_type==3||data.equipment_type==4)
              {
                return true;
              }
              return false;
            }
          },
          pressure:{
            layout: 12,
            type: "input",
            label: "压力",
            vif(data)
            {
              if(data.equipment_type==3||data.equipment_type==4)
              {
                return true;
              }
              return false;
            }
          },
           torque:{
            layout: 12,
            type: "input",
            label: "扭矩",
            vif(data)
            {
              if(data.equipment_type==3||data.equipment_type==4)
              {
                return true;
              }
              return false;
            }
          },
          max_pressure: {
            layout: 12,
            type: "input",
            required: true,
            label: "最大压力",
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type==2)
              {
                
                return true;
              }
              return false;
            }

          },
          min_pressure: {
            layout: 12,
            type: "input",
            required: true,
            label: "最小压力",
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type==2)
              {
                return true;
              }
              return false;
            }
          },
          effective_period: {
             layout: 12,
            type: "input",
            required: true,
            label: "有效周期",
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type==2)
              {
                return true;
              }
              return false;
            }
          },
         
          remark: {
            type: "textarea",
            label: "显示信息",
            attrs: {
              autosizeType: "switch",
              autosize: false,
              rows: 2
            },
            vif(data)
            {
              if(data.equipment_type&&data.equipment_type<5)
              {
                return true;
              }
              return false;
            }
          }
        },
        order: ['equipment_type','equipment_model',"name","material_number",'issue_unit','fixed_asset_number','fixed_asset_number2',"serial_number",'factory_number','check_last_time','check_next_time','check_status','status',"number","department_id","pressure","pressure","torque",'angle_sensor',"imei","min_pressure","max_pressure", "effective_period","remark"]
      }
    };
  },
  created() {
    this.$http.get("departments").then(response => {
      this.formConfig.formDesc.department_id.options = response.data;
    });
     this.$http.get("getHydEquipmentType",{ params: {code:'ToolStatus'} }).then(response => {
      this.formConfig.formDesc.status.options = response.data;
    });
      this.$http.get("getHydEquipmentType",{ params: {code:'Check'} }).then(response => {
      this.formConfig.formDesc.check_status.options = response.data;
    });
      this.$http.get("findAllByPid",{ params: {id:1} }).then(resp => {
        let data= resp.data;
        let ar=[];
        data.forEach((item) => {
            ar.push({text:item.name,value:item.id})
        });
        this.formConfig.formDesc.equipment_type.options = ar;
     });
  },
  methods: {
    //更新内容
    update()
    {
      // this.formFieldsData.equipment_type=this.formFieldsData.equipment_type;

      if(this.formFieldsData.list)
      {
        this.formFieldsData.number = this.formFieldsData.list.number;
        this.formFieldsData.remark = this.formFieldsData.list.remark;
        this.formFieldsData.department_id = this.formFieldsData.list.department_id;
        if(this.formFieldsData.equipment_type==2)
        {
          //泵
           this.formFieldsData.imei = this.formFieldsData.list.imei;
           this.formFieldsData.max_pressure= this.formFieldsData.list.max_pressure;
           this.formFieldsData.min_pressure= this.formFieldsData.list.min_pressure;
           this.formFieldsData.effective_period = this.formFieldsData.list.effective_period
        }
        else
        {
          //扳手
           this.formFieldsData.angle_sensor = this.formFieldsData.list.angle_sensor;
           this.formFieldsData.pressure= this.formFieldsData.list.pressure;
           this.formFieldsData.torque= this.formFieldsData.list.torque;
          
        }

      }

    },
    handleFormSubmit(data) {
      this.$parent.handleSubmit();
    },
    handleRequest(data) {
      return Promise.resolve();
    },
    handleRequestSuccess() {
      this.$message.success("发送成功");
    }
  },
  computed: {
    DialogVisible: {
      set(val) {
          this.fresh = false;
            this.$nextTick(() => {
            this.fresh = true
            })
        this.$emit("sendVal", val); // 表示将子组件改变的值传递给父组件
      },
      get() {

        if(this.formModelVisible)
        {
          this.update();
  
        }
        return this.formModelVisible; // 表示获取父组件的值
      }
    }
  }
};
</script>