<template>
  <div class="classify">
    <div class="classify-list">
      <div class="classify-content">
        <div class="list" v-for="(v, i) in datas.keyList" :key="'0' + i">
          <div class="list-box">
            <div class="list-value">
              <p>关键词: {{v.key.join(' ') || '--'}}</p>
              <p>附加词: {{v.appendkey.join('、') || '--'}}</p>
              <p>排除词: {{v.notkey.join('、') || '--'}}</p>
            </div>
            <div class="list-icon" @click="deleteKeyFn(i)"></div>
            <div class="list-edit" @click="editKeyFn(v, i)">
              编辑
              <i class="tri-down"></i>
            </div>
          </div>
        </div>
        <div class="words-add" @click="addKeyFn()">
          <i class="el-icon-plus"></i>
        </div>
      </div>
    </div>
    <!-- 关键词dialog -->
    <el-dialog
      custom-class="sub-dialog"
      :visible.sync="dialog.editKey"
      :close-on-click-modal="false"
      :show-close="false"
      center
      width="800px"
      v-if="dialog.editKey"
    >
      <KeyCard @onCancel="dialog.editKey = false" @onConfirm="submitKeywords">
        <div slot="header">修改关键词</div>
        <div class="key-edit-content">
          <ScopeEdit ref="keyEditRef" :datas="props"></ScopeEdit>
        </div>
      </KeyCard>
    </el-dialog>
  </div>
</template>
<script>
import { Tooltip, Dialog, Input, Button } from 'element-ui'
import KeyCard from '@/components/selector/SelectorCard'
import ScopeEdit from './Edit'
export default {
  name: 'scope-list',
  components: {
    [Tooltip.name]: Tooltip,
    [Dialog.name]: Dialog,
    [Input.name]: Input,
    [Button.name]: Button,
    KeyCard,
    ScopeEdit
  },
  props: {
    datas: {
      keyList: Array,
      maxCount: Number
    }
  },
  data () {
    return {
      dialog: {
        editKey: false // 修改关键词弹框
      },
      // 传给dialog子组件的数据
      props: {
        ways: '', // 编辑还是新增
        keyIndex: null, // 关键词下标
        key: [], // 关键词
        notkey: [], // 排除词
        appendkey: [] // 附加词
      }
    }
  },
  computed: {},
  mounted () {},
  methods: {
    // 添加关键词
    addKeyFn () {
      const isCan = this.getIsAdd()
      if (isCan) {
        return this.$message({
          type: 'warning',
          message: '关键词超出最大限制'
        })
      }
      this.dialog.editKey = true
      this.props.ways = 'add'
      this.props.key = []
      this.props.notkey = []
      this.props.appendkey = []
    },
    // 删除单个关键词
    deleteKeyFn (i) {
      const data = this.datas.keyList
      if (!data) return
      data.splice(i, 1)
      this.$emit('updateKey', data)
    },
    // 编辑单个关键词
    editKeyFn (v, i) {
      this.props.ways = 'edit'
      this.dialog.editKey = true
      // 把当前关键词传到dialog key,notkey,appendKey都为数组
      this.props.key = v.key
      this.props.notkey = v.notkey
      this.props.appendkey = v.appendkey
      this.props.keyIndex = i
    },
    // 清空传值
    clearPropsData () {
      this.props.key = []
      this.props.notkey = []
      this.props.appendkey = []
    },
    // 修改关键词dialog 保存数据
    submitKeywords () {
      const data = this.datas.keyList
      const refs = this.$refs.keyEditRef.cur
      const keyArr = this.getKeyTotalArray()
      const type = this.props.ways
      const obj = {
        key: refs.key.split(' '),
        notkey: refs.notkey,
        appendkey: refs.appendkey
      }
      if (refs.key === '') {
        return this.$message({
          type: 'warning',
          message: '关键词不能为空'
        })
      }
      // 判断附加词是否重复
      if (this.getArrIsRepeat(refs.appendkey)) {
        return this.$message({
          type: 'warning',
          message: '设置的附加词重复，请调整后再添加'
        })
      }
      // 判断排除词是否重复
      if (this.getArrIsRepeat(refs.notkey)) {
        return this.$message({
          type: 'warning',
          message: '设置的排除词重复，请调整后再添加'
        })
      }
      if (type === 'add') {
        if (keyArr.indexOf(refs.key) > -1) {
          return this.$message({
            type: 'warning',
            message: '关键词不能重复'
          })
        }
        data.push(obj)
      } else if (type === 'edit') {
        // 如果当前编辑的关键词和修改框里的关键词不相同 需校验是否重复
        if (data[this.props.keyIndex].key.toString() !== refs.key) {
          if (keyArr.indexOf(refs.key) > -1) {
            return this.$message({
              type: 'warning',
              message: '关键词不能重复'
            })
          }
        }
        data[this.props.keyIndex].key = refs.key.split(' ')
        data[this.props.keyIndex].notkey = refs.notkey
        data[this.props.keyIndex].appendkey = refs.appendkey
      }
      // 通知父组件发请求修改并更新
      this.$emit('updateKey', data)
      this.dialog.editKey = false
    },
    // 获取所有关键词的key的属性，并返回一个数组(主要用于判断添加关键词是否重复)
    getKeyTotalArray () {
      const data = this.datas.keyList
      const keysArr = []
      data.forEach((s) => {
        if (s && s.key && Array.isArray(s.key)) {
          s.key.forEach((v) => {
            keysArr.push(v)
          })
        }
      })
      console.log(keysArr, 'keysArr')
      return keysArr
    },
    // 判断附加词、排除词是否重复
    getArrIsRepeat (arr) {
      this.removeEmptyInput(arr)
      return (new Set(arr)).size !== arr.length
    },
    // 判断是否超限
    getIsAdd () {
      const keyArr = this.getKeyTotalArray()
      if (keyArr.length >= this.datas.maxCount) {
        return true
      } else {
        return false
      }
    },
    // 输入框为空移除
    removeEmptyInput (arr) {
      for (let i = 0; i < arr.length; i++) {
        // eslint-disable-next-line
        if (arr[i] === '' || arr[i] === null || typeof (arr[i]) === undefined) {
          arr.splice(i, 1)
          i = i - 1
        }
      }
      return arr
    }
  }
}
</script>
<style lang="scss" scoped>
// dialog 自定义class样式
.sub-dialog{
  border-radius: 8px;
  .selector-card{
    width: 800px;
    height: auto;
  }
  .key-edit-content,.class-edit-content{
    width: 740px;
    padding: 30px 0;
    height: auto;
    max-height: 340px;
    margin: 0 auto;
    border: 1px solid #ececec;
    overflow-y: scroll;
    box-sizing: border-box;
  }
  .item{
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
  }
  .item-label{
    margin-right: 8px;
    min-width: 120px;
    height: 40px;
    color: #1d1d1d;
    font-size: 14px;
    line-height: 40px;
    text-align: right;
  }
  .item-label-required:before{
    content: "*";
    color: #f56c6c;
    margin-right: 2px;
  }
  .item-value{
    width: 352px;
  }
  .custom-long-input{
    width: 352px;
  }
  .custom-short-input{
    width: 170px;
  }
  .delete-class{
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 32px 32px 42px;
    background: #FFFFFF;
    border-radius: 6px;
    .delete-class-header{
      font-size: 18px;
      line-height: 28px;
      color: #1D1D1D;
    }
    .delete-class-content{
      padding: 20px 0 42px;
      color: #686868;
      font-size: 14px;
      line-height: 22px;
    }
    .delete-class-footer{
      display: flex;
      align-items: center;
      .confirm,
      .cancel {
        padding: 10px 50px;
        margin: 0 20px;
      }
      ::v-deep {
        .el-button--primary {
          color: #2cb7ca;
          background: none;
          border-color: #2cb7ca;
          &:hover {
            color: #fff;
            background-color: #2cb7ca;
          }
        }
        .el-button--default {
          &:hover,
          &:active {
            color: #2cb7ca;
            border-color: #2cb7ca;
            background: #eaf8fa;
          }
        }
      }
    }
  }
}
.classify-list{
  margin-bottom: 30px;
  .icon-edit,.icon-delete{
    display: inline-block;
    width: 16px;
    height: 16px;
    background-repeat: no-repeat;
    background-position: center center;
    cursor: pointer;
  }
  .icon-edit{
    margin: 0 10px;
    background-image: url('~@/assets/images/icon-edit.png');
    background-size: contain;
  }
  .icon-delete{
    background-image: url('~@/assets/images/icon-delete.png');
    background-size: contain;
  }
  .classify-content{
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    .list{
      padding-top: 10px;
      margin-right: 10px;
      margin-bottom: 10px;
      width: 160px;
      height: 80px;
      box-sizing: border-box;
      &:hover .list-edit{
        transform: scaleY(1);
        transition: transform .1s;
      }
      &:hover .list-box{
        border: 1px solid #2cb7ca;
        box-sizing: border-box;
      }
    }
    .list:nth-child(6n) {
      margin-right: 0;
    }
    .list-box{
      position: relative;
      display: flex;
      padding: 10px 10px 10px 16px;
      border: 1px solid #ececec;
      border-radius: 9px;
      box-sizing: border-box;
      cursor: pointer;
    }
    .list-edit{
      transform: scaleY(0);
      transition: transform .1s;
      position: absolute;
      top: -40px;
      left: 50%;
      margin-left: -22px;
      padding: 10px;
      color: #fff;
      background: #1d1d1d;
      border-radius: 4px;
      font-size: 12px;
      cursor: pointer;
      .tri-down{
        position: absolute;
        bottom: -6px;
        left: 50%;
        margin-left: -5px;
        width: 0;
        height: 0;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-top: 6px solid #1d1d1d;
      }
    }
    .list-value{
      width: 124px;
      font-size: 12px;
      line-height: 20px;
      color: #000;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      p{
        @extend .list-value
      }
    }
    .list-icon{
      @extend .icon-delete;
      width: 12px;
      height: 12px;
    }
    .words-add{
      width: 162px;
      height: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      border: 1px solid #ececec;
      margin: 10px 0 0 0;
      border-radius: 9px;
      color: #c4c4c4;
      cursor: pointer;
    }
  }
}
</style>
