<template>
  <div class="wechat-tplmsg-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-upload" type="primary" @click="showMpInitTmp">
          {{ __('初始化模板消息') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="fetchData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('模版编号')"
        prop="tplmsg_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('模版编号')"
        prop="tplmsg_tpl_id"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :label="__('模版标题')"
        prop="tplmsg_title"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatId"
        :label="__('模版类型')"
        prop="tplmsg_type_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('模版库编号')"
        prop="tplmsg_number"
        show-overflow-tooltip
        width="150"
      />

      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="tplmsg_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuidin"
        :label="__('系统内置')"
        prop="tplmsg_is_buildin"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('备注')"
        prop="tplmsg_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="tplmsg_sort"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商城编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('消息模板编号')"
        prop="message_id"
        show-overflow-tooltip
        width="150"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <el-dialog
      :append-to-body="true"
      :before-close="handleClose"
      height="20%"
      title="系统提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div>
        <p>请确认更新数据？</p>
        <br />
        <el-button type="primary" @click="initTmp">确认</el-button>
        <el-button type="primary" @click="handleClose">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Edit from './components/WechatTplmsgEdit'
  import { getList, initMpTmp } from '@/api/wechat/tplmsg'

  export default {
    name: 'WechatTplmsg',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        dialogVisible: false,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatId(row) {
        return row.tplmsg_type_id === 1
          ? this.__('订单提醒')
          : row.tplmsg_type_id === 2
          ? this.__('支付提醒')
          : this.__('发货提醒')
      },
      formatBuidin(row) {
        return row.tplmsg_is_buildin === 1 ? this.__('是') : this.__('否')
      },
      formatEnable(row) {
        return row.tplmsg_enable === 1 ? this.__('启用') : this.__('停用')
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      showMpInitTmp() {
        this.dialogVisible = true
      },
      async initTmp() {
        const { msg, status } = await initMpTmp(this.form)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
