<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="标题搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="{row, $index}">
          <span>{{ (listQuery.page - 1) * listQuery.limit + $index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="200px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送方式" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag>{{ row.send_mode_display }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="通知类型" width="120px" align="center">
        <template slot-scope="{row}">
          <el-tag type="info">{{ row.notification_type_display }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="接收人数" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.recipient_count }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleViewRecipients(row)">
            查看详情
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="80%">
       <el-table :data="recipientList" border fit highlight-current-row style="width: 100%;" v-loading="recipientLoading">
          <el-table-column property="student_name" label="姓名" width="120"></el-table-column>
          <el-table-column property="student_phone" label="电话" width="120"></el-table-column>
          <el-table-column property="teaching_center" label="教学中心" width="150"></el-table-column>
          <el-table-column property="project_info" label="关联项目/优惠券" min-width="150"></el-table-column>
          <el-table-column property="status_info" label="状态" width="100"></el-table-column>
          <el-table-column label="是否已读" width="100" align="center">
              <template slot-scope="{row}">
                  <el-tag :type="row.is_read ? 'success' : 'info'">{{ row.is_read ? '已读' : '未读' }}</el-tag>
              </template>
          </el-table-column>
       </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNotificationBatches, getBatchRecipients, deleteNotificationBatch } from '@/api/crm'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'

export default {
  name: 'NotificationHistory',
  components: { Pagination },
  filters: {
    parseTime
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        search: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        view: '发送详情'
      },
      recipientList: [],
      recipientLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getNotificationBatches(this.listQuery).then(response => {
        this.list = response.data.results
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleViewRecipients(row) {
        this.dialogStatus = 'view'
        this.dialogFormVisible = true
        this.recipientLoading = true
        getBatchRecipients(row.id).then(response => {
            this.recipientList = response.data
            this.recipientLoading = false
        })
    },
    handleDelete(row) {
      this.$confirm('确认删除该条通知记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteNotificationBatch(row.id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    }
  }
}
</script>
