<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="卡号"
        prop="cardNo">
        <el-input v-model="form.cardNo" clearable></el-input>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status">
        <el-select
          v-model="form.status" clearable>
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="item in cardStatusOptions"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList(true)">
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        IC卡列表
      </div> -->
      <el-table
        border
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="cardNo"
          label="卡片编号">
        </el-table-column>
        <el-table-column
          prop="validDateStart"
          label="有效期开始">
        </el-table-column>
        <el-table-column
          prop="validDateEnd"
          label="有效期结束">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态">
          <template slot-scope="props">
            <span>{{ props.row.status | cardStatusFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="activeTime"
          label="开卡时间">
        </el-table-column>
        <el-table-column
          prop="backTime"
          label="退卡时间">
        </el-table-column>
        <el-table-column
          prop="repealTime"
          label="挂失时间">
        </el-table-column>
        <el-table-column
          width="150px"
          label="操作">
          <template slot="header">
            操作
            <Tip msg="窗口退卡注销"></Tip>
          </template>
          <template slot-scope="scope">
            <el-link
              v-if="scope.row.status === 1"
              type="primary"
              @click="repealCard(scope.row)"
            >
              挂失
              <Tip msg="卡作废，卡片无法在使用，无法进行任何后续操作"></Tip>
            </el-link>
            <span><br></span>
            <el-link
              v-if="scope.row.status !== 2"
              type="primary"
              @click="cancelCard(scope.row)">
              注销退卡
              <Tip msg="卡片可以被再次使用"></Tip>
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getIcCardList, repealIcCard, refundIcCard } from '@/api/checker'
// import ConfigSetting from './deviceManage/ConfigSetting'

export default {
  data () {
    return {
      cardStatusOptions: [
        { id: 0, name: '未使用' },
        { id: 1, name: '使用中' },
        { id: 2, name: '已挂失' }
      ],
      form: {
        cardNo: '', // 名称
        status: '', // 状态
        pageNum: 1,
        pageSize: 10
      },
      loading: false,
      tableData: [],
      total: 0,
      currentItem: {}
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  created () {
    this.getList()
  },
  components: {},
  filters: {
    filterOnline (n) {
      return n === 1 ? '在线' : '离线'
    },
    cardStatusFilter (n) {
      switch (n) {
        case 0:
          return '未使用'
        case 1:
          return '使用中'
        case 2:
          return '已挂失'
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取通道列表
    getList (goFirst) {
      let params = { ...this.form }

      goFirst && (params.pageNum = 1)
      this.loading = true
      getIcCardList(params).then(res => {
        const list = res.data?.records || []
        this.total = res.data?.total || 0
        this.tableData = list
        this.loading = false
      })
    },
    repealCard (data, index) {
      this.$confirm('确定挂失卡片吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        repealIcCard(data).then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {})
    },
    cancelCard (data, index) {
      this.$confirm('确定注销退卡吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        refundIcCard(data).then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>
