<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="label_name" placeholder="请输入标签名" class="filter-item"/>
      <el-select v-model="label_class" clearable placeholder="请选择标签分组" class="filter-item" @clear="clearLabelClass" >
        <el-option label="全部" value="0"></el-option>
        <el-option label="系统标签" value="1"></el-option>
        <el-option label="基础标签" value="2"></el-option>
        <el-option label="活动标签" value="3"></el-option>
      </el-select>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button v-permission="'HY_add'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add">添加</el-button>
      <el-button v-permission="'HY_edit'" type="primary" class="filter-item" @click="btn_edit">编辑</el-button>
    </div>
    <div v-for="item in titleDataLists" :key="item.title" class="content">
      <div class="content-title">{{ item.title }}</div>
      <div>
        <el-tag
          v-for="tag in item.labelData"
          :key="tag.F_TagName"
          size="medium"
          closable
          :color="selectedTagName==tag.F_TagName?'rgba(0, 246, 255, .6)':''"
          @close="btn_delete(tag)"
          @click="clickTag(tag)"
          class="tag-item">
          {{ tag.F_TagName }}
        </el-tag>
      </div>
    </div>
  </div>
</template>

<script>

import { getCommodityLabel, deleteCommodityLabel } from '@/api/commodityManagement/commodityLabel'
import addOrEdit from './components/addOrEdit'

export default {
  name: 'CommodityLabel',
  data() {
    return {
      loading: false,
      label_name: '',//标签名
      label_class: '0',//标签分组
      dataLists: [], // 列表数据
      selectedTagName: '', //选中标签名称
      selectedData: {}, //选中标签数据
      // allLabelData: [], //全部标签数据
      // systemLabelData: [], //系统标签数据
      // basisLabelData: [], //基础标签数据
      // activityLabelData: [], //活动标签数据
      titleDataLists: [{
        title: '全部标签',
        labelData: [],
      },{
        title: '系统标签',
        labelData: [],
      },{
        title: '基础标签',
        labelData: [],
      },{
        title: '活动标签',
        labelData: [],
      }]
    }
  },
  created() {
    this.getList()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const data = {
          TagName: this.label_name, //标签名称
          CLass: this.label_class, //标签分组
        }
        getCommodityLabel(data).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            let allLabelData = [] //全部标签数据
            let systemLabelData = [] //系统标签数据
            let basisLabelData = [] //基础标签数据
            let activityLabelData = [] //活动标签数据
            this.dataLists = response.data
            allLabelData = response.data
            for (let i = 0; i < allLabelData.length; i++) {
              if (allLabelData[i].F_CLass === 1) {
                systemLabelData.push(allLabelData[i])
              } else if (allLabelData[i].F_CLass === 2) {
                basisLabelData.push(allLabelData[i])
              } else if (allLabelData[i].F_CLass === 3) {
                activityLabelData.push(allLabelData[i])
              }
            }
            for (let i = 0; i < this.titleDataLists.length; i++) {
              if (this.titleDataLists[i].title === '全部标签') {
                this.titleDataLists[i].labelData = allLabelData
              } else if (this.titleDataLists[i].title === '系统标签') {
                this.titleDataLists[i].labelData = systemLabelData
              } else if (this.titleDataLists[i].title === '基础标签') {
                this.titleDataLists[i].labelData = basisLabelData
              } else if (this.titleDataLists[i].title === '活动标签') {
                this.titleDataLists[i].labelData = activityLabelData
              } 
            }
          }
          this.selectedData.F_TagName = ""
        })
        resolve()
      })
    },
    btn_search() {
      this.getList()
    },
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': '' }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['80%', '90%'],
        title: '添加标签'
      })
    },
    btn_edit() {
      if (!this.selectedData.F_TagName) {
        this.$message.warning('请选中一个标签!')
        return
      }
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowData': this.selectedData }// props
        },
        area: ['80%', '90%'],
        title: '编辑标签'
      })
    },
    btn_delete(tag) {
      this.$confirm('您确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          Type: 1,
          TagName: tag.F_TagName,
          CLass: tag.F_CLass,
        }
        deleteCommodityLabel(data).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    clickTag(tag) {
      this.selectedTagName = tag.F_TagName
      this.selectedData = tag
    },
    clearLabelClass() {
      this.label_class = '0'
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.tag-item {
  margin-right: 8px;
}
.filter-container .filter-item.el-select{
  margin-right: 10px;
}
.content {
  margin: 15px 0 0 0;
  .content-title {
    margin: 0 0 5px 0;
  }
}
</style>
