<template>
  <th-table
    class="sms-right-table-container"
    v-bind="list"
    @handleSelectionChange="handleSelectionChange"
    @handleSizeChange="handleSizeChange"
    @handleCurrentChange="handleCurrentChange"
  />
</template>

<script>
import ThTable from '../../table/src/main'
import baseTable from '../../../src/mixins/baseTable'
import { serialize } from '../../../src/utils/util'

const columns = [
  {
    prop: 'PHONENUMBER',
    label: '手机号',
    minWidth: '100',
    align: 'center'
  },
  {
    prop: 'SENDSTATUS',
    label: '发送状态',
    minWidth: '80',
    formatter: (row, column, cellValue, index) => {
      switch (cellValue) {
        case '0':
          return '待发送'
        case '1':
          return '已发送'
        default:
          return ''
      }
    },
    align: 'center'
  },
  {
    prop: 'SHEETTYPE',
    label: '工单类型',
    minWidth: '100',
    align: 'center'
  },
  {
    prop: 'SENDOBJECTTYPE',
    label: '发送对象类型',
    minWidth: '100',
    align: 'center'
  },
  {
    prop: 'SMS_TYPE',
    label: '发送设备',
    minWidth: '80',
    formatter: (row, column, cellValue, index) => {
      switch (cellValue) {
        case 0:
          return '短信猫'
        case 1:
          return '短信平台'
        case 2:
          return '95598短信平台'
        default:
          return ''
      }
    },
    align: 'center'
  }
]
export default {
  components: {
    ThTable
  },
  mixins: [baseTable],
  props: {
    smsSendObjOptions: {
      type: Array,
      default: () => []
    },
    smsSheetTypeOptions: {
      type: Array,
      default: () => []
    },
    request: {
      type: Object,
      required: true
    }
  },
  data() {
    columns[2].formatter = this.formatSheetType
    columns[3].formatter = this.formatSendObjType
    return {
      list: {
        columns,
        params: {},
        handleColumn: []
      }
    }
  },
  methods: {
    formatSheetType(row, column, cellValue, index) {
      if (Array.isArray(this.smsSheetTypeOptions)) {
        const item = this.smsSheetTypeOptions.find(d => d.value === cellValue)
        if (item) {
          return item.label
        }
      }
    },
    formatSendObjType(row, column, cellValue, index) {
      if (Array.isArray(this.smsSendObjOptions)) {
        const item = this.smsSendObjOptions.find(d => d.value === cellValue)
        if (item) {
          return item.label
        }
      }
    },
    // 接口请求
    fetchTableApi(groupId) {
      this.list.loading = true
      const params = {}
      params.params = JSON.stringify({
        filter: serialize({ groupId }),
        pageIndex: this.list.pageNo,
        pageSize: this.list.pageSize
      })
      this.request.fetchSmsRecordList && this.request.fetchSmsRecordList(params).then(res => {
        this.hideTableLoading()
        if (res.data.successful === true) {
          this.list.data = res.data.resultValue.items
          this.list.totalCount = res.data.resultValue.itemCount
        }
      }).catch(this.hideTableLoading)
    },
    // 清空数据
    clearTable() {
      this.list.data = []
      this.list.totalCount = 0
      this.list.pageNo = 1
    }
  }
}
</script>
