<template>
  <div>
    <el-table
      :data="attributeGroupList"
      style="width: 100%">
      <el-table-column
        label="属性组编号"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px" >{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="属性组名称"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px" >{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="下辖属性"
        width="180">
        <template slot-scope="scope">

          <el-row>
            <el-col v-for="attribute in scope.row.attributeInfos">
              <el-tag
                :key="attribute.id"
                type="">
                {{attribute.name}}
              </el-tag>
            </el-col>
          </el-row>

        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px" >{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px" value-format="yyyy-MM-dd HH:dd:ss">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px" >{{ scope.row.createBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleEdit(scope.$index, scope.row)">删除</el-button>
          <el-button
            size="mini"
            @click="handleDelete(scope.$index, scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      size="mini"
      type="danger"
      @click="queryAttributeList">新增</el-button>
    <el-dialog title="收货地址" :visible.sync="isOpenAddAttributeGroup">
      <el-form :model="attributeGroupReq">
        <el-form-item label="属性组名称" :label-width="formLabelWidth">
          <el-input v-model="attributeGroupReq.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="attributeGroupReq.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="已选属性" :label-width="formLabelWidth">
          <el-tag
            v-for="attribute in attributeGroupReq.selectAttributeList"
            v-model="attributeGroupReq.selectAttributeList"
            :key="attribute.id"
            closable
            :disable-transitions="false"
            @close="handleClose(attribute)">
            {{attribute.name}}
          </el-tag>
        </el-form-item>
      </el-form>
      <br>
      <span>可选属性</span>
      <div>
        <el-checkbox  @change="updateAttribute(attribute)"
                      style="margin-left: 10px"
                      v-for="attribute in attributeList"
                      :key="attribute.id"
                      v-model="attribute.checked"
                      :label="attribute.name" border></el-checkbox>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isOpenAddAttributeGroup = false">取 消</el-button>
        <el-button type="primary" @click="insertAttributeGroup">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {getAttributeGroupList, list, insertAttributeGroup, deleteAttributeById} from "@/api/product/attribute";
import attribute from "@/views/product/attribute/index.vue";

export default {
    name: "attributeGroup",
    //import引入的组件需要注入到对象中才能使用"
    components: {},
    props: {},
    data() {

      //这里存放数据"
      return {
        attributeName:"",
        attributeGroupList: [],
        isOpenAddAttributeGroup: false,
        formLabelWidth: "120px",
        //新增请求对象
        attributeGroupReq: {
          //已选中属性
          selectAttributeList: []
        },
        attributeQueryName: "",
        //未选中
        attributeList: [],
        attr:{}

      };
    },
    //计算属性 类似于data概念",
    computed: {},
    //监控data中的数据变化",
    watch: {},
    //方法集合",
    methods: {
      //修改
      handleDelete(index, row) {
        this.isOpenAddAttributeGroup = true
        console.log(index, row);
        this.attributeGroupReq = row
        this.attributeGroupReq.selectAttributeList = row.attributeInfos
        list(this.attributeQueryName).then(
          res => {
            this.attributeList = res.data
            this.attributeList.forEach(
              attribute => {
                attribute.checked = false
                this.attributeGroupReq.selectAttributeList.forEach(
                  attributeReq =>{
                    if (attribute.id == attributeReq.id){
                      console.log("六六六")
                      attribute.checked = true
                    }
                  }
                )
              }
            )
          }
        )
      },
      //重置属性
      reset() {
        this.attributeGroupReq = {
          selectAttributeList: []
        },
          this.attributeList =[]
      },
      //删除选中属性
      handleClose(attribute){
        this.attributeList = this.attributeList.filter(attr => {
          if (attr.id == attribute.id){
            attr.checked = false
          }
          return attr
        })
        this.attributeGroupReq.selectAttributeList = this.attributeGroupReq.selectAttributeList.filter(item => item!=attribute);
      },
      //更新选中属性
      updateAttribute(attribute) {
        const isExit = this.attributeGroupReq.selectAttributeList.findIndex(item => item.id == attribute.id);
        if (isExit != -1){
          console.log("1")
          this.attributeGroupReq.selectAttributeList = this.attributeGroupReq.selectAttributeList.filter(item => item.id != attribute.id)
          this.attributeList = this.attributeList.filter(attr => {
            if (attr.id == attribute.id){
              console.log(attr.id,attribute.id)
              attr.checked = false
            }
            return attr
          })
        }else{
          console.log("2")
          this.attributeList = this.attributeList.filter(attr => {
            if (attr.id == attribute.id){
              attr.checked = true
            }
            return attr
          })
          this.attributeGroupReq.selectAttributeList.push(attribute)
        }
      },
      //得到属性组列表
      getAttributeGroupList(){
        getAttributeGroupList(this.attributeName).then(
          res => {
            console.log(res);
            this.attributeGroupList = res.data
          }
        )
      },
      handleEdit(index, row) {
        console.log(index, row);
        deleteAttributeById(row.id).then(
          res => {
            console.log(res)
            this.getAttributeGroupList()
          }
        )
      },
      queryAttributeList(){
        this.isOpenAddAttributeGroup = true
        list(this.attributeQueryName).then(
          res => {
            this.attributeList = res.data
          }
        )
      },
      insertAttributeGroup() {
        console.log(this.attributeGroupReq)
        this.attributeGroupReq.attributeGroupId = this.attributeGroupReq.id
        insertAttributeGroup(this.attributeGroupReq).then(
          res => {
            this.reset();
            this.getAttributeGroupList()
            this.isOpenAddAttributeGroup = false
          }
        )
      }
    },
    //生命周期 - 创建完成（可以访问当前this实例）",
    created() {
      this.getAttributeGroupList()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）",
    mounted() {
    },
    beforeCreate() {
    }, //生命周期 - 创建之前",
    beforeMount() {
    }, //生命周期 - 挂载之前",
    beforeUpdate() {
    }, //生命周期 - 更新之前",
    updated() {
    }, //生命周期 - 更新之后",
    beforeDestroy() {
    }, //生命周期 - 销毁之前",
    destroyed() {
    }, //生命周期 - 销毁完成",
    activated() {
    } //如果页面有keep-alive缓存功能，这个函数会触发",
  };
</script>
<style scoped>

</style>
