<!-- 物料组编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :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" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="物料组编码:" prop="code">
             <el-input
               clearable
               :maxlength="20"
               :disabled="isUpdate"
               v-model="form.code"
               placeholder="请输入物料组名称"
             />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料组名称:" prop="name">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.name"
              placeholder="请输入物料组名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类树" prop="categoryLevelRootName">
            <el-input
              v-model="form.categoryLevelRootName"
               @click.native="chooseCategory"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </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" @changeCategory="changeCategory"></CategoryDialog>
  </ele-modal>
</template>

<script>
  import CategoryDialog from './CategoryDialog.vue'
  import { saveItemGroup , updateItemGroup } from '@/api/material/list';

  export default {
    components: {
      CategoryDialog
    },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        name: '',
        code: '',
        categoryLevelRootName: '',
        categoryLevelRootId:''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          code: [
            { required: true, message: '请输入物料组编码', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入物料组名称', trigger: 'blur' }
          ],
          categoryLevelRootName: [
            { required: true, message: '请选择所属分类', trigger: 'change' }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        chooseItem:null
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      chooseCategory(){
          let item = {
             id: this.form.categoryLevelRootId,
             name: this.form.categoryLevelRootName
          }
           this.$refs.categoryRefs.open(item)
      },
      changeCategory(row){
            this.$set(this.form,'categoryLevelRootName',row.name)
            this.$set(this.form,'categoryLevelRootId',row.id)
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          if (!this.isUpdate) {
            delete this.form.id;
          }
          const data = {
            ...this.form
          };
          const saveOrUpdate = this.isUpdate ? updateItemGroup : saveItemGroup;
          saveOrUpdate(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);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data,
              // roleId: this.data.roleList.map((d) => d.id),
              // loginPwd: ''
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
