<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="会员ID"
        prop="keyWords7">
        <el-input v-model="form.keyWords7"></el-input>
      </el-form-item>
      <el-form-item
        label="会员类型"
        prop="keyWords1"
      >
        <el-select
          v-model="form.keyWords1"
          placeholder="请选择"
        >
          <el-option
            label="全部"
            value=""
          >
          </el-option>
          <el-option
            v-for="item in memberTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="起始时间"
        prop="startDate"
      >
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate"
      >
        <el-date-picker
          v-model="form.endDate"
          type="datetime"
          default-time="23:59:59"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="IC卡号"
        prop="keyWords2">
        <ReaderInput
          type="IC"
          v-model="form.keyWords2"></ReaderInput>
      </el-form-item>
      <el-form-item
        label="身份证号"
        prop="keyWords3">
        <ReaderInput
          type="ID"
          v-model="form.keyWords3"></ReaderInput>
      </el-form-item>
      <el-form-item
        label="会员名字"
        prop="keyWords4">
        <el-input v-model="form.keyWords4"></el-input>
      </el-form-item>
      <el-form-item
        label="闸机名称"
        prop="keyWords5">
        <el-input v-model="form.keyWords5"></el-input>
      </el-form-item>
      <el-form-item
        label="检票类型"
        prop="keyWords6">
        <el-select v-model="form.keyWords6">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="指纹"
            value="finger"
          ></el-option>
          <el-option
            label="人脸"
            value="face"
          ></el-option>
          <el-option
            label="IC卡"
            value="iccard"
          ></el-option>
          <el-option
            label="身份证"
            value="idcard"
          ></el-option>
          <el-option
            label="人工检票"
            value="person"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="keyWords8">
        <el-input v-model="form.keyWords8"></el-input>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="exportMemberCheckList">
          导出
        </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="iccardNo"
          label="IC卡号"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="category"
          label="会员大类"
        >
          <template slot-scope="prop">
            {{ prop.row.category | memberCategory }}
          </template>
        </el-table-column>
        <el-table-column
          prop="memberTypeName"
          label="会员类型"
        >
        </el-table-column>
        <el-table-column
          width="80"
          prop="name"
          label="姓名"
        >
        </el-table-column>
        <el-table-column
          width="170"
          prop="idcardNo"
          label="身份证号"
        >
          <template slot-scope="scope">
            {{ isPapersIdDesensitize ? $options.filters.formatIDCard(scope.row.idcardNo) : scope.row.idcardNo }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="phone"
          label="手机号"
        >
        </el-table-column>
        <el-table-column
          width="120"
          label="有效期至"
        >
          <template slot-scope="prop">
            {{ prop.row.endtime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          prop="checkLimitTimes"
          label="可检次数"
        >
          <template slot-scope="prop">
            {{ prop.row.checkLimitTimes | ifNolimit }}
          </template>
        </el-table-column>
        <el-table-column
          width="80"
          prop="has_checked_times"
          label="已检次数"
        >
        </el-table-column>
        <el-table-column
          prop="checker_name"
          label="检票设备"
        >
        </el-table-column>
        <el-table-column
          width="160"
          label="检票时间"
        >
          <template slot-scope="prop">
            {{ prop.row.checkTime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="检票类型"
        >
          <template slot-scope="prop">
            {{ prop.row.checkType | checkType }}
          </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>
  </div>
</template>

<script>
import ReaderInput from '@/components/ReaderInput'

import { getMemberType, getMemberCheckLog, exportMemberCheckList } from '@/api/member'
import moment from 'moment'

export default {
  name: 'memberCheckLog',
  components: {
    ReaderInput
  },
  data () {
    return {
      memberTypeList: [],
      form: {
        startDate: new Date(moment().startOf('day').valueOf()), // 销售起始时间
        endDate: new Date(moment().endOf('day').valueOf()), // 销售截止时间
        keyWords1: '', // 会员类型ID
        keyWords2: '', // ic卡号
        keyWords3: '', // 身份证号
        keyWords4: '', // 会员名字
        keyWords5: '', // 闸机名称
        keyWords6: '', // 检票类型 finger face iccard idcard
        keyWords7: '', // 会员id
        keyWords8: '', // 会员手机号
        orderByKeyWords: '',
        orderString: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  filters: {
    inOrOut (input) {
      switch (input) {
        case 'in':
          return '进'
        case 'out':
          return '出'
        default:
          break
      }
    },
    checkType (input) {
      switch (input) {
        case 'finger':
          return '指纹'
        case 'face':
          return '人脸'
        case 'iccard':
          return 'IC卡'
        case 'idcard':
          return '身份证'
        case 'person':
          return '人工检票'
        default:
          break
      }
    }
  },
  mounted () {
    this.getMemberTypeList().then(() => {
      this.getList()
    })
  },
  computed: {
    isPapersIdDesensitize () {
      const value = this.$store.state.app.systemConfig.find(i => i.configKey === 'papers_id_desensitize')?.configValue
      return value && value === '1'
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取会员类型
    getMemberTypeList () {
      return getMemberType({
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.memberTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    // 获取列表
    getList () {
      this.loading = true
      getMemberCheckLog(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    exportMemberCheckList () {
      exportMemberCheckList(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    }
  }
}
</script>
