<template>
  <div class="subPage">
    <div class="inputCardNo flex-h">
      <div class="flex-item">
        <div>
          <el-input
            placeholder="请输入VIP卡号"
            v-model="CardNo"
            clearable>
          </el-input>
        </div>
      </div>
      <el-button type="success" @click='activateVip'>激活</el-button>
    </div>
    <el-dialog
      title="VIP卡激活"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <div>
        <ul class="activateList">
          <li class="flex-h">
            <span>卡号：</span>
            <div class="flex-item">
              <div>
                <span>{{vip.VipCardChildCode}}</span>
              </div>
            </div>
          </li>
          <li class="flex-h">
            <span>金额：</span>
            <div class="flex-item">
              <div>
                <span>{{vip.Amount}}</span>
              </div>
            </div>
          </li>
          <li class="flex-h">
            <span>绑定用户：</span>
            <div class="flex-item" style="margin-right:10px;">
              <div>
                <el-input
                  placeholder="请输入手机号"
                  v-model="customerPhone"
                  clearable>
                </el-input>
              </div>
            </div>
            <el-button type="success" @click='searchCustomer'>查询</el-button>
          </li>
          <li class="flex-h">
            <span>用户姓名：</span>
            <div class="flex-item">
              <div>
                <span>{{customer.CustomerName}}</span>
              </div>
            </div>
          </li>
          <li class="flex-h">
            <span>销售：</span>
            <div class="flex-item" style="margin-right:10px;">
              <div>
                <el-input
                  placeholder="请输入销售手机号"
                  v-model="salesPhone"
                  clearable>
                </el-input>
              </div>
            </div>
            <el-button type="success" @click='searchSeller'>查询</el-button>
          </li>
          <li class="flex-h">
            <span>销售姓名：</span>
            <div class="flex-item">
              <div>
                <span>{{sales.RealName}}</span>
              </div>
            </div>
          </li>
        </ul>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="vipCharger">激 活</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const { mapState: mapVipState, mapActions: mapVipActions } = createNamespacedHelpers('vip')
const { mapState: mapCustomerState, mapActions: mapCustomerActions } = createNamespacedHelpers('customer')
const { mapState: mapUserState, mapActions: mapUserActions } = createNamespacedHelpers('user')
export default {
  name: '',
  data () {
    return {
      ajaxOff: true,
      centerDialogVisible: false,
      CardNo: '',
      salesPhone: '',
      customerPhone: ''
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    ...mapCustomerState({
      customer: x => x.customer,
    }),
    ...mapVipState({
      vip: x => x.vipinfo,
    }),
    ...mapUserState({
      sales: x => x.userinfo,
    }),
    CaseId: {
      get () {
        return this.postData.CaseId === '' ? this.defaultCaseId || '' : this.postData.CaseId
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  methods: {
    ...mapCustomerActions([
      'GetCustomerByTel',
      'SetCustomerInfoNull',
    ]),
    ...mapVipActions([
      'VipCharge',
      'GetVipByCode',
      'SetVipInfoNull',
    ]),
    ...mapUserActions([
      'GetUserByTel',
      'SetUserInfoNull',
    ]),
    searchCustomer () {
      if (this.customerPhone === '') {
        this.$message({
          type: 'error',
          message: '请先输入电话！',
        })
        return
      }
      this.GetCustomerByTel({ tel: this.customerPhone })
    },
    searchSeller () {
      if (this.salesPhone === '') {
        this.$message({
          type: 'error',
          message: '请先输入销售电话！',
        })
        return
      }
      this.GetUserByTel({ tel: this.salesPhone })
    },
    setNull () {
      this.salesPhone = ''
      this.customerPhone = ''
      this.SetUserInfoNull()
      this.SetCustomerInfoNull()
    },
    activateVip () {
      if (this.CardNo !== '') {
        this.GetVipByCode({ code: this.CardNo }).then((res) => {
          if (res) {
            if (res.Status !== 0) {
              this.$message({
                type: 'error',
                message: 'VIP卡已被激活！',
              })
              return
            }
            this.setNull()
            this.centerDialogVisible = true
          } else {
            this.$message({
              type: 'error',
              message: '没有查询到VIP卡信息！',
            })
          }
        })
      } else {
        this.$message({
          type: 'error',
          message: '请输入VIP卡号！',
        })
      }
    },
    vipCharger () {
      if (this.vip.VipCardChildCode === '') {
        this.$message({
          type: 'error',
          message: '没有需要激活的卡信息！',
        })
        return
      }
      if (this.customer.Phone === '') {
        this.$message({
          type: 'error',
          message: '没有绑定的用户信息！',
        })
        return
      }
      if (this.ajaxOff) {
        this.ajaxOff = false
        this.VipCharge({
          code: this.vip.VipCardChildCode,
          tel: this.customer.Phone,
          salesid: this.sales.UserId || '',
          salesname: this.sales.RealName || '',
        }).then(() => {
          this.ajaxOff = true
          this.$message({
            type: 'success',
            message: '激活成功！',
          })
          // setTimeout(() => {
          //   that.setNull()
          //   that.centerDialogVisible = false
          // }, 1000)
          this.$router.push({name: 'vipList'})
        }).catch(() => {
          this.ajaxOff = true
        })
      }
    },
  },
  mounted () { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.subPage {
  .inputCardNo {
    width: 400px;
    margin: 40px auto 0;
    white-space: nowrap;
    > div {
      margin-right: 20px;
    }
  }
  .activateList {
    > li {
      padding: 10px 0;
      span {
        line-height: 40px;
      }
      > span {
        min-width: 80px;
      }
    }
  }
}
</style>
