<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="会员ID"
        prop="filter1">
        <el-input v-model="form.filter1"></el-input>
      </el-form-item>
      <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="keyWords9">
        <el-input v-model="form.keyWords9"></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="1"
          ></el-option>
          <el-option
            label="不可用"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="库存状态"
        prop="keyWords8">
        <el-select v-model="form.keyWords7">
          <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="keyWords8">
        <el-select v-model="form.keyWords8">
          <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
          type="primary"
          @click="queryMemberList">
          查询
        </el-button>
        <el-button
          type="primary"
          v-if="checkPermission('member:recharge_card')"
          @click="cancelBatch">
          {{ isMultiple?'退出批量充值':'批量充值' }}
        </el-button>
        <el-button
          v-if="isMultiple"
          type="primary"
          @click="showBatchRechargeDialog()">
          充值
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        查询结果
        <el-button
          type="primary"
          v-if="isMultiple"
          @click="$refs.dataTable.clearSelection()">
          清空选择
        </el-button>
      </div>

      <el-table
        ref="dataTable"
        border
        stripe
        height="600"
        :row-key="(row) => row.id"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        :data="tableData">
        <el-table-column
          v-if="isMultiple"
          type="selection"
          :row-key="(row)=>{ return row.id}"
          :reserve-selection="true"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          width="100"
          label="编号">
        </el-table-column>
        <el-table-column
          width="100"
          prop="memberType.name"
          label="会员类型">
        </el-table-column>
        <el-table-column
          width="100"
          prop="name"
          label="姓名">
        </el-table-column>
        <el-table-column
          width="100"
          prop="address"
          label="地址">
          <template slot-scope="prop">
            <Ellipsis
              type="tooltips"
              :content="prop.row.address"
              :length="10"></Ellipsis>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="phone"
          label="手机号">
        </el-table-column>
        <el-table-column
          width="100"
          prop="pay_balance"
          label="可用支付余额">
        </el-table-column>
        <el-table-column
          width="100"
          prop="total_balance"
          label="总余额">
        </el-table-column>
        <el-table-column
          width="100"
          prop="is_valid"
          label="可用状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_valid?'success':'danger'">
              {{ scope.row.is_valid?'可用':'不可用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="isActive"
          label="出库状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_out_storage?'success':'info'">
              {{ scope.row.is_out_storage?'已出库':'未出库' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="isActive"
          label="激活状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isActive?'success':'info'">
              {{ scope.row.isActive?'已激活':'未激活' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="activate_end_time"
          label="激活截止时间">
          <template slot-scope="scope">
            {{ scope.row.activate_end_time | formatDate }}
          </template>
        </el-table-column>

        <el-table-column
          width="140"
          label="创建时间">
          <template slot-scope="props">
            {{ props.row.createTime | formatTime }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="生效时间">
          <template slot-scope="props">
            {{ props.row.starttime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          label="截止时间">
          <template slot-scope="props">
            {{ props.row.endtime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          width="150"
          label="无效时间">
          <template slot-scope="props">
            {{ props.row.invalid_time }}
          </template>
        </el-table-column>
        <el-table-column
          width="200"
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              v-if="scope.row.memberType.category ===5"
              @click="$refs.balanceRechargeDialog.show(scope.row)">
              余额充值
            </el-button>
            <el-button
              :disabled="cantRecharge(scope.row.memberType)"
              type="text"
              size="mini"
              v-if="checkPermission('member:recharge_card')"
              @click="showRecharge(scope.row)">
              充值 / 时效设置
            </el-button>
            <el-button
              type="text"
              size="mini"
              v-if="checkPermission('member:back_card')"
              @click="showClear(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>
    <BatchRechargeDialog
      ref="batchRechargeDialog"
      :multiple-selection="multipleSelection"
      @updateList="resetMultipleSelection"
    >
    </BatchRechargeDialog>

    <BalanceRechargeDialog
      ref="balanceRechargeDialog"
      :current-item="currentItem"
      @updateList="getMemberList"
    ></BalanceRechargeDialog>
    <ElDialog
      title="充值 / 时效设置"
      width="740px"
      v-model="rechargeVisible"
      :submit-disable="submitDisable"
      @updateDisable="updateDisable">
      <RechargeDialog
        :current-item="currentItem"
        @updateList="getMemberList"
        :submit-disable="submitDisable"
        @updateBtn="updateBtn"
      ></RechargeDialog>
    </ElDialog>
    <ElDialog
      title="会员结算"
      width="740px"
      v-model="clearVisible">
      <ClearDialog
        :current-item="currentItem"
        @updateList="getMemberList"
      ></ClearDialog>
    </ElDialog>
  </div>
</template>

<script>

import ElDialog from '@/components/Dialog'
import RechargeDialog from './memberRecharge/rechargeDialog'
import BatchRechargeDialog from './memberRecharge/batchRechargeDialog'
import ClearDialog from './memberRecharge/clearDialog'
import BalanceRechargeDialog from './memberRecharge/balanceRechargeDialog'
import ReaderInput from '@/components/ReaderInput'
import checkPermission from '@/utils/checkPermission'
import { getPayChannel } from '@/api/systemConfig'
import { apiMember, getMemberType } from '@/api/member'
import moment from 'moment'

export default {
  name: 'memberRecharge',
  components: {
    ElDialog,
    RechargeDialog,
    BatchRechargeDialog,
    ClearDialog,
    BalanceRechargeDialog,
    ReaderInput
  },
  data () {
    return {
      comName: '',
      memberTypeList: [],
      multipleSelection: [],
      batchRechargeVisible: false,
      isMultiple: false,
      form: {
        startDate: new Date(moment('2021-05-01').startOf('day').valueOf()), // 销售起始时间
        endDate: new Date(moment().endOf('day').valueOf()), // 销售截止时间
        filter1: '',
        keyWords1: '', // 会员类型ID
        keyWords2: '', // 姓名
        keyWords3: '', // 手机号
        keyWords4: '', // 卡号
        keyWords5: '', // 身份证号
        keyWords6: '', // 可用状态
        keyWords7: '', // 是否库存
        keyWords8: '', // 激活状态
        keyWords9: '', // 住址
        orderByKeyWords: '',
        orderString: '',
        pageNum: 1,
        pageSize: 100
      },
      tableData: [],
      total: 0,
      rechargeVisible: false,
      clearVisible: false,
      balanceRechargeVisible: false,
      currentItem: {},
      loading: false,
      payChannelOptions: [],
      submitDisable: false
    }
  },
  async mounted () {
    await getPayChannel().then(res => {
      this.payChannelOptions = res
    })
    // this.getMemberList()

    this.getMemberTypeList().then(() => {
      this.getMemberList()
    })
  },
  methods: {
    checkPermission,
    queryMemberList () {
      this.$refs.dataTable.clearSelection()
      this.getMemberList()
    },
    cancelBatch () {
      this.isMultiple = !this.isMultiple
      this.$refs.dataTable.clearSelection()
    },
    showBatchRechargeDialog () {
      if (!this.multipleSelection.length) {
        this.$message.error('请至少选择一项')
        return
      }
      this.$refs.batchRechargeDialog.show()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    resetMultipleSelection () {
      this.$refs.dataTable.clearSelection()
      this.getMemberList()
    },
    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
      })
    },
    // 获取会员类型
    getMemberTypeList () {
      return getMemberType({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.memberTypeList = res.list
      })
    },
    cantRecharge (type) {
      // 按次检票，景点单独限制的，和按时间，直到票种有效期的，不能充值
      let eachByCount = type.check_pattern === 'count' && type.scenic_checkTimes_type === 1
      let untillEnd = type.check_pattern === 'time' && type.valid_days === -1
      return eachByCount || untillEnd
    },
    showRecharge (item) {
      this.currentItem = item
      this.rechargeVisible = true
    },
    showBatchRecharge () {
      this.batchRechargeVisible = true
    },
    showClear (item) {
      this.currentItem = item
      this.clearVisible = true
    },
    showBalanceRecharge (item) {
      this.currentItem = item
      this.balanceRechargeVisible = true
    },
    updateBtn (val) {
      this.submitDisable = val
      console.log('触发更新submitDisable到', this.submitDisable)
    },
    updateDisable (val) {
      this.submitDisable = val
    }
  }
}
</script>
<style>
.finger-pic {
  width: 90px;
  height: 120px;
  background-size: contain;
  background-position: center;
}
</style>
