<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.template.name')" prop="name">
          <el-input v-model="queryParams.name" type="text" :placeholder="$t('ec.template.name')" />
        </el-form-item>
        <el-form-item :label="$t('ec.template.code')" prop="code">
          <el-input v-model="queryParams.code" type="text" :placeholder="$t('ec.template.code')" />
        </el-form-item>
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" @click="toolbarButtonClickEvent({'code':'reset'})">{{ $t('ec.table.search') }}</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:operation="{ row }">
        <div>
          <el-button size="mini" type="primary" @click="edit(row.id)">{{ $t('ec.table.edit') }}</el-button>
          <el-button size="mini" type="danger" @click="remove(row.id)">{{ $t('ec.table.del') }}</el-button>
        </div>
      </template>

      <template v-slot:role_toolbar_btns>
        <el-button v-permission="['emailTemplate:add']" type="primary" @click="add">{{ $t('ec.table.add') }}</el-button>
        <el-button v-permission="['emailTemplate:remove']" type="danger" @click="remove()">
          {{ $t('ec.table.delete') }}
        </el-button>
<!--        <el-button @click="toolbarButtonClickEvent({'code':'reset'})">{{ $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/email/emailTemplateApi'

export default {
  name: 'ModulesEmailEmailTemplateView',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      tableToolbar: {
        id: 'modules.email.emailTemplateView-toolbar',
        // buttons: [
        //   { code: 'add', name: this.$t('ec.table.add'), visible: this.checkPermission(['emailTemplate:add']) },
        //   { code: 'remove', name: this.$t('ec.table.delete'), visible: this.checkPermission(['emailTemplate:remove']) },
        //   { code: 'search', name: this.$t('ec.table.search') },
        //   { code: 'reset', name: this.$t('ec.table.refresh') }
        // ],
        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: 'ec.template.name', key: 'name', field: 'name', minWidth: 100, showOverflow: true },
        { title: 'ec.template.code', key: 'code', field: 'code', minWidth: 100, showOverflow: true },
        { title: 'ec.template.subject', key: 'templateSubject', field: 'templateSubject', minWidth: 100, showOverflow: true },
        { title: 'ec.table.updateDate', key: 'updateDate', field: 'updateDate', minWidth: 100, showOverflow: true },
        { title: 'ec.table.actions', 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.page.currentPage =1;
          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(`/emailTemplateAdd`)
    },
    edit(id) {
      this.$router.push(`/emailTemplateEdit/${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.getData()
    },
    handleReset() {
      this.$refs['searchForm'].resetFields()
      this.toolbarButtonClickEvent({'code': 'reset'})
    }
  }
}
</script>

<style scoped>

</style>
