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

<script>
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: {},
      deviceTypes:[],
      formFieldsData: {
        department_id: "",
        number: "",
        name: "",
        model: "",
        brand:"",
        supplier:"",
        max_pressure:"",
        min_pressure:"",
        out_date:"",
        remark: ""
      },
      url: "hydraulic",
      formConfig: {
        formDesc: {
          department_id: {
            type: "cascader",
            label: "所属部门",
            isOptions: true,
            options: [],
            required: true,
            attrs: {
              props: {
                label: "department_name",
                value: "id",
                emitPath: false,
                checkStrictly: true
              }
            }
          },
          number: {
            type: "input",
            label: "编号",
            required:true,
          },
           name: {
            type: "input",
            label: "名称",
            required:true
          },
          model: {
            type: "select",
            label: "型号",
            required:true,
     
             options: [],
          },
           brand: {
            type: "input",
            label: "品牌"
          },
          supplier: {
            type: "input",
            label: "供应商"
          },
          max_pressure: {
            type: "input",
            label: "最大压力"
          },
          min_pressure: {
            type: "input",
            label: "最小压力"
          },
          out_date: {
            type: "date",
            label: "出厂日期"
          },
          remark: {
            type: "textarea",
            label: "显示信息"
          }
        },
        rules: {
          max_pressure:  { validator: validateMaxPressure, trigger: 'blur' } ,
          min_pressure:  { validator: validateMinPressure, trigger: 'blur' } ,
        },
        order: ["department_id","number", "name", "model","brand","supplier","min_pressure","max_pressure","out_date","remark"]
      }
    };
  },
  created() {
    this.$http.get("departments").then(response => {
      this.formConfig.formDesc.department_id.options = response.data;
    });
     // 0-液压泵
    this.$http.get("get_device_mold",{ params: {type:0} }).then(resp => {
        this.formConfig.formDesc.model.options = resp.data
     });
  },
  methods: {
    handleFormSubmit(data) {
      this.$parent.handleSubmit();
    },
    handleRequest(data) {
      return Promise.resolve();
    },
    handleRequestSuccess() {
      this.$message.success("发送成功");
    }
  },
  computed: {
    DialogVisible: {
      set(val) {
        this.$emit("sendVal", val); // 表示将子组件改变的值传递给父组件
      },
      get() {
        return this.formModelVisible; // 表示获取父组件的值
      }
    }
  }
};
</script>