<template>
  <a-modal
    centered
    class="zero-clearing-modal"
    :footer="null"
    :maskClosable="false"
    :title="'清零'+titName"
    v-model="isShow"
    @cancel="isShow = false"
    :width="450">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="mainContent" v-if="conInfo">
        <a-checkbox-group v-model="accountType" id="zero-clearing-checkbox">
          <a-checkbox value="RECHARGE" id="zero-clearing-checkbox1">清空{{ (conInfo.rechargeBalance||conInfo.rechargeBalance==0)?toThousands(conInfo.rechargeBalance):'--' }}元【充值余额】</a-checkbox>
          <br />
          <a-checkbox value="GIVE" id="zero-clearing-checkbox2">清空{{ (conInfo.giveBalance||conInfo.giveBalance==0)?toThousands(conInfo.giveBalance):'--' }}元【补贴余额】</a-checkbox>
        </a-checkbox-group>
        <div style="margin-top:10px;">注意：请慎重操作，一旦清空，不可变更</div>
      </div>
      <div class="btn-cont">
        <a-button id="zero-clearing-modal-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="zero-clearing-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 接口
import { storeAccountSetZero } from '@/api/storeAccount'
export default {
  name: 'ZeroClearingModal',
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false, // 页面加载loading
      isShow: this.openModal, //  是否打开弹框
      titName: '', // 弹窗标题
      accountType: undefined, // 清空目标
      conInfo: null // 回显数据内容
    }
  },
  methods: {
    // 清零保存
    handleSave () {
      const _this = this
      if (!_this.accountType || (_this.accountType && _this.accountType.length === 0)) {
        _this.$message.warning('请选择清空目标')
        return
      }
      const ajaxData = _this.accountType.map(con => {
        const obj = {
          accountType: con,
          tradeAmount: con === 'RECHARGE' ? '-' + _this.conInfo.rechargeBalance : '-' + _this.conInfo.giveBalance,
          storeAccountSn: _this.conInfo.storeAccountSn,
          storeSn: _this.conInfo.storeSn,
          shelfSn: _this.conInfo.shelfSn
        }
        return obj
      })
      _this.spinning = true
      storeAccountSetZero(ajaxData).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok', res.data)
          _this.isShow = false
        }
        _this.spinning = false
      })
    },
    // 初始化  获取页面回显数据
    pageInit (row) {
      this.titName = '（' + row.customerName + '）' || ''
      this.conInfo = row
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.accountType = undefined
      }
    }
  }
}
</script>

<style lang="less">
.zero-clearing-modal {
	.ant-modal-body {
		padding: 40px 40px 24px;
	}
  .mainContent{
    width: 65%;
    margin:0 auto;
    .ant-checkbox-group{
      .ant-checkbox-wrapper{
        margin-bottom:10px;
      }
    }
  }
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
