<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="会员类型"
        prop="keyWords1">
        <el-select
          v-model="form.keyWords1"
          placeholder="请选择">
          <el-option
            value=""
            label="全部"></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
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="姓名"
        prop="keyWords2">
        <el-input v-model="form.keyWords2"></el-input>
      </el-form-item>
      <el-form-item
        label="IC卡号"
        prop="keyWords4">
        <ReaderInput
          type="IC"
          v-model="form.keyWords4">
        </ReaderInput>
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="keyWords3">
        <el-input v-model="form.keyWords3"></el-input>
      </el-form-item>
      <el-form-item
        label="身份证号"
        prop="keyWords5">
        <ReaderInput
          type="ID"
          v-model="form.keyWords5"></ReaderInput>
      </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="1"
          ></el-option>
          <el-option
            label="不可用"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getMemberList">
          搜索
        </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="id"
          label="编号">
        </el-table-column>
        <el-table-column
          prop="memberType.name"
          label="会员类型">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>
        <el-table-column
          width="100"
          prop="phone"
          label="手机号">
        </el-table-column>
        <el-table-column
          label="证件类型">
          <template slot-scope="scope">
            {{ scope.row.papers_type | paperType }}
          </template>
        </el-table-column>
        <el-table-column
          width="160"
          prop="papers_id"
          label="证件号码">
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址">
        </el-table-column>
        <el-table-column
          width="150"
          label="激活时间">
          <template slot-scope="prop">
            {{ prop.row.createTime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="生效时间">
          <template slot-scope="prop">
            {{ prop.row.starttime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="截止时间">
          <template slot-scope="prop">
            {{ prop.row.endtime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="showDetail(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="getMemberList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <ElDialog
      title="会员详情"
      width="900px"
      v-model="detailDialogVisible">
      <DetailDialog
        :info="currentItem"
      ></DetailDialog>
    </ElDialog>
  </div>
</template>

<script>
import ElDialog from '@/components/Dialog'
import DetailDialog from './memberList/DetailDialog'
import ReaderInput from '@/components/ReaderInput'

import { getMemberType, apiMember } from '@/api/member'

export default {
  components: {
    ElDialog,
    DetailDialog,
    ReaderInput
  },
  data () {
    return {
      memberTypeList: [],
      form: {
        startDate: '', // 销售起始时间
        endDate: '', // 销售截止时间
        keyWords1: '', // 会员类型ID
        keyWords2: '', // 姓名
        keyWords3: '', // 手机号
        keyWords4: '', // 卡号
        keyWords5: '', // 身份证号
        keyWords6: 1, // 状态
        keyWords7: 1, // 是否库存会员
        orderByKeyWords: '',
        orderString: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      detailDialogVisible: false,
      editDialogVisible: false,
      currentItem: {},
      currentMemberType: {},
      loading: false
    }
  },
  mounted () {
    this.getMemberTypeList().then(() => {
      this.getMemberList()
    })
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    // 获取会员类型
    getMemberTypeList () {
      return getMemberType({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.memberTypeList = res.list
      })
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getMemberList()
    },
    // 获取会员列表
    getMemberList () {
      this.loading = true
      apiMember('getMemberList', this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    showEditDialog (item) {
      let memberType = this.memberTypeList.find(type => type.id === item.memberType.id)
      if (memberType) {
        this.currentMemberType = memberType
        this.currentItem = item
        this.editDialogVisible = true
      } else {
        this.$message.error('会员类型已失效')
      }
    },
    showDetail (item) {
      this.currentItem = item
      this.detailDialogVisible = true
    }
  }
}
</script>
<style>
.finger-pic {
  width: 90px;
  height: 120px;
  background-size: contain;
  background-position: center;
}
</style>
