<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <manage-tab @change="handleChange" />

      <div v-if="listQuery.type === 5">
        <el-tabs v-model="listQuery.allStatus" @tab-click="handleChange(listQuery.type)">
          <el-tab-pane label="企业话术" name="1" />
          <el-tab-pane label="个人话术" name="2" />
        </el-tabs>
      </div>

      <div class="filter-control">
        <span>标题:</span>
        <el-input
          v-model="listQuery.title"
          placeholder="标题"
          clearable
          @keyup.enter.native="handleChange(listQuery.type)"
          @clear="handleChange(listQuery.type)"
        />
      </div>
      <div v-if="listQuery.allStatus !== '2'" class="filter-control">
        <span>用户阶段:</span>
        <el-select v-model="listQuery.stageStatus" placeholder="用户阶段" @change="handleChange(listQuery.type)">
          <el-option
            v-for="(i, index) in userClass"
            :key="index"
            :label="i.value"
            :value="i.id"
          />
        </el-select>
      </div>
      <div v-if="listQuery.allStatus !== '2'" class="filter-control">
        <span>标签:</span>
        <data-select :is-edit="false" :is-multiple="true" @handle-emit="handleSelect" />
      </div>
      <div v-if="listQuery.allStatus === '2'" class="filter-control">
        <span>上传者:</span>
        <el-select v-model="listQuery.spId" placeholder="上传者" @change="handleChange(listQuery.type)">
          <el-option
            v-for="item in sellerList"
            :key="item.serviceProviderId"
            :label="item.linkMan"
            :value="item.serviceProviderId"
          />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="handleChange(listQuery.type)">查询</el-button>
        <el-button v-if="listQuery.allStatus !== '2'" type="primary" @click="$router.push('/database/store-list/data-manage/edit?id=' + id + '&t=' + listQuery.type)">添加</el-button>
        <el-button v-if="listQuery.allStatus !== '2'" type="primary" @click="exportDialogVisible = true">删除</el-button>
      </div>
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="list"
      border
    >
      <el-table-column
        label="ID"
        prop="id"
        align="center"
        width="80"
      />
      <el-table-column prop="title" label="标题" align="center" />
      <el-table-column prop="stageStatus" label="用户阶段" align="center">
        <template slot-scope="scope">
          <div>
            {{ scope.row.stageStatus | userStateFilter(userClass) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="labelIds" label="标签" align="center">
        <template slot-scope="scope">
          <div>
            {{ scope.row.keywords }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="pv" label="浏览量" align="center" />
      <el-table-column prop="addTime" label="添加时间" align="center" />
      <el-table-column prop="createBy" label="创建人" align="center" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="{ row }">
          <div class="labelCenter">
            <el-button
              v-if="listQuery.allStatus === '1'"
              type="primary"
              size="mini"
              @click="$router.push('/database/store-list/data-manage/edit?id=' + id + '&t=' + listQuery.type + '&tId=' + row.id)"
            >编辑</el-button>
            <el-button
              v-if="listQuery.allStatus === '1'"
              type="primary"
              size="mini"
              @click="
                handleDelete({ ids: row.id, productArchiveId: id, type: listQuery.type.toString() })
              "
            >删除</el-button>
            <el-button
              v-if="listQuery.type === 5 && listQuery.allStatus === '2'"
              type="primary"
              size="mini"
              @click="
                $router.push('/database/store-list/data-manage/edit?id=' + row.id)
              "
            >查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="handleChange(listQuery.type)"
    />

    <el-dialog title="选择商品" :visible.sync="exportDialogVisible" width="70%">
      <data-list-selector
        v-if="exportDialogVisible"
        :type-id="listQuery.type"
        @cancel="onSelectorCancel"
        @confirm="onSelectorConfirm"
      />
    </el-dialog>
  </div>
</template>

<script>
import ManageTab from '../../components/dataManageTab.vue'
import DataListSelector from '../../components/dialogTypeTable.vue'
import databaseMixin from '../../mixins/index'
import { getContextList, deleteContext } from '@/api/contentLibrary/context'
import dataSelect from '../../components/data-select.vue'

export default {
  components: {
    ManageTab,
    DataListSelector,
    dataSelect
  },
  mixins: [databaseMixin],
  data() {
    return {
      listQuery: {
        type: '',
        pageNum: 1,
        pageSize: 10,
        labelIds: '',
        stageStatus: '',
        allStatus: '1',
        spId: ''
      },
      list: [],
      total: 0,
      isLoading: false,
      exportDialogVisible: false
    }
  },
  watch: {
    'listQuery.allStatus': {
      handler(val) {
        if (val) {
          this.listQuery.labelIds = ''
          this.listQuery.spId = ''
        }
      }
    }
  },
  mounted() {
    this.getServiceList()
  },
  methods: {
    async getInfo() {
      this.isLoading = true
      const { data } = await getContextList(Object.assign(this.listQuery, { productArchiveId: this.id, productId: this.pId || '' }))
      this.list = data.results
      this.total = data.totalRecord
      this.isLoading = false
    },
    handleSelect($event) {
      this.listQuery.labelIds = $event.join(',')
      this.handleChange(this.listQuery.type)
    },
    handleChange($event) {
      if (this.id && $event) {
        this.listQuery.type = Number($event)
        this.isLoading = true
        this.getInfo()
      }
    },
    handleDelete(val) {
      this.$confirm(`是否删除该资料?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const form = new FormData()
          form.append('ids', val.ids)
          form.append('type', val.type)
          form.append('productArchiveId', val.productArchiveId)
          await deleteContext(form)
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.handleChange(this.listQuery.type)
        } catch (error) {
          console.error(error)
        }
      })
    },
    onSelectorCancel() {
      this.exportDialogVisible = false
    },
    onSelectorConfirm(row) {
      console.log(row)
      this.handleDelete({ ids: row.map(e => e.id).join(','), type: this.listQuery.type.toString(), productArchiveId: this.id })
      this.exportDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tabs__nav-wrap::after {
  background: none;
}
</style>
