<template>
  <div class="app-container">
    <div v-show="showSearch" class="search-form">
      <el-form ref="searchForm" :model="queryParams" inline :label-width="'100px'">
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" size="mini" @click="getData">查询</el-button>
          <el-button type="default" size="mini" @click="handleReset">重置</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="['weixinGzuser:add']" type="primary" size="mini" @click="add()">新增</el-button>
        <el-button v-permission="['weixinGzuser:edit']" type="warning" size="mini" :disabled="banState1" @click="edit()">编辑</el-button>
        <el-button v-permission="['weixinGzuser:remove']" type="danger" size="mini" :disabled="banState2" @click="remove()">删除</el-button>
        <el-button size="mini" @click="handleReset">刷新</el-button>
        <el-button size="mini" @click="showSearch = !showSearch">搜索</el-button>
        <el-button v-permission="['weixinGzuser:import']" size="mini" @click="importVisible = true">导入</el-button>
        <el-button v-permission="['weixinGzuser:export']" type="success" @click="exportExcel">导出</el-button>
      </template>

      <template v-slot:operation="{ row }">
        <el-button v-permission="['weixinGzuser:edit']" type="warning" size="mini" @click="edit(row.id)">编辑</el-button>
        <el-button v-permission="['weixinGzuser:remove']" type="danger" size="mini" @click="remove(row.id)">删除</el-button>
      </template>
    </vxe-grid>

    <ec-import
      :upload-url="importUrl"
      :tpl-url="tplUrl"
      title="导入微信公众号用户数据"
      :visible="importVisible"
      :upload-params="{ isNewPk: false, strategy: 'ignore' }"
      @onSuccess="getData"
      @close="importVisible = false"
    >
      <template v-slot:extra_params="{ extraParams }">
        <el-form-item label="是否生成新主键">
          <ec-radio-group v-model="extraParams.isNewPk" :ec-data="[ {value: false, label: '否'}, {value: true, label: '是' } ]" />
        </el-form-item>
        <el-form-item label="导入策略">
          <ec-radio-group v-model="extraParams.strategy" :ec-data="[ {value: 'ignore', label: '忽略'}, {value: 'overwrite', label: '覆盖' } ]" />
        </el-form-item>
      </template>
    </ec-import>

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

  </div>
</template>

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

export default {
  name: 'modules.wx.weixinGzuserView',
  components: { Detail },
  mixins: [...mixin],
  data() {
    return {
      importVisible: false,
      showSearch: false,
      loading: false,
      banState1: true, // 工具栏编辑按钮是否禁用，只有当且仅当选择一条记录的时候才可编辑
      banState2: true, //  工具栏删除按钮是否禁用，当选中条数为0的时候禁用
      importUrl: `api/v1/wx/weixinGzuser/importExcel`,
      tplUrl: 'api/v1/wx/weixinGzuser/excelTemplate',
      exportUrl: 'api/v1/wx/weixinGzuser/exportExcel',
      tableToolbar: {
        id: 'modules.wx.weixinGzuserView-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: '序号', key: 'id', field: 'id', minWidth: 100, showOverflow: true },
        { title: 'openid', key: 'openid', field: 'openid', minWidth: 100, showOverflow: true },
        { title: '昵称', key: 'nickname', field: 'nickname', minWidth: 100, showOverflow: true },
        { title: '过滤后昵称', key: 'nicknameTxt', field: 'nicknameTxt', minWidth: 100, showOverflow: true },
        { title: '备注名称', key: 'bzname', field: 'bzname', minWidth: 100, showOverflow: true },
        { title: '用户头像', key: 'headimgurl', field: 'headimgurl', minWidth: 100, showOverflow: true },
        { title: '性别', key: 'sex', field: 'sex', minWidth: 100, showOverflow: true },
        { title: '是否关注', key: 'subscribe', field: 'subscribe', minWidth: 100, showOverflow: true },
        { title: '关注时间', key: 'subscribeTime', field: 'subscribeTime', minWidth: 100, showOverflow: true },
        { title: '用户关注渠道来源', key: 'subscribeScene', field: 'subscribeScene', minWidth: 100, showOverflow: true },
        { title: '手机号', key: 'mobile', field: 'mobile', minWidth: 100, showOverflow: true },
        { title: '绑定状态', key: 'bindStatus', field: 'bindStatus', minWidth: 100, showOverflow: true },
        { title: '绑定时间', key: 'bindTime', field: 'bindTime', minWidth: 100, showOverflow: true },
        { title: '标签id', key: 'tagidList', field: 'tagidList', minWidth: 100, showOverflow: true },
        { title: '省份', key: 'province', field: 'province', minWidth: 100, showOverflow: true },
        { title: '城市', key: 'city', field: 'city', minWidth: 100, showOverflow: true },
        { title: '地区', key: 'country', field: 'country', minWidth: 100, showOverflow: true },
        { title: '二维码扫码场景', key: 'qrScene', field: 'qrScene', minWidth: 100, showOverflow: true },
        { title: '二维码扫码常见描述', key: 'qrSceneStr', field: 'qrSceneStr', minWidth: 100, showOverflow: true },
        { title: '用户所在分组id', key: 'groupid', field: 'groupid', minWidth: 100, showOverflow: true },
        { title: '用户的语言，简体中文为zh_CN', key: 'language', field: 'language', minWidth: 100, showOverflow: true },
        { title: 'UNIONID', key: 'unionid', field: 'unionid', minWidth: 100, showOverflow: true },
        { title: '公众号原始id', key: 'jwid', field: 'jwid', minWidth: 100, showOverflow: true },
        { title: '创建时间', key: 'createTime', field: 'createTime', minWidth: 100, showOverflow: true },
        { title: '操作', width: 150, fixed: 'right', align: 'center', slots: { default: 'operation' }}
      ],
      queryParams: {}
    }
  },
  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(`获取数据失败: ${e}`)
      }).finally(() => {
        this.loading = false
      })
    },
    handlePageChange({ currentPage, pageSize }) {
      this.page.currentPage = currentPage
      this.page.pageSize = pageSize
      this.getData()
    },
    add() {
      this.$refs.weixinGzuserDialog.init('add', '')
    },
    edit(id) {
      const dataId = id || this.selections[0].id
      this.$refs.weixinGzuserDialog.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(`确定删除所选项吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(deleteIds).then(res => {
          this.getData()
          this.$message.success('删除成功')
        }).catch(e => {
          this.$message.error(`删除失败: ${e}`)
        })
      })
    },
    async exportExcel() {
      this.loading = true
      await download({ url: this.exportUrl, method: 'post', data: this.queryParams })
      this.loading = false
    },
    handleReset() {
      this.$refs.searchForm.resetFields()
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
