<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="60%"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改干燥区' : '新增干燥区'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-row gutter="100">
        <el-col :span="11">
          <el-form-item label-width="100px" label="干燥区编码:" prop="code">
            <el-input
              clearable
              :disabled="true"
              v-model="form.code"
              placeholder="干燥区编码"
            />
          </el-form-item>
        </el-col>

        <el-col :span="11">
          <el-form-item label-width="100px" label="干燥区名称:" prop="name">
            <el-input
              clearable
              v-model="form.name"
              placeholder="请输入干燥区名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label-width="100px" label="位置:" prop="region">
            <el-input
              clearable
              v-model="form.region"
              placeholder="请输入位置"
            />
          </el-form-item>
        </el-col>

        <el-col :span="11">
          <el-form-item label-width="100px" label="规格:" prop="specification">
            <el-input
              clearable
              v-model="form.specification"
              placeholder="请输入规格"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label-width="100px" label="状态:" prop="status">
            <DictSelection
              dictName="干燥区状态"
              clearable
              v-model="form.status"
            >
            </DictSelection>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item
            label-width="100px"
            label="分类:"
            prop="categoryLevelName"
          >
            <el-input
              v-model="form.categoryLevelName"
              @click.native="openCategory"
              readonly
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-table
        ref="multipleTable"
        :data="form.aridRegionList"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
      >
        <el-table-column label="干燥区编号" prop="code" min-width="120">
          <template slot-scope="{ row, $index }">
            <el-form-item
              :prop="'aridRegionList.' + $index + '.code'"
              :rules="rules.code"
            >
              <el-input
                placeholder="请输入"
                clearable
                v-model="row.code"
              ></el-input>
            </el-form-item> </template
        ></el-table-column>
        <el-table-column label="干燥区长(cm)" min-width="120">
          <template slot-scope="scope">
            <el-form-item
              :prop="'aridRegionList.' + scope.$index + 'length'"
              :rules="rules.length"
            >
              <el-input
                placeholder="请输入"
                type="number"
                v-model.number="scope.row.length"
                clearable
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="干燥区宽(cm)" min-width="120">
          <template slot-scope="scope">
            <el-form-item
              :prop="'aridRegionList.' + scope.$index + '.breadth'"
              :rules="rules.breadth"
            >
              <el-input
                placeholder="请输入"
                type="number"
                v-model.number="scope.row.breadth"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column label="基本干燥区面积(cm²)" min-width="120">
          <template slot-scope="scope">
            <el-form-item
              :prop="'aridRegionList.' + scope.$index + '.area'"
              :rules="rules.area"
            >
              <el-input
                placeholder="请输入"
                type="number"
                v-model.number="scope.row.area"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>

    <CategoryDialog
      ref="categoryRefs"
      @chooseCategory="changeCategory"
    ></CategoryDialog>
  </ele-modal>
</template>

<script>
  import CategoryDialog from './CategoryDialog.vue';
  import { getCode } from '@/api/codeManagement/index.js';
  import { save, update } from '@/api/ledgerAssets/dryArea';

  export default {
    components: {
      CategoryDialog
    },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      rootId: [Number, String]
    },
    data() {
      const defaultForm = {
        code: '',
        name: '',
        categoryLevelName: '',
        categoryLevelId: '',
        status: '',
        region: '',
        specification: '',
        aridRegionList: []
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          name: [
            { required: true, message: '请输入干燥区名称', trigger: 'blur' }
          ],
          categoryLevelName: [
            { required: true, message: '请选择分类', trigger: 'change' }
          ],

          status: [
            { required: true, message: '请选择状态', trigger: 'change' }
          ],
          code: [
            {
              required: true,
              message: '请输入干燥区编码',
              trigger: 'blur'
            }
          ],
          region: [{ required: true, message: '请输入位置', trigger: 'blur' }],
          region: [{ required: true, message: '请输入位置', trigger: 'blur' }]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },

    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          if (!this.isUpdate) {
            delete this.form.id;
          }
          const data = {
            ...this.form
          };
          data.positionList = data.aridRegionList;
          delete data.aridRegionList;
          // const saveOrUpdate = this.isUpdate ? update : save;
          save(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },

      openCategory() {
        this.$refs.categoryRefs.open(this.rootId, '选择分类');
      },

      changeCategory(row) {
        this.$set(this.form, 'categoryLevelName', row.name);
        this.$set(this.form, 'categoryId', row.id);
      },

      async _getCode() {
        const code = await getCode('dryArea');
        this.form.code = code;
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
            console.log('this.form-------------', this.form);
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
            this._getCode();
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .el-select {
    position: relative;
    .el-select__tags {
      position: inherit;
      transform: translateY(0);
      padding: 3px 0;
      min-height: 28px;
    }
    .el-select__tags ~ .el-input {
      height: 100%;
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      .el-input__inner {
        min-height: 20px;
        height: 100% !important;
      }
    }
    .el-select__input.is-mini {
      min-height: 20px;
    }
  }
</style>
