<template>
  <div class="payment-channel-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-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>
          <el-button
            :disabled="row.payment_channel_buildin === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('编号')"
        prop="payment_channel_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('代码名称')"
        prop="payment_channel_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('账户名称')"
        prop="payment_channel_name"
        show-overflow-tooltip
      />

      <el-table-column
        v-if="false"
        align="center"
        :label="__('支付接口配置信息')"
        prop="payment_channel_config"
        show-overflow-tooltip
        width="200"
      />

      <el-table-column
        align="center"
        :formatter="formatWechat"
        :label="__('微信启动')"
        prop="payment_channel_wechat"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatChannelEnable"
        :label="__('是否启用')"
        prop="payment_channel_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatId"
        :label="__('支付方式')"
        prop="payment_type_id"
        show-overflow-tooltip
        width="260"
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="payment_channel_order"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuidin"
        :label="__('系统内置')"
        prop="payment_channel_buildin"
        show-overflow-tooltip
      />
    </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" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete } from '@/api/payment/channel'
  import Edit from './components/PaymentChannelEdit'

  export default {
    name: 'PaymentChannel',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatBuidin(row) {
        return row.payment_channel_buildin === 1
          ? this.__('系统内置')
          : this.__('线下账户')
      },
      formatWechat(row) {
        return row.payment_channel_wechat === 1
          ? this.__('启用')
          : this.__('禁用')
      },
      formatChannelEnable(row) {
        return row.payment_channel_enable === 1
          ? this.__('启用')
          : this.__('禁用')
      },
      formatId(row) {
        return row.payment_type_id === 1302
          ? this.__('在线支付【站内支付余额充值卡白条】')
          : this.__('线下支付')
      },
      handleEdit(row) {
        if (row.payment_channel_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            payment_channel_id: row.payment_channel_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      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>
