<template>
  <div class="app-container">
    <div  class="search-form">
      <el-form ref="searchForm" :model="queryParams" inline :label-width="'100px'">
        <el-form-item label="openid" prop="openid">
          <el-input v-model="queryParams.openid" type="text" placeholder="openid" />
        </el-form-item>
        <el-form-item :label="$t('ec.wx.sceneId')" prop="sceneId">
          <el-input v-model="queryParams.sceneId" type="text" :placeholder="$t('ec.wx.sceneId')" />
        </el-form-item>
        <el-form-item :label="$t('ec.wx.jwId')" prop="jwid">
          <el-input v-model="queryParams.jwid" type="text" :placeholder="$t('ec.wx.jwId')" />
        </el-form-item>
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" size="mini" @click="getData">{{ $t('ec.table.query') }}</el-button>
          <el-button type="default" size="mini" @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"
      @page-change="handlePageChange"
      @checkbox-change="({ selection }) => { selections = selection }"
    >
      <template v-slot:toolbar_buttons>
        <el-button v-permission="['weixinQrcodeScanRecord:add']" type="primary" size="mini" @click="add()">{{ $t('ec.table.add') }}</el-button>
        <el-button v-permission="['weixinQrcodeScanRecord:edit']" type="warning" size="mini" :disabled="banState1" @click="edit()">
          {{ $t('ec.table.edit') }}
        </el-button>
        <el-button v-permission="['weixinQrcodeScanRecord:remove']" type="danger" size="mini" :disabled="banState2" @click="remove()">
          {{ $t('ec.table.delete') }}
        </el-button>
<!--        <el-button size="mini" @click="handleReset">{{ $t('ec.table.refresh') }}</el-button>-->
<!--        <el-button size="mini" @click="showSearch = !showSearch">{{ $t('ec.table.search') }}</el-button>-->
        <el-button v-permission="['weixinQrcodeScanRecord:import']" size="mini" @click="importVisible = true">{{ $t('ec.table.import') }}</el-button>
        <el-button v-permission="['weixinQrcodeScanRecord:export']" size="mini">
          <template>{{ $t('ec.table.export') }}</template>
          <template v-slot:dropdowns>
            <el-button type="text" @click="exportExcel('data')">{{ $t('ec.table.exportData') }}</el-button>
            <el-button type="text" @click="exportExcel('template')">{{ $t('ec.table.downloadTemplate') }}</el-button>
          </template>
        </el-button>
      </template>

      <template v-slot:operation="{ row }">
        <el-button v-permission="['weixinQrcodeScanRecord:edit']" type="warning" size="mini" @click="edit(row.id)">{{ $t('ec.table.edit') }}</el-button>
        <el-button v-permission="['weixinQrcodeScanRecord:remove']" type="danger" size="mini" @click="remove(row.id)">
          {{ $t('ec.table.del') }}
        </el-button>
      </template>
    </vxe-grid>

    <el-dialog :title="$t('ec.route.uploadExcel')" :visible.sync="importVisible">
      <el-upload
        class="avatar-uploader"
        :action="importUrl"
        :on-success="() => { importVisible = false }"
        :show-file-list="false"
      >
        <el-input type="text" placeholder="请选择文件" readonly />
      </el-upload>
    </el-dialog>

    <detail ref="weixinQrcodeScanRecordDialog" @refreshDataList="getData" />

  </div>
</template>

<script>
import mixin from 'ecip-web/utils/common-mixin'
import { getData, remove } from 'ecip-web/api/vue/wx/weixinQrcodeScanRecordApi'
import { getToken } from '@/utils/auth'
import Detail from './components/detail'

export default {
  name: 'modules.wx.weixinQrcodeScanRecordView',
  components: { Detail },
  mixins: [...mixin],
  data() {
    return {
      importVisible: false,
      showSearch: false,
      loading: false,
      banState1: true, // 工具栏编辑按钮是否禁用，只有当且仅当选择一条记录的时候才可编辑
      banState2: true, //  工具栏删除按钮是否禁用，当选中条数为0的时候禁用
      importUrl: `api/v1/wx/weixinQrcodeScanRecord/importExcel?token=${getToken()}`,
      tableToolbar: {
        id: 'modules.wx.weixinQrcodeScanRecordView-toolbar',
        custom: true,
        slots: {
          buttons: 'toolbar_buttons'
        },
        zoom: true, // 最大化按钮
        // 列宽操作记录
        resizable: {
          storage: true
        },
        // 列操作记录
        setting: {
          storage: true
        }
      },
      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
      },
      selections: [],
      tableData: [],
      columns: [
        { type: 'checkbox', width: 40, fixed: 'left', align: 'center' },
        { title: 'ID', key: 'id', field: 'id', minWidth: 150, showOverflow: true },
        { title: 'ec.user.realName', key: 'realName', field: 'realName', minWidth: 150, showOverflow: true },
        { title: 'ec.user.userName', key: 'username', field: 'username', minWidth: 150, showOverflow: true },
        { title: 'ec.user.tenant', key: 'tenantName', field: 'tenantName', minWidth: 180, showOverflow: true },
        { title: 'openid', key: 'openid', field: 'openid', minWidth: 200, showOverflow: true },
        { title: 'ec.wx.scanTime', key: 'scanTime', field: 'scanTime', minWidth: 180, showOverflow: true },
        { title: 'ec.wx.sceneId', key: 'sceneId', field: 'sceneId', minWidth: 180, showOverflow: true },
        { title: 'ec.wx.jwId', key: 'jwid', field: 'jwid', minWidth: 150, showOverflow: true },
        { title: 'ec.wx.subscribe', key: 'isScanSubscribe', field: 'isScanSubscribe', minWidth: 80, showOverflow: true }
        // { title: '操作', width: 150, fixed: 'right', align: 'center', slots: { default: 'operation' } }
      ],
      queryParams: {
        openid:'',
        sceneId:'',
        jwid:''
      }
    }
  },
  watch: {
    selections(val) {
      this.banState1 = val.length !== 1
      this.banState2 = val.length === 0
    }
  },
  created() {
    this.getData()
  },
  methods: {
    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
      }).catch(e => {
        this.$message.error(this.$t('ec.message.getError') + `: ${e}`)
      }).finally(() => {
        this.loading = false
      })
    },
    handlePageChange({ currentPage, pageSize }) {
      this.page.currentPage = currentPage
      this.page.pageSize = pageSize
      this.getData()
    },
    add() {
      this.$refs.weixinQrcodeScanRecordDialog.init('add', '')
    },
    edit(id) {
      const dataId = id || this.selections[0].id
      this.$refs.weixinQrcodeScanRecordDialog.init('edit', dataId)
    },
    remove(id) {
      const deleteIds = []
      if (id) {
        deleteIds.push(id)
      } else {
        this.$refs.vxe.getSelectRecords().forEach(row => {
          const id = row.id
          deleteIds.push(id)
        })
      }
      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.$message.success(this.$t('ec.message.delSuccess'))
        })
      })
    },
    exportExcel(type) {
      if (type === 'data') {
        window.open(`api/v1/wx/weixinQrcodeScanRecord/exportExcel?token=${getToken()}`)
      } else if (type === 'template') {
        window.open(`api/v1/wx/weixinQrcodeScanRecord/excelTemplate?token=${getToken()}`)
      }
    },
    handleReset() {
      this.$refs.searchForm.resetFields()
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
