<template>
  <div class="app-container">

    <el-form :model="queryForm" inline size="small">
      <el-form-item label="标题">
        <el-input v-model="queryForm.title" placeholder="标题" />
      </el-form-item>
      <el-form-item label="投诉类型">
        <el-input v-model="queryForm.category" placeholder="投诉类型" />
      </el-form-item>
      <el-form-item label="渠道">
        <el-input v-model="queryForm.channel" placeholder="渠道" />
      </el-form-item>
      <el-form-item label="处理状态">
        <el-select v-model="queryForm.state" clearable placeholder="处理状态">
          <el-option label="待处理" value="PENDING" />
          <el-option label="已处理" value="PROCESSED" />
        </el-select>
      </el-form-item>
      <el-form-item label="投诉时间">
        <el-date-picker
          v-model="queryForm.createdTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="处理时间">
        <el-date-picker
          v-model="queryForm.processedTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <el-row class="action-bar-container" type="flex" justify="end">
      <el-button type="primary" size="small" @click.native="fetchData">查询</el-button>
      <el-button-group>
        <el-button v-permission="['complaint.add']" size="small" @click.native="$refs.dialogForm.open({})">
          新建
        </el-button>
        <el-button v-permission="['complaint.delete']" size="small" @click.native="deleteByIds(multipleSelection)">
          删除
        </el-button>
      </el-button-group>
    </el-row>
    <el-table
      v-loading="isLoading"
      :data="pageData.records"
      border
      fit
      highlight-current-row
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" prop="id" width="120" />
      <el-table-column label="标题" prop="title" />
      <el-table-column label="类型" prop="category" />
      <el-table-column label="渠道" prop="channel" />
      <el-table-column label="内容" prop="content" />
      <el-table-column label="处理结果" prop="state.text" />
      <el-table-column label="投诉时间" prop="createdTime" :formatter="dateTimeFormatter" />
      <el-table-column label="处理时间" prop="processedTime" :formatter="dateTimeFormatter" />
      <el-table-column label="操作" width="70" fixed="right">

        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-permission="['complaint.edit']" type="text" @click.native="$refs.dialogForm.open(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item v-permission="['complaint.process']" @click.native="setProcessedByIds([scope.row.id])"> 设为已处理</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      background
      layout="prev, pager, next"
      :total="pageData.total"
      :page-size="pageData.pageSize"
      @current-change="paginationChange"
    />
    <dialog-form ref="dialogForm" @ok="fetchData" />
  </div>
</template>

<script>
import DialogForm from './DialogForm'
import { findPage, deleteByIds, processedByIds } from '@/api/statistics/complaint'
import { dateTimeFormatter } from '@/utils/formater'

export default {
  name: 'StatisticsComplaint',
  components: { DialogForm },
  data() {
    return {
      queryForm: {},
      isLoading: true,
      pageData: {},
      currentPage: 1,
      multipleSelection: []
    }
  },
  computed: {},
  created() {
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    dateTimeFormatter,
    deleteByIds(ids) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(ids).then(() => {
          this.fetchData()
        })
      })
    },
    setProcessedByIds(ids) {
      this.$confirm('确认要设为已处理吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        processedByIds(ids).then(() => {
          this.fetchData()
        })
      })
    },
    selectionChange(val) {
      const temp = []
      val.forEach(o => {
        temp.push(o.id)
      })
      this.multipleSelection = temp
    },
    paginationChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      this.isLoading = true
      findPage({ ...this.queryForm, current: this.currentPage })
        .then((response) => {
          this.pageData = response
        })
        .finally(() => (this.isLoading = false))
    }
  }
}
</script>
