<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="700px"
    @close="close"
  >
    <el-button type="primary" size="small" @click="addAtt">
      新增配置项
    </el-button>
    <div class="item-line" v-for="(item, index) in attribute" :key="index">
      <div class="item-title">
        <i
          class="el-icon-remove"
          style="color: red; margin-top: 26px; font-size: 23px"
          size="26"
          @click="delItem(index)"
        ></i>
        配置项{{ parseInt(index) + 1 }}
      </div>
      <el-row type="flex" class="row-bg">
        <el-col :span="12" class="item-content">
          <el-input
            v-model.trim="item.attributeName"
            size="small"
            placeholder="配置参数"
            autocomplete="off"
          ></el-input>
        </el-col>
        <el-col :span="12" class="item-content">
          <el-input
            v-model.trim="item.attributeValue"
            size="small"
            placeholder="配置值"
            autocomplete="off"
          ></el-input>
        </el-col>
        <el-col :span="12" class="item-content">
          <el-input
            v-model.trim="item.attributeNameEn"
            size="small"
            placeholder="configuration parameter"
            autocomplete="off"
          ></el-input>
        </el-col>
        <el-col :span="12" class="item-content">
          <el-input
            v-model.trim="item.attributeValueEn"
            size="small"
            placeholder="Configuration value"
            autocomplete="off"
          ></el-input>
        </el-col>
      </el-row>
      <div class="item-sort">
        <i
          class="el-icon-sort-up sort"
          @click="sortData(index, -1)"
          atl="上移"
        ></i>
        <i
          class="el-icon-sort-down sort"
          @click="sortData(index, 1)"
          alt="上移"
        ></i>
      </div>
    </div>

    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
      <el-button size="small" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addData, updateData, addAttribute } from '@/api/product';
export default {
  name: 'GroupEdit',
  data() {
    return {
      form: {
        id: '',
        name: '',
      },
      attribute: [],
      categoryId: '',
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入产品名称' }],
        materialCode: [
          { required: true, trigger: 'blur', message: '请输入物料编码' },
        ],
      },
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    addAtt() {
      this.attribute.push({
        attributeName: '',
        attributeValue: '',
        attributeNameEn: '',
        attributeValueEn: '',
      });
    },
    showEdit(row, categoryId) {
      this.categoryId = categoryId;
      this.title = '配置参数';
      this.attribute = Object.assign([], row);
      this.dialogFormVisible = true;
    },
    delItem(index) {
      this.$confirm('你确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.attribute.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    sortData(index, type) {
      let index1, index2;
      if (
        (index == 0 && type == -1) ||
        (index == this.attribute.length - 1 && type == 1)
      ) {
        return;
      }
      if (type == -1) {
        index1 = index;
        index2 = index - 1;
      } else {
        index1 = index + 1;
        index2 = index;
      }
      [this.attribute[index1], this.attribute[index2]] = [
        this.attribute[index2],
        this.attribute[index1],
      ];
    },
    close() {
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      console.log(this.attribute);
      let attribute = this.attribute.map((item, index) => {
        return {
          ...item,
          orderNum: index + 1,
        };
      });
      addAttribute({ categoryId: this.categoryId, attribute }).then(res => {
        this.$notify({
          title: '操作成功',
          type: 'success',
        });
        this.dialogFormVisible = false;
      });
    },
  },
};
</script>
<style lang="scss">
.el-dialog {
  .el-dialog__header {
    border-bottom: 1px solid #e3e3e3 !important;
    padding: 15px 20px;
  }
  .dialog-footer {
    text-align: right;
    .el button {
      height: 36px !important;
      min-height: 36px !important;
      line-height: 366px !important;
    }
  }
}
.item-line {
  width: 100%;
  display: flex;
  .item-title {
    width: 140px;
    display: flex;
    line-height: 76px;
  }
}
.item-content {
  padding: 5px;
}
.item-sort {
  width: 50px;
  display: flex;
  .sort {
    font-size: 18px;
    font-weight: bolder;
    margin-top: 33px;
    &:hover {
      color: #409eff;
    }
  }
}
</style>
