<template>
  <div class="app-container">
    <div class="search-form">
      <el-form ref="searchForm" :model="queryParams" inline :label-width="'100px'">
        <el-form-item :label="$t('ec.sms.phone')" prop="phone">
          <el-input v-model="queryParams.phone" type="text" :placeholder="$t('ec.sms.phone')" />
        </el-form-item>
        <el-form-item :label="$t('ec.template.code')" prop="sendCode">
          <el-input v-model="queryParams.sendCode" type="text" :placeholder="$t('ec.template.code')" />
        </el-form-item>
        <el-form-item :label="$t('ec.sms.status')" prop="status">
          <el-select v-model="queryParams.status">
            <el-option
              v-for="dict in $dictUtils.getDictList('sendStatus')"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
              style="padding-left: 10px;"
            >{{ dict.label }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" @click="handleSearch">{{ $t('ec.table.query') }}</el-button>
          <el-button type="default" @click="handleReset">{{ $t('ec.table.reset') }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <vxe-grid
      ref="vxe"
      highlight-hover-row
      border
      resizable
      sync-resize
      auto-resize
      class="vxe-table-element"
      :loading="loading"
      :start-index="(page.currentPage - 1) * page.pageSize"
      :pager-config="page"
      :columns="columns"
      :data="tableData"
      :toolbar="tableToolbar"
      @toolbar-button-click="toolbarButtonClickEvent"
      @page-change="handlePageChange"
    >
      <template v-slot:status="{row}">
        <span>{{ $dictUtils.getDictLabel('sendStatus', row.status, row.status) }}</span>
      </template>
      <template v-slot:operation="{row}">
        <div>
          <el-button size="mini" type="danger" @click="remove(row.id)">{{ $t('ec.table.del') }}</el-button>
        </div>
      </template>
    </vxe-grid>

  </div>
</template>

<script>
import mixin from 'ecip-web/utils/common-mixin'
import { getData, remove } from 'ecip-web/api/vue/sms/smsSendLogApi'

export default {
  name: 'ModulesSmsSmsSendLogView',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      tableToolbar: {
        id: 'modules.sms.smsSendLogView-toolbar',
        // buttons: [
        //   { code: 'remove', name: this.$t('ec.table.delete'), visible: this.checkPermission(['smsSendLog:remove']) },
        //   { code: 'search', name: this.$t('ec.table.search') },
        //   { code: 'reset', name: this.$t('ec.table.reset') }
        // ],
        slots: {
        },
        export: true, // 导出按钮
        zoom: true, // 最大化按钮
        // 列宽操作记录
        resizable: {
          storage: true
        },
        // 列操作记录
        setting: {
          storage: true
        }
      },
      showSearch: false,
      loading: false,
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        align: 'left',
        pageSizes: [10, 20, 50, 100, 200, 500],
        layouts: ['Sizes', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'FullJump', 'Total'],
        perfect: true
      },
      tableData: [],
      columns: [
        { type: 'seq', width: 60, fixed: 'left', align: 'center' },
        { title: 'ec.sms.phone', key: 'phone', field: 'phone', minWidth: 100, showOverflow: true },
        { title: 'ec.template.name', key: 'templateName', field: 'templateName', minWidth: 100, showOverflow: true },
        { title: 'ec.template.code', key: 'sendCode', field: 'sendCode', minWidth: 100, showOverflow: true },
        { title: 'ec.sms.status', key: 'status', field: 'status', minWidth: 100, showOverflow: true, slots: { default: 'status' }},
        { title: 'ec.sms.smsId', key: 'smsid', field: 'smsid', minWidth: 100, showOverflow: true },
        { title: 'ec.sms.msg', key: 'msg', field: 'msg', minWidth: 100, showOverflow: true },
        { title: 'ec.sms.responseDate', key: 'responseDate', field: 'responseDate', minWidth: 100, showOverflow: true }
        /* { title: 'ec.table.actions', width: 150, fixed: 'right', align: 'center', slots: { default: 'operation' }}*/
      ],
      queryParams: {
        phone:'',
        sendCode:'',
        status:''
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    toolbarButtonClickEvent({ code }, event) {
      switch (code) {
        case 'remove':
          this.remove()
          break
        case 'search':
          this.showSearch = !this.showSearch
          break
        case 'reset':
          this.handleSearch()
          break
      }
    },
    getData() {
      this.loading = true
      getData(Object.assign({}, { size: this.page.pageSize, current: this.page.currentPage }),
        this.queryParams).then(res => {
        const data = res.data
        this.tableData = data.records
        this.page.total = data.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    handlePageChange({ currentPage, pageSize }) {
      this.page.currentPage = currentPage
      this.page.pageSize = pageSize
      this.getData()
    },
    add() {
      this.$router.push(`/smsSendLogAdd`)
    },
    edit(id) {
      this.$router.push(`/smsSendLogEdit/${id}`)
    },
    remove(id) {
      const deleteIds = []
      if (id) {
        deleteIds.push(id)
      } else {
        this.$refs.vxe.getSelectRecords().forEach(row => {
          const id = row.id
          deleteIds.push(id)
        })
      }
      if (deleteIds.length > 0) {
        this.$confirm(this.$t('ec.message.delConfirm'), this.$t('ec.message.prompt'), {
          confirmButtonText: this.$t('ec.message.confirm'),
          cancelButtonText: this.$t('ec.message.cancel'),
          type: 'warning'
        }).then(() => {
          remove(deleteIds).then(res => {
            this.getData()
            this.$notify.success(this.$t('ec.message.delSuccess'))
          })
        }
        )
      } else {
        this.$message({
          message: this.$t('ec.message.selectEmpty'),
          type: 'warning'
        })
      }
    },
    handleSearch() {
      this.page.currentPage = 1
      this.getData()
    },
    handleReset() {
      this.$refs['searchForm'].resetFields()
      this.handleSearch()
    }
  }
}
</script>

<style scoped>

</style>
