<template>
  <view class="content">
    <view class="supplyInfo">
	  <view class="tel flex">
	    <label>缴费名称：</label>
	    <input type="text" class="card input" v-model="payInfo.name" disabled/>
	  </view>
	  <view class="tel flex">
	    <label>订单编号：</label>
	    <input type="text" class="card input" v-model="payInfo.code" disabled/>
	  </view>
	  <view class="tel flex">
	    <label>住宅地址：</label>
	    <input type="text" class="card input" v-model="payInfo.address" disabled/>
	  </view>
	  <view class="tel flex">
	    <label>户主姓名：</label>
	    <input type="text" class="card input" v-model="payInfo.owner_name" disabled/>
	  </view>
	  <view class="tel flex">
	    <label>住宅面积：</label>
	    <input type="text" class="card input" v-model="payInfo.area" disabled/>
	  </view>
	  <view class="tel flex">
	    <label>缴费类型：</label>
		<radio-group  @change="payChange">
		  <radio value="1" :checked="payInfo.type!==2" style="margin-right: 30rpx;transform: scale(0.85);">金额</radio>
		  <radio value="2" :checked="payInfo.type===2" style="transform: scale(0.85);">空置</radio>
		</radio-group>
	  </view>
	  <view class="tel flex">
	    <label>缴费金额：</label>
	    <input type="text" class="card input" id="real_fee" v-model="payInfo.real_fee" disabled/>
	  </view>			
    </view>
    <button class="submit" @tap="paySubmit">{{payStatus ? '申请开票' : '缴费'}}</button>
	<uni-popup ref="popup" :mask-click="false">
		  <view class="popup-box">
			  <view>
				  <view class="popup-title">申请开票</view>
				  <view class="popup-content-box">
				  		  <view class="popup-content">
				  			  <view class="popup-type">
				  			  	  <view>开票抬头：</view><input type="text" value="" />
				  			  </view>
				  			  <view class="popup-type">
				  			  	  <view>备注：</view><input type="text" value="" />
				  			  </view>
				  		  </view>
				  </view>
			  </view>
			  <view class="popup-footer">
			  	<button class="popup-submit" @click="submitInvocie">提交</button>
			  </view>
		  </view>
		</uni-popup>
  </view>
</template>
<script>
import md5 from "@/common/md5.js";
export default {
  data() {
    return {
      payInfo: {
      },
	  payStatus:0,
	  pay_code : '',
    };
  },
  onLoad(option) {
	this.getPayDetail(option.id);
	this.pay_code = option.id;
  },
  methods: {   
    getPayDetail(payId) {
      let md5Sign = md5(
        "method=" +
          "user" +
          "&timestamp=" +
          getApp().globalData.globalTimestamp +
          "&secret=" +
          getApp().globalData.secret
      );
      let url =
        getApp().globalData.shareUrl +
        "api/api.php" +
        "?method=user&source=user&action=get_order_info&timestamp=" +
        getApp().globalData.globalTimestamp +
        "&sign=" +
        md5Sign;
      let postData = {
        code:payId,
        openId: getApp().globalData.open_id,
      };
      uni.request({
        url: url,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded",
        },
        data: postData,
        success: (res) => {
          if (res.data.code === 200) {
               this.payInfo = res.data.data;
			   this.payInfo.address = this.payInfo.estate_name  + this.payInfo.building_name + this.payInfo.unit + this.payInfo.room
          } else {
            uni.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2500,
            });
          }
        },
        fail: () => {
          console.log("连接失败");
        },
      });
    },
    payChange(e){
		let v = e.detail.value;
		if (v == 2)
		{
			let value = this.payInfo.part_fee - this.payInfo.reduction;
			this.payInfo.real_fee = value;
		}
		else
		{
			let value = this.payInfo.total_fee - this.payInfo.reduction;
			this.payInfo.real_fee = value;
		}
		this.payInfo.type = e.detail.value;
	},
	paySubmit(){
		if(this.payStatus){
			this.open()
			return
		}
		
		let md5Sign = md5(
		  "method=" +
		    "user" +
		    "&timestamp=" +
		    getApp().globalData.globalTimestamp +
		    "&secret=" +
		    getApp().globalData.secret
		);
		let url =
		  getApp().globalData.shareUrl +
		  "api/api.php" +
		  "?method=user&source=user&action=prepay&timestamp=" +
		  getApp().globalData.globalTimestamp +
		  "&sign=" +
		  md5Sign;
		let postData = {
		  code:this.pay_code,
		  type : this.payInfo.type,
		  openId: getApp().globalData.open_id,
		};
		uni.request({
		  url: url,
		  method: "POST",
		  header: {
		    "content-type": "application/x-www-form-urlencoded",
		  },
		  data: postData,
		  success: (res) => {
		    if (res.data.code === 200) {
		         let r = res.data.data;
				 uni.requestPayment({
				 	provider: 'wxpay',
				 	timeStamp: String(Date.now()),
				 	nonceStr: r.nonce_str,
				 	package: 'prepay_id=' + r.prepay_id,
				 	signType: 'MD5',
				 	paySign: r.sign,
				 	success: function (res) {
				 		console.log('success:' + JSON.stringify(res));
				 		this.payStatus = 1;
				 	},
				 	fail: function (err) {
				 		console.log('fail:' + JSON.stringify(err));
				 	}
				 });
				 
				 
		    } else {
		      uni.showToast({
		        title: res.data.msg,
		        icon: "none",
		        duration: 2500,
		      });
		    }
		  },
		  fail: () => {
		    console.log("连接失败");
		  },
		});
		
			
	  //支付这里缺少签名参数 还需要后台配合调用微信那边的接口
	 //参考https://github.com/dcloudio/H5P.Server/tree/master/payment
			
	},
	submitInvocie(){
		this.closePopup()
	},
	open() {
		  this.$refs.popup.open('center')
	  },
	closePopup() {
		  this.$refs.popup.close()
	  },	
  },
};
</script>
<style lang="scss" scoped>
.content {
  font-size: 28rpx;
  font-weight: 200;
  padding: 1% 2%;
  .title {
    font-size: 30rpx;
    margin: 4% 0;
  }
  label {
    display: inline-block;
    width: 25%;
    vertical-align: middle;
  }
  .card {
    background-color: rgb(248, 247, 247);
    border-radius: 10rpx;
  }
  .flex {
    display: flex;
    align-items: center;
    margin-bottom: 2%;
  }
  .input {
    padding: 0 2%;
    margin: 2% 0;
    display: inline-block;
    width: 80%;
    height: 70rpx;
  }
  .supplyInfo {
    border-radius: 40rpx;
    padding: 4%;
    box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
	margin: 30rpx 0 30rpx 0;

  }
  .info {
    margin-top: 2%;
    border-radius: 40rpx;
    padding: 2% 4%;
    box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
  }
  .submit {
    color: white;
    font-weight: normal;
    width: 55%;
    border-radius: 20rpx;
    background-color: #02a7f0;
    margin: 120rpx auto;
  }
}
 .popup-box {
	  height: 100%;
	  display: flex;
	  flex-direction: column;
	  justify-content: space-between;
  }
  .popup-title {
	  text-align: center;
	  font-weight: bold;
	  height: 30px;
	  border-bottom: 1px solid #d8d8d8;
  }
  .popup-content {
	  font-size: 28rpx;
	  padding: 10rpx 0 10rpx 0;
  }
  .popup-type {
	  display: flex;
	  height: 80rpx;
	  align-items: center;
	  margin-top: 60rpx;
		view {
			width: 30%; 
		}
		input {
			border: 1px solid #d7d7d7;
			width: 60%;
			border-radius: 5rpx;
			padding-left: 10rpx;
			height: 56rpx;
		}
  }
  .popup-footer {
	  display: flex;
	  button{
		  font-size: 26rpx;
		  height: 60rpx;
		  width: 150rpx;
		  line-height: 60rpx;
		  color: #FFFFFF;
	  }
  }
  .popup-cancel {
	  background-color: #AAAAAA;
  }
  .popup-submit {
	  background-color: #249CD3;
  }
</style>