<template>
  <ai-list class="AppPetitionManage" isTabs>
    <template slot="content">
      <ai-search-bar>
        <template slot="left">
          <el-button type="primary" icon="iconfont iconAdd" @click="id = '', form.name = '', form.tagList = [], isShow = true">添加标签组</el-button>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        ref="aitableex"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="tags" label="标签" align="left">
          <template slot-scope="{ row }">
            <div class="table-tags">
              <el-tag type="info" v-for="(item, index) in row.tagList" size="small" :key="index">{{ item.name }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column slot="options" label="操作" width="180" fixed="right" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <!-- <el-button type="text" @click="edit(row)">添加标签</el-button> -->
              <el-button type="text" @click="edit(row)">编辑</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
        :visible.sync="isShow"
        width="800px"
        title="添加标签组"
        @onConfirm="onConfirm"
        @onCancel="onCancel">
        <el-form class="ai-form" ref="form" label-width="100px" :model="form">
          <el-form-item style="width: 100%" label="标签组名称" prop="name" :rules="[{ required: true, message: '请输入标签组名称', trigger: 'blur' }]">
            <el-input size="small" v-model.trim="form.name" :maxlength="15" show-word-limit placeholder="请输入标签组名称"></el-input>
          </el-form-item>
          <el-form-item style="width: 100%" label="标签" prop="tagList" :rules="[{ required: true, message: '请输入标签组名称', trigger: 'blur' }]">
            <div class="table-tags">
              <el-tag type="info" color="#fff" closable @close="onClose(index)" v-for="(item, index) in form.tagList" :key="index">{{ item.name }}</el-tag>
              <el-input
                v-if="inputVisible"
                v-model.trim="tagName"
                size="small"
                style="width: 100px;"
                maxlength="30"
                clearable
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm">
              </el-input>
              <el-button v-else size="small" icon="iconfont iconAdd" @click="inputVisible = true">添加</el-button>
            </div>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
  export default {
    name: 'Tags',

    props: {
      instance: Function,
      dict: Object
    },

    data () {
      return {
        search: {
          current: 1,
          size: 10
        },
        tagName: '',
        form: {
          name: '',
          tagList: []
        },
        inputVisible: false,
        isShow: false,
        ids: [],
        total: 10,
        colConfigs: [
          { prop: 'name', label: '标签组', align: 'left', width: 160 },
          { slot: 'tags' },
          { slot: 'options', label: '操作', align: 'center' }
        ],
        id: '',
        tableData: [],
      }
    },

    mounted () {
      this.getList()
    },

    methods: {
      getList() {
        this.instance.post(`/app/wxcp/wxcorptag/listAll`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      onCancel () {
        this.form.name = ''
        this.form.tagList = []
        this.id = ''
        this.inputVisible = ''
        this.isShow = false
      },

      edit (e) {
        this.id = e.id
        this.form = JSON.parse(JSON.stringify(e))

        this.$nextTick(() => {
          this.isShow = true
        })
      },

      handleInputConfirm () {
        if (!this.tagName) {
          return
        }

        this.form.tagList.push({
          name: this.tagName
        })

        this.$nextTick(() => {
          this.tagName = ''
          this.inputVisible = false
        })
      },

      onClose (index) {
        this.form.tagList.splice(index, 1)
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(this.id ? '/app/wxcp/wxcorptag/update' : `/app/wxcp/wxcorptag/add`, {
              name: this.form.name,
              id: this.id || '',
              tagList:  this.form.tagList
            }).then(res => {
              if (res.code === 0) {
                this.getList()
                this.isShow = false
                this.$message.success(`${this.id}` ? '编辑成功' : '提交成功')
              }
            })
          }
        })
      },

      remove (id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/wxcp/wxcorptag/delete?id=${id}&type=0`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .table-tags {
    .el-tag {
      margin-right: 8px;
      border: 1px solid #D0D4DC;
      background: #F3F4F7;
      border-radius: 4px;
      font-size: 13px;
      color: #222222;

      &:last-child {
        margin-right: 0;
      }
    }
  }
</style>
