<!--
属性设置
@Author: linqian
@Date: 2021-05-25 11:22
-->
<template>
  <div>
    <dg-card class="box-card" shadow="never" header="属性详情">
      <div style="height: calc(100vh - 15.5rem)">
        <dg-scrollbar>
          <el-form
            ref="ruleForm"
            :rules="rules"
            :model="form"
            label-width="100px"
            :style="{ width: type == 'SUB' ? '40%' : '90%' }"
          >
            <el-form-item label="属性类别">
              <el-input v-model="rootName" placeholder="请输入属性类别" disabled></el-input>
            </el-form-item>
            <el-form-item label="属性类别编码">
              <el-input v-model="rootCode" placeholder="请输入属性类别编码" disabled></el-input>
            </el-form-item>
            <el-form-item label="属性名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入属性名称" :disabled="readOnly"></el-input>
            </el-form-item>
            <el-form-item label="属性编码" prop="code" class="attr-code">
              <template v-if="parentNode.pid && !parentNode.child">
                <el-input v-model="attrCode" placeholder="请输入属性编码" :disabled="readOnly">
                  <template slot="prepend">{{ rootCode }}-</template>
                </el-input>
              </template>
              <template v-else>
                <el-input v-model="form.code" placeholder="请输入属性编码" :disabled="readOnly">
                  <template slot="prepend">{{ rootCode }}-</template>
                </el-input>
              </template>
              <!-- <dg-row>
                <dg-col :span="5"><el-input value="APP-RA" :disabled="true"></el-input></dg-col>
                <dg-col :span="2" style="text-align: center"><i class="el-icon-minus"></i></dg-col>
                <dg-col :span="17">
                  <el-input v-model="form.code" placeholder="请输入属性编码" :disabled="readOnly"></el-input>
                </dg-col>
              </dg-row> -->
            </el-form-item>
            <el-form-item label="是否内置" v-if="operateType !== 'add'">
              <el-input :value="form.builtIn == 1 ? '是' : '否'" disabled></el-input>
            </el-form-item>
            <template v-for="(item, index) in form.child">
              <dg-row :key="index">
                <dg-col :span="12">
                  <el-form-item
                    :label="index == 0 ? '属性值名称' : ''"
                    :prop="'child.' + index + '.name'"
                    class="prop-value-item"
                    :rules="{
                      required: true,
                      message: '属性值名称不能为空',
                      trigger: 'blur'
                    }"
                  >
                    <el-input v-model="item.name" placeholder="请输入属性值名称" :disabled="readOnly"></el-input>
                  </el-form-item>
                </dg-col>
                <dg-col :span="12">
                  <el-form-item
                    :label="index == 0 ? '属性值编码' : ''"
                    :prop="'child.' + index + '.code'"
                    class="prop-value-item"
                    :rules="{
                      required: true,
                      message: '属性值编码不能为空',
                      trigger: 'blur'
                    }"
                  >
                    <div class="subject-property__form-item">
                      <el-input v-model="item.code" placeholder="请输入属性值编码" :disabled="readOnly"></el-input>
                      <i
                        class="el-icon-remove-outline icon"
                        @click="handleDeletePropValue(index)"
                        v-if="form.child.length > 1 && !readOnly"
                      ></i>
                    </div>
                  </el-form-item>
                </dg-col>
              </dg-row>
            </template>
            <el-form-item label="">
              <dg-button
                v-if="!readOnly"
                :icon="'添加属性值' | optIcon"
                size="mini"
                type="primary"
                plain
                @click="handleAddPropValue"
                >添加属性值</dg-button
              >
            </el-form-item>
            <!-- <el-form-item label="说明" v-if="!readOnly">
              <el-input v-model="form.remark" type="textarea" row="3" placeholder="请输入说明"></el-input>
            </el-form-item> -->
            <el-form-item v-if="!readOnly" label="" style="text-align: center"
              ><dg-button type="primary" @click="handleSaveProp" :icon="'保存' | optIcon">保存</dg-button></el-form-item
            >
          </el-form>
        </dg-scrollbar>
      </div>
    </dg-card>
  </div>
</template>

<script>
import { saveProp } from '@/api/data-auth';
import _ from 'lodash';
export default {
  props: {
    parentNode: Object,
    type: String,
    operateType: String
  },
  components: {},
  data() {
    return {
      form: {
        attrSubType: '',
        child: [
          {
            name: '',
            code: ''
          }
        ],
        remark: '',
        name: '',
        code: '',
        pid: '',
        builtIn: this.parentNode.builtIn,
        ptype: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '属性名称不能为空', trigger: 'change' }],
        code: [{ required: true, message: '属性编码不能为空', trigger: 'change' }]
      },
      rootName: '',
      rootCode: '',
      attrCode: ''
    };
  },
  computed: {
    readOnly() {
      const { pid, child, builtIn, ptype } = this.parentNode;
      if (pid && !child) {
        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        const index = ptype.indexOf('-');
        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        this.attrCode = ptype.substring(index + 1);
      }
      return pid && (!child || builtIn == 1);
    }
  },
  methods: {
    /**
     * 添加属性值
     */
    handleAddPropValue() {
      this.form.child.push({
        name: '',
        code: ''
      });
    },
    /**
     * 删除属性值
     */
    handleDeletePropValue(index) {
      this.form.child.splice(index, 1);
    },
    /**
     * 保存属性
     */
    handleSaveProp() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 属性值不能重复
          // let flag = false;
          // this.form.child.sort((a, b) => {
          //   if (a.name == b.name) {
          //     flag = true;
          //     return;
          //   }
          // });
          // if (flag) {
          //   this.$message.warning('属性值不能重复');
          //   return;
          // }
          const { child, ...otherParams } = this.form;
          const params = {
            ...otherParams,
            child: child.map((item) => {
              const { id, code, name } = item;
              return {
                id,
                code,
                name
              };
            })
          };
          saveProp(params).then((res) => {
            const { result, msg } = res.data;
            if (result == '200') {
              this.$message.success('保存成功');
              this.$emit('savePropSuccess');
            } else {
              this.$message.error(msg);
            }
          });
        }
      });
    }
  },
  created() {
    const { attrSubType, type, id, code, name } = this.parentNode;

    if (this.parentNode.pname) {
      const pName = this.parentNode.pname.split('-');
      const pType = this.parentNode.ptype.split('-');
      delete this.parentNode.operateType;
      this.form = _.cloneDeep(this.parentNode);
      if (pName.length > 1) {
        this.form.name = pName[1];
        this.form.child = [
          {
            ...this.parentNode
          }
        ];
      }
      this.rootName = pName[0];
      this.rootCode = pType[0];
    } else {
      this.rootName = this.parentNode.name;
      this.rootCode = this.parentNode.code;
      this.form.attrSubType = attrSubType;
      this.form.pid = id;
      this.form.ptype = type;
    }
  },
  mounted() {}
};
</script>

<style lang='scss' scoped>
/deep/.el-input-group__prepend {
  padding: 0 12px;
}
</style>