<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1200px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <div style="display: flex">
          <div>{{ __('可用短信条数：') }}{{ userSmsNum }}</div>
          <div style="margin-left: 20px">{{ __('已用条数：') }}{{ total }}</div>
        </div>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="sms_record_id"
        show-overflow-tooltip
        width="100px"
      />
      <el-table-column
        align="center"
        :label="__('会员编号')"
        prop="user_id"
        show-overflow-tooltip
        width="100px"
      />
      <el-table-column
        align="center"
        :label="__('接收号码')"
        prop="sms_mobile"
        show-overflow-tooltip
        width="120px"
      />
      <el-table-column
        align="center"
        :label="__('短信内容')"
        prop="sms_content"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('发送日期')"
        prop="sms_date"
        show-overflow-tooltip
        width="160px"
      />
      <el-table-column
        align="center"
        :formatter="formatTimeTwo"
        :label="__('发送时间')"
        prop="sms_time"
        show-overflow-tooltip
        width="160px"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.rows"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getSmsRecord } from '@/api/base/config'
  import {renderTime, timeToYMD} from "@/utils/date";

  export default {
    name: 'SmsRecord',
    data() {
      return {
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        userSmsNum: 0,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          page: 1,
          rows: 10,
        },
      }
    },
    created() {
      this.title = this.__('消息记录列表')
    },
    methods: {
      __,
      showTable() {
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.queryForm = {
          page: 1,
          rows: 10,
        }
        this.dialogFormVisible = false
      },
      formatTime(row) {
        const sms_date = row.sms_date
        return sms_date ? timeToYMD(sms_date) : ''
      },
      formatTimeTwo(row) {
        const sms_time = row.sms_time
        return sms_time ? renderTime(sms_time) : ''
      },
      handleSizeChange(val) {
        this.queryForm.rows = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getSmsRecord(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.userSmsNum = data.user_sms_num
        this.listLoading = false
      },
    },
  }
</script>
