<template>
  <div class="app-container">
    <div  class="search-form">
      <el-form ref="searchForm" :model="queryParams" inline :label-width="'100px'">
        <el-form-item label="模版名称" prop="name">
          <el-input v-model="queryParams.name" type="text" placeholder="模版名称" />
        </el-form-item>
        <el-form-item label="模版编码" prop="code">
          <el-input v-model="queryParams.code" type="text" placeholder="模版编码" />
        </el-form-item>
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="default" @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"
      @toolbar-button-click="toolbarButtonClickEvent"
      @page-change="handlePageChange"

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

      <template v-slot:role_toolbar_btns>
        <el-button v-permission="['weixinTemplate:add']" type="primary" @click="add">{{ $t('ec.table.add') }}</el-button>
        <el-button v-permission="['weixinTemplate:remove']" type="danger"  @click="remove()">
          {{ $t('ec.table.delete') }}
        </el-button>
<!--        <el-button @click="refresh">{{ $t('ec.table.refresh') }}</el-button>-->
      </template>
    </vxe-grid>

  </div>
</template>

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

export default {
  name: 'ModulesWxTemplateView',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      selectedRole: {},
      tableToolbar: {
        id: 'modules.wx.wxTemplateView-toolbar',
        // buttons: [
        //   { code: 'add', name: '新增', visible: this.checkPermission(['weixinTemplate:add']) },
        //   { code: 'remove', name: '删除', visible: this.checkPermission(['weixinTemplate:remove']) },
        //   { code: 'search', name: '搜索' },
        //   { code: 'reset', name: '刷新' }
        // ],
        slots: {
          buttons: 'role_toolbar_btns'
        },
        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: 'checkbox', width: 40, fixed: 'left', align: 'center' },
        { title: '模版名称', key: 'name', field: 'name', minWidth: 100, showOverflow: true },
        { title: '模版编码', key: 'code', field: 'code', minWidth: 100, showOverflow: true },
        { title: '模板标识', key: 'templateContent', field: 'templateContent', minWidth: 100, showOverflow: true },
        { title: '备注信息', key: 'remarks', field: 'remarks', minWidth: 100, showOverflow: true },
        { title: '更新时间', key: 'updateDate', field: 'updateDate', minWidth: 100, showOverflow: true },
        { title: '操作', width: 150, fixed: 'right', align: 'center', slots: { default: 'operation' }}
      ],
      queryParams: {
        name:'',
        code:''
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {

    toolbarButtonClickEvent({ code }, event) {
      switch (code) {
        case 'add':
          this.add()
          break
        case 'remove':
          this.remove()
          break
        case 'search':
          this.showSearch = !this.showSearch
          break
        case 'reset':
          this.selectedRole = {}
          this.getData()
          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(`/wxTemplateAdd`)
    },
    edit(id) {
      this.$router.push(`/wxTemplateEdit/${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(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(deleteIds).then(res => {
            this.getData()
            this.$notify.success('删除成功')
          })
        }
        )
      } else {
        this.$message({
          message: '请至少选择一条数据',
          type: 'warning'
        })
      }
    },
    handleSearch() {
      this.page.currentPage = 1
      this.getData()
    },
    handleReset() {
      this.$refs['searchForm'].resetFields()
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
