<template>
  
  <view class="page__view">

		<navbar title="提现" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">
      
      <view class="flex card row">
        <view class="row-label">余额</view>
        <view class="row-content">{{ `¥${userCenterData.balance}` }}</view>
      </view>

      <view class="card">
        <view class="card-header">微信提现</view>
        <view class="form">
          <uv-form 
            ref="form"
            :model="form"
            :rules="rules"
            errorType="toast"
          >
            <view class="form-item">
              <uv-form-item prop="userName" :customStyle="formItemStyle">
                <view class="form-item-label">真实姓名</view>
                <view class="form-item-content">
                  <formInput v-model="form.userName"></formInput>
                </view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="transferAmount" :customStyle="formItemStyle">
                <view class="form-item-label">提现金额</view>
                <view class="form-item-content">
                  <formInput v-model="form.transferAmount"></formInput>
                </view>
              </uv-form-item>
            </view>
					</uv-form>
				</view>
			</view>

      <view class="notice">
        请仔细检查并确认相关信息，因用户个人疏忽导致的充值错误。需由用户自行承担。
        <text class="highlight" @click="$refs.modal.open('withdrawal_instructions', '提现须知')">《提现须知》</text>
      </view>

		  <agreementModal ref="modal"></agreementModal>

    </view>
  
    <view class="bottom">
      <button class="btn" @click="onSubmit">提现</button>
    </view>

  </view>
</template>

<script>
	import { mapState } from 'vuex'

  import formInput from '@/pages_order/components/formInput.vue'
  import agreementModal from '@/pages_order/components/agreementModal.vue'

  export default {
    components: {
      formInput,
      agreementModal,
    },
    data() {
      return {
        form: {
          userName: null,
          transferAmount: null,
        },
        rules: {
          'userName': {
            type: 'string',
            required: true,
            message: '请输入真实姓名',
          },
          'transferAmount': {
            type: 'string',
            required: true,
            message: '请输入提现金额',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    computed: {
			...mapState(['userCenterData']),
    },
    methods: {
      async onSubmit() {
        try {
          await this.$refs.form.validate()

          const {
            userName,
            transferAmount,
          } = this.form

          const params = {
            userName,
            transferAmount,
          }

          const result = await this.$fetch('cashout', params)

          console.log('cashout', result)

          // 拉起微信收款确认页面
          if (!wx.canIUse('requestMerchantTransfer')) {
              wx.showModal({
                  content: '你的微信版本过低，请更新至最新版本。',
                  showCancel: false,
              });
              return
          }
         
          // 在真机环境中，调用API
          wx.requestMerchantTransfer({
            mchId: this.$config.mchId,
            appId: wx.getAccountInfoSync().miniProgram.appId,
            package: result.packageInfo,
            success: (res) => {
              uni.showToast({
                  title: '提现申请已提交',
                  icon: 'success'
              })
              this.form.transferAmount = null
              this.form.userName = null
									
              this.$api('getMoney', {
                id : result.outBillNo,
              }).then(res => {
                this.$store.commit('getUserInfo')
				        this.$store.commit('getUserCenterData')
              })
            },
            fail: (res) => {
              console.log('fail:', res);
              uni.showToast({
                  title: '提现失败，请稍后再试',
                  icon: 'none'
              })
            },
            complete: (res) => {
              console.log('requestMerchantTransfer完成:', res);
            }
          });


          // uni.showToast({
          //   icon: 'success',
          //   title: '提交成功',
          // });

          setTimeout(() => {
            this.$utils.navigateBack()
          }, 800)

        } catch (err) {
          console.log('cashout err', err)
          const { message } = err

          message && uni.showToast({
            icon: 'none',
            title: message,
          });
        }
      },
    },
  }
</script>

<style lang="scss" scoped>

	.page__view {
		width: 100vw;
		min-height: 100vh;
    background: $uni-bg-color;
		position: relative;
		
		/deep/ .nav-bar__view {
			position: fixed;
			top: 0;
			left: 0;
		}
	}

	.main {
		padding: calc(var(--status-bar-height) + 144rpx) 32rpx 224rpx 32rpx;
	}

	.card {
		padding: 32rpx;
		background: #FFFFFF;
		border: 2rpx solid #FFFFFF;
		border-radius: 32rpx;

		& + & {
			margin-top: 40rpx;
		}

		&-header {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1.4;
			color: #252545;
			margin-bottom: 32rpx;
		}
	}

  .row {
    justify-content: flex-start;
    column-gap: 16rpx;
    font-family: PingFang SC;
    font-weight: 400;
    font-size: 36rpx;
    line-height: 1.4;
    color: #181818;

    &-label {
			font-weight: 500;
    }

    &-content {

    }
  }

	.form {
		padding: 8rpx 0 0 0;

		&-item {
			border-bottom: 2rpx solid #EEEEEE;

			&:last-child {
				border: none;
			}

			& + & {
				margin-top: 40rpx;
			}

			&-label {
				font-family: PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				line-height: 1.4;
				color: #181818;
			}

			&-content {
				margin-top: 14rpx;
				padding: 6rpx 0;

				.text {
					padding: 2rpx 0;
					font-family: PingFang SC;
					font-weight: 400;
					font-size: 32rpx;
					line-height: 1.4;

					&.placeholder {
						color: #C6C6C6;
					}
				}
			}
		}
	}

  .notice {
    margin-top: 40rpx;
    font-size: 24rpx;
    line-height: 1.4;
    color: #BABABA;

    .highlight {
      color: #F79400;
    }
  }

	.bottom {
		position: fixed;
		left: 0;
		bottom: 0;

		width: 100vw;
		// height: 200rpx;
		padding: 24rpx 40rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 24rpx);
		background: #FFFFFF;
		box-sizing: border-box;

		.btn {
			width: 100%;
			padding: 14rpx 0;
			box-sizing: border-box;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1.4;
			color: #FFFFFF;
			background: linear-gradient(to right, #21FEEC, #019AF9);
			border: 2rpx solid #00A9FF;
			border-radius: 41rpx;
		}
	}
</style>