<template>
  <div class="sync-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="同步类型：">
            <el-select
              v-model="filter.messageType"
              placeholder="短信类型"
              clearable
            >
              <el-option
                v-for="(val, key) in SMS_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发送结果：">
            <el-select
              v-model="filter.sendStatus"
              placeholder="发送结果"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="(val, key) in SEND_STATUS"
                :key="key"
                :value="key * 1"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input
              v-model.trim="filter.mobileNumber"
              placeholder="手机号模糊查询"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="businessOperate" label="短信类型" width="200">
        </el-table-column>
        <el-table-column prop="mobileNumber" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="userName" label="用户" min-width="120">
        </el-table-column>
        <el-table-column prop="templateCode" label="模板代号" width="200">
        </el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号" width="160">
        </el-table-column>
        <el-table-column prop="courseCode" label="课程代码" width="140">
        </el-table-column>
        <el-table-column prop="sendStatus" label="结果" width="60">
          <template slot-scope="scope">
            <span v-if="scope.row.sendStatus === 'OK'">成功</span>
            <el-popover
              v-else
              placement="top"
              width="300"
              trigger="hover"
              :content="scope.row.sendResult"
            >
              <span class="color-danger" slot="reference">失败</span>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发送时间" width="170">
          <span slot-scope="scope">
            {{ scope.row.createTime | timestampFilter }}
          </span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="
                checkPrivilege('link', 'send') && scope.row.sendStatus !== 'OK'
              "
              type="text"
              class="btn-primary"
              :disabled="loading"
              @click="toResend(scope.row)"
              >重新发送</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { smsListQuery, smsTypes, resendSms } from "../api";

export default {
  name: "sync-manage",
  data() {
    return {
      SEND_STATUS: {
        0: "失败",
        1: "成功",
      },
      filter: {
        sendStatus: "",
        messageType: "",
        mobileNumber: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      SMS_TYPE: {},
      loading: false,
    };
  },
  created() {
    this.getSmsTypes();
    this.getList();
  },
  methods: {
    async getSmsTypes() {
      const data = await smsTypes();
      let types = {};
      data.forEach((item) => {
        types[item.name] = item.desc;
      });
      this.SMS_TYPE = types;
    },
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      // data.sendStatus
      const data = await smsListQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toResend(row) {
      if (this.loading) return;

      this.loading = true;
      const res = await resendSms(row.id).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.$message.success("发送成功！");
      this.getList();
    },
  },
};
</script>
