<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item label="名称">
        <el-input
          v-model="form.keyWords"
          placeholder="请输入名称">
        </el-input>
      </el-form-item>
      <el-form-item
        label="支付类型"
        prop="payType">
        <el-select v-model="form.payType">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            label="无需支付"
            value="NO_PAY"></el-option>
          <el-option
            label="B扫C支付"
            value="B_SCAN_C_PAY"></el-option>
          <el-option
            label="C扫B支付"
            value="C_SCAN_B_PAY"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status">
        <el-select v-model="form.status">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            label="激活"
            :value="1"></el-option>
          <el-option
            label="禁用"
            :value="0"></el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item
        label="类型"
        prop="type">
        <el-select v-model="form.type">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="(value,key) in nameMap"
            :key="key"
            :label="value"
            :value="key">
          </el-option>
        </el-select>
      </el-form-item> -->

      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getList(true)"
        >
          搜索
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="$refs.modal.show('add')"
        >
          新增
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="name"
          label="名称"
        >
        </el-table-column>

        <el-table-column
          prop="payType"
          label="类型"
        >
          <template slot-scope="scope">
            {{ nameMap[scope.row.payType] || scope.row.payType }}
          </template>
        </el-table-column>

        <el-table-column
          prop="status"
          label="状态"
        >
          <template slot-scope="scope">
            <el-switch
              @change="handelStatusChange($event,scope.row)"
              v-model="scope.row.status"
              active-text="激活"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column
          prop="action"
          label="操作"
          width="260"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.name === '工行支付'"
              @click="$refs.subMerchant.show(scope.row)">
              子商户管理
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <Modal
      ref="modal"
      @update="getList()"></Modal>

    <TicketSpot
      ref="ticketSpot"
      @update="getList()"></TicketSpot>
    <SubMerchant
      ref="subMerchant"
      @update="getList()"></SubMerchant>
  </div>
</template>

<script>
import Modal from './Modal.vue'
import TicketSpot from './TicketSpot.vue'
import SubMerchant from './SubMerchant.vue'
import { getPayChannel, updatePayChannel, deletePayChannel } from '@/api/payChannel'

const nameMap = {
  'POS': 'POS机',
  'VALUECARD': '存值卡',
  'B_SCAN_C_PAY': 'B扫C支付',
  'NO_PAY': '无需支付',
  'C_SCAN_B_PAY': 'C扫B支付'
}

export default {
  components: {
    Modal,
    TicketSpot,
    SubMerchant
  },
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    }
  },
  data () {
    return {
      nameMap,
      ticketTypeList: [],
      form: {
        pageNum: 1,
        pageSize: 10,
        keyWords: '',
        payType: '',
        status: '',
        type: ''
      },
      accountList: [],
      tableData: [],
      total: 0,
      loading: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList (goFirst) {
      this.loading = true
      goFirst && (this.form.pageNum = 1)
      getPayChannel(this.form).then(res => {
        this.total = res.data?.total || 0
        this.tableData = res.data?.records
      }).finally(() => {
        this.loading = false
      })
    },
    handleDelete (id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePayChannel(id).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      }).catch(() => {})
    },
    handelStatusChange (value, item) {
      const newItem = Object.assign({}, { ...item, status: value })

      updatePayChannel(newItem).then(res => {
        this.$message.success('操作成功')
        this.getList()
        this.$store.dispatch('getPayChannel')
      })
    }
  }
}
</script>

<style scoped>

</style>
