<template>
  <el-dialog
    title="充值信息"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="600px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="80px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="IC卡号" prop="cardNo">
            <el-row>
              <el-col :span="24">
                <el-input
                  v-model="rechargeCardData.cardNo"
                  style="width: 100%"
                  disabled
                />
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="用户名" prop="farmerName">
            <el-row>
              <el-col :span="24">
                <el-input
                  v-model="rechargeCardData.farmerName"
                  style="width: 100%"
                  disabled
                />
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="当前余额" prop="cardAmount">
            {{rechargeCardData.cardAmount}} 元
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="充值金额" prop="rechargercdRechargeamount">
            <el-row>
              <el-col :span="24">
                <el-input
                  type="number"
                  placeholder="充值金额"
                  style="width: 100px;margin-right: 10px"
                  v-model="baseForm.rechargercdRechargeamount">
                </el-input>
                元
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="支付方式" prop="rechargercdRechargetype">
            <div>
              <span @click="() => changePayType('pay')" :class="payType === 'pay' ? 'xianjin_pay_action' : ''" class="pay">
                <div class="img_container">
                  <img :src="cash" class="icon"/>
                  现金支付
                </div>
              </span>
              <span  @click="() => changePayType('weixin')" :class="payType === 'weixin' ? 'weixin_pay_action' : ''" class="pay">
                <div class="img_container">
                  <img :src="weixin" class="icon"/>
                  微信支付
                  <img :src="weixinRightBottom" class="bottomRight" v-if="payType === 'weixin'"/>
                </div>
              </span>
              <span  @click="() => changePayType('zhifubao')" :class="payType === 'zhifubao' ? 'zhifubao_pay_action' : ''" class="pay">
                <div class="img_container">
                <img :src="zhifubao" class="icon_zfb"/>
              </div>
              </span>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: right">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { rechargeCard } from '@/api/card/index.js'
import { assign } from 'lodash-es';
import cash from '../assets/cash.svg'
import weixin from '../assets/weixin.svg'
import weixinAction from '../assets/weixin_action.svg'
import zhifubao from '../assets/zhifubao.svg'
import weixinRightBottom from '../assets/weixin_right_bottom.svg'
export default {
  name: 'rechargeCard',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rechargeCardData:{
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      weixinRightBottom,
      payType: 'pay',
      cash: cash,
      weixinAction,
      weixin,
      zhifubao,
      dialogSubmitLoading: false,
      dialogVisible: false,
      baseForm: {
        rechargercdRechargeamount: 1
      },
      rechargercdRechargetype: 0,
      hasFetched: false,
      rules: {
        rechargercdRechargeamount: [
          { required: true, pattern: /^[1-9]\d*(\.\d+)?$/, message: '请正确输入充值金额', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
  },
  methods: {
    changePayType(payType) {
      this.payType = payType
      this.rechargercdRechargetype = payType == 'pay' ? 0 : payType == 'weixin' ? 1 : 2
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.resetFormData();
      this.dialogVisible = false;
    },
    resetFormData() {
      this.baseForm = {};
    },
    async rechargeCardHandler() {
      const { cardId  } = this.rechargeCardData
      const rechargercdRechargeamount = Number(this.baseForm.rechargercdRechargeamount)
      /*
        现金 0
        微信 1
        支付宝 2
      */
      const res = await rechargeCard({
        cardId,
        rechargercdRechargeamount,
        rechargercdRechargetype: this.rechargercdRechargetype
      })
      if(res?.code === "000000") {
        this.handleClose()
        this.$emit('refresh')
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        valid && this.rechargeCardHandler()
      })
    },
    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
::v-deep input[type='number'] {
  -moz-appearance: textfield !important;
}
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
.img_container{
  display: flex;
  height: 40px;
  justify-content: center;
  align-items: center;
  position: relative;
  color: #333333;
  font-size: 14px;
  font-weight: 400;
  .bottomRight{
    position: absolute;
    right:0;
    bottom:0;
  }
  .icon{
    margin:0 8px 0 8px;
  }
  .icon_zfb{
    margin-left: -12px;
  }
}
.xianjin_pay_action{
  border: 1px solid #F4A72F !important;
}
.weixin_pay_action{
  border: 1px solid #14A478 !important;
}
.zhifubao_pay_action{
  border: 1px solid #35B4FD !important;
}
.pay{
  display: block;
  width: 124px;
  height: 40px;
  line-height: 40px;
  margin-bottom: 7px;
  text-align: center;
  border-radius: 5px;
  border:1px solid #e4e7ed;
  cursor: pointer;
  align-items: center;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
