<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="批次：">
        <el-input v-model="form.no" placeholder="请输入批次" clearable />
      </el-form-item>
      <el-form-item label="服务时长：">
        <el-select v-model="form.serviceTime" placeholder="请选择服务时长" clearable>
          <el-option v-for="item in serviceTimeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名：">
        <el-input v-model="form.userName" placeholder="请输入用户姓名" clearable />
      </el-form-item>
      <el-form-item label="手机号：">
        <el-input v-model="form.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item label="激活状态：">
        <el-select v-model="form.activeStatus" placeholder="请选择激活状态" clearable>
          <el-option v-for="item in activeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <Upload action="/qqService/importData" @upload="getUpload" style="margin: 0 10px"
          v-hasPermi="['service:platform:push']">批量推送短信</Upload>
        <el-button type="danger" @click="getDialog()" v-hasPermi="['service:platform:over']">批量过期</el-button>
        <el-button type="text" @click="getTemplate">模板下载</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="批次" prop="no" align="center" show-overflow-tooltip />
      <el-table-column label="服务时长" prop="serviceTime" align="center" :formatter="timeFormatter" />
      <el-table-column label="Key" prop="key" align="center" show-overflow-tooltip />
      <el-table-column label="发送时间" prop="sendTime" align="center" show-overflow-tooltip />
      <el-table-column label="当前状态" prop="codeStatus" align="center" :formatter="statusFormatter" />
      <el-table-column label="是否激活" prop="activeStatus" align="center" :formatter="activeStatusFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog(scope.row)">查看</el-button>
          <el-button v-if="scope.row.codeStatus === 2" type="text">重新发送</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title">
      <!-- 批量发送短信 批量过期 -->
      <el-form v-if="title === '批量推送短信'">
        <el-form-item label="服务时长：" label-width="100px" style="width: 300px">
          <el-select v-model="dialogForm.serviceTime" placeholder="请选择服务时长">
            <el-option v-for="item in serviceTimeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-table :data="dialogData" ref="dialogData" :row-key="rowKey" @selection-change="selectionChange"
            height="497">
            <el-table-column type="selection" align="center" />
            <el-table-column label="购买套餐" prop="name" align="center" />
            <el-table-column label="收货地址" prop="receiveAddress" align="center" />
            <el-table-column label="用户姓名" prop="userName" align="center" />
            <el-table-column label="用户手机号" prop="phone" align="center" />
          </el-table>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
          <el-button v-if="title === '批量推送短信'" type="primary" @click="getSend">发送</el-button>
          <el-button v-else type="primary">确定</el-button>
        </el-form-item>
      </el-form>

      <!-- 批量过期 -->
      <div v-else-if="title === '批量过期'">
        <el-table :data="dialogData">
          <el-table-column label="批次" prop="no" align="center" show-overflow-tooltip />
          <el-table-column label="批次状态" prop="status" align="center" :formatter="batchFormatter" />
          <el-table-column label="批次人数" prop="num" align="center" />
          <el-table-column label="成功" prop="successNum" align="center" />
          <el-table-column label="失败" prop="failNum" align="center" />
          <el-table-column label="添加日期" prop="createTime" align="center" show-overflow-tooltip />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="delete" @click="getPast(scope.row)" :disabled="scope.row.status === 1">强制过期</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="batchTotal > 0" :total="batchTotal" :page.sync="batchForm.pageNum"
          :limit.sync="batchForm.pageSize" @pagination="getBatchPage" />
      </div>

      <!-- 查看详情 -->
      <div v-else>
        <el-descriptions title="基础信息" :column="1">
          <el-descriptions-item label="收货地址">{{ dialogDetail.receiveAddress }}</el-descriptions-item>
          <el-descriptions-item label="用户姓名">{{ dialogDetail.userName }}</el-descriptions-item>
          <el-descriptions-item label="用户手机号">{{ dialogDetail.phone }}</el-descriptions-item>
          <el-descriptions-item label="购买套餐">{{ dialogDetail.name }}</el-descriptions-item>
        </el-descriptions>
        <el-descriptions title="服务信息" :column="1">
          <el-descriptions-item label="批次">{{ dialogDetail.batchNo }}</el-descriptions-item>
          <el-descriptions-item label="Key">{{ dialogDetail.key }}</el-descriptions-item>
          <el-descriptions-item label="发送时间">{{ dialogDetail.sendTime }}</el-descriptions-item>
          <el-descriptions-item label="激活账号">{{ dialogDetail.activeAccount }}</el-descriptions-item>
        </el-descriptions>
        <el-button type="danger" @click="getPast(dialogDetail)" v-hasPermi="['service:qqmusic:over']">
          强制过期
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { page, send, importData, detail, past, batchList, batchPage } from '@/api/service/platform'
import { serviceTimeMixin } from '@/mixin/index'
export default {
  mixins: [serviceTimeMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      total: 0,
      // 弹窗
      dialogVisible: false,
      title: '',
      dialogData: [],
      // 弹窗详情
      dialogDetail: {},
      // 发送表单
      dialogForm: {
        serviceTime: ''
      },
      // 批次分页
      batchForm: {
        pageNum: 1,
        pageSize: 10
      },
      batchTotal: 0,
      // 批次状态
      batchStatusOptions: [{
        value: 0,
        label: '有效'
      }, {
        value: 1,
        label: '已被强制过期'
      }, {
        value: 2,
        label: '删除'
      }],
      // batchOptions: [], （3.0.6版本舍弃）
      // 当前状态
      statusOptions: [
        { value: 0, label: '已发送' },
        { value: 1, label: '未发送' },
        { value: 2, label: '发送失败' }
      ],
      activeOptions: [
        { value: 0, label: '未激活' },
        { value: 1, label: '已激活' }
      ]
    }
  },
  mounted() {
    // this.getBatchList() （3.0.6版本舍弃）
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      page(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.form.pageSize = 10
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 下载模板
    getTemplate() {
      this.download('qqService/importTemplate', {
      }, `推送短信模板.xlsx`)
    },

    // 批量推送短信
    getUpload(e) {
      importData({
        batchId: e.file
      }).then(res => {
        if (res.code === 0) {
          this.dialogVisible = true
          this.title = '批量推送短信'
          this.dialogData = res.data
          this.$nextTick(() => {
            this.$refs.dialogData.toggleAllSelection()
          })
        }
      })
    },

    rowKey(row) {
      return row.id
    },

    // 多选 
    selectionChange(e) {
      this.dialogForm.ids = []
      e.map(i => {
        this.dialogForm.ids.push(i.id)
      })
    },

    // 弹窗
    getDialog(row) {
      this.dialogVisible = true
      if (row) {
        this.title = row.name
        detail({
          id: row.id
        }).then(res => {
          if (res.code === 0) {
            this.dialogDetail = res.data
          }
        })
      } else {
        this.title = '批量过期'
        this.getBatchPage()
      }
    },

    // 批次分页
    getBatchPage() {
      batchPage(this.batchForm).then(res => {
        if (res.code === 0) {
          this.dialogData = res.data.records
          this.batchTotal = res.data.total
        }
      })
    },

    // 发送
    getSend() {
      if (this.dialogForm.serviceTime !== '') {
        send(this.dialogForm).then(res => {
          if (res.code === 0) {
            this.$message.success('发送成功！')
            this.dialogVisible = false
            this.getList()
          }
        })
      } else {
        this.$message.error('请选择服务时长！')
      }
    },

    // 强制过期
    getPast(row) {
      this.$confirm('确定强制过期吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        past({
          id: row.id,
          status: 1
        }).then(() => {
          this.$message.success('操作成功！')
          this.getBatchPage()
        })
      }).catch(() => {
        this.$message.info('已取消！')
      })
    },

    // 取消
    cancel() {
      this.dialogVisible = false
    },

    // 字典翻译
    statusFormatter(row) {
      return row.codeStatus === null ? '/' : this.selectDictLabel(this.statusOptions, row.codeStatus)
    },
    timeFormatter(row) {
      return row.serviceTime === null ? '/' : this.selectDictLabel(this.serviceTimeOptions, row.serviceTime)
    },
    batchFormatter(row) {
      return this.selectDictLabel(this.batchStatusOptions, row.status)
    },
    nameFormatter(row) {
      return row.name === null ? '/' : row.name
    },
    userNameFormatter(row) {
      return row.userName === null ? '/' : row.userName
    },
    phoneFormatter(row) {
      return row.phone === null ? '/' : row.phone
    },
    activeStatusFormatter(row) {
      return this.selectDictLabel(this.activeOptions, row.activeStatus)
    }
  }
}
</script>