<template>
  <el-dialog
    title="参数设置"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="1200px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="130px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row>
        <el-col :span="24">
          <el-row>
            <el-col :span="12">
              <el-form-item label="电表品牌型号：" prop="productName">
                <el-input
                  placeholder="请输入"
                  v-model="textarea">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电表品牌型号：" prop="productName">
                <el-input
                  placeholder="请输入"
                  v-model="textarea">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-row>
            <el-col :span="12">
              <el-form-item label="计算模式：" prop="productName">
                <el-input
                  placeholder="请输入"
                  v-model="textarea">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="以电折水系数：" prop="productName">
                <el-input
                  placeholder="请输入"
                  v-model="textarea">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: right">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { assign } from 'lodash-es';
export default {
  name: 'setPreferenc',
  props: {
    data: {
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogSubmitLoading: false,
      dialogVisible: false,
      baseForm: {
        productValue: '',
        productName: '',
        stockAmount: '',
        goodsUnit: '',
        goodsSpecValue: '',
        imageArr1: [],
        supplierId: '',
        imageArr2: []
      },
      hasFetched: false,
      rules: {
        stockAmount: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ],
        sourceinfoId: [
          { required: true, message: '请选择管辖范围', trigger: 'blur' }
        ],
        productName: [
          { required: true, message: '请输入协会名称', trigger: 'blur' }
        ],
        supplierId: [
          { required: false, message: '请输入协会介绍', trigger: 'blur' }
        ],
        goodsSpecValue: [
          { required: true, message: '请选择规格', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
    data: {
      deep: true,
      handler(val) {
        assign(this.baseForm, val);
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.resetFormData();
      this.dialogVisible = false;
    },
    resetFormData() {
      this.baseForm = {};
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid)
      })
    },

    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    }
  }
};
</script>

<style lang="scss" scoped>
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
