<template>
	<div class="order-check-whole">
		<back-header :path="'/mall/web/vgoods/detail/'+goodsNo">
			订单确认
		</back-header>
		<div class="top">
			<div class="goods-container">
				<img :src="imghost+info.goodsImgUrl">
				<div class="goods-main-container">
					<div class="name">{{info.goodsName}}</div>
					<div class="box">
						<div class="box-text">{{info.shortDesc}}</div>
						<div class="box-price">¥{{info.totalPrice?info.totalPrice/100:''}}</div>
					</div>
				</div>
			</div>
			<div class="gift-container" v-if="info.gifts&&info.gifts.length>0">
				<div class="gift-item" v-for="item in info.gifts">
					<div class="text">【赠品】</div>
					<div class="name">{{item.name}}</div>
					<div class="num">x{{item.num}}</div>
				</div>
			</div>
		</div>
		<div class="service-area">
			<div class="title">
				<img src="../assets/images/order-list/location.png">
				服务区域：
			</div>
			<div class="area-text">
				{{info.serviceAreaStr}}
			</div>
		</div>
		<div class="coupon-container" @click="go()">
			<div class="coupon-left">
				<div class="left-box">
					<img src="../assets/images/order-list/coupon.png">
					<div class="text">
						优惠券：
					</div>
					<div class="name" v-if="info.primeCoupon">{{info.primeCoupon.couponName}}</div>
				</div>
				<div class="price" v-if="info.primeCoupon">-￥{{info.primeCoupon?info.primeCoupon.amount/100:""}}</div>
			</div>
			<img class="icon" src="../assets/images/order-list/arrow.png">
		</div>
		<div class="message-container">
			<div class="title">
				<img src="../assets/images/order-list/edit.png">
				留言备注：
			</div>
			<textarea v-model="info.text" placeholder="为了更好地为您服务，请简单说明您家的格局面积与保洁史"></textarea>
		</div>
		<div class="pay-container">
			<div class="text">支付方式：</div>
			<div class="pay">
				<img src="../assets/images/order-list/wechat.png">
				微信支付
			</div>
		</div>
		<div class="clause">
			<div class="true-box" @click="agree=!agree">
				<img v-show="agree" src="../assets/images/order-list/true.png">
			</div>
			<div @click="agree=!agree">
				我已阅读并同意
			</div>
			<a class="tips" href="http://www.yihaojiazheng.cn/mall/web/agreement">
				《一号家政服务协议书》
			</a>
		</div>
		<div class="fill"></div>
		<div class="button" role="button">
			<div class="button-text">
				还需支付：
				<div class="num">¥{{price}}</div>
			</div>
			<button :class="{disabled:!agree}" @click="pay()">立即付款</button>
		</div>
	</div>
</template>
<script>
import backHeader from "../components/back-header.vue";
import orderApi from "../models/order-model.js";
var qs = require("qs");
export default {
    data() {
        return {
            info: {
                text: ""
            },
            agree: false,
            goodsNo: ""
        };
    },
    components: {
        "back-header": backHeader
    },
    computed: {
        price() {
            let price = this.info.payPrice;
            let discount = this.info.primeCoupon
                ? this.info.primeCoupon.amount
                : 0;
            return price / 100 - discount / 100;
        }
    },
    methods: {
        getData() {
            let goodsNo = this.getParam("goodsNo");
            let couponNo = this.getParam("couponNo");
            let goodsVersion =
                this.getParam("goodsVersion") ||
                sessionStorage.getItem("goodsVersion");
            orderApi
                .checkOrder(
                    qs.stringify({
                        goodsNo: goodsNo,
                        couponNo: couponNo,
                        goodsVersion: goodsVersion
                    })
                )
                .then(res => {
                    if (res.code == 0) {
                        this.agree = true;
                        let data = res.response;
                        this.info = data;
                        this.wechat(data.merchantNo);
                    } else if (res.code == 666) {
                        this.$vux.toast.text("您还没登录，请先登录!", "middle");
                        setTimeout(function() {
                            window.location.href = "/mall/web/user/login";
                        }, 2000);
                    } else {
                        if (
                            res.msg == "" ||
                            res.msg == undefined ||
                            res.msg == null
                        ) {
                            res.msg = "操作失败，请刷新重试";
                        }
                        this.$vux.toast.text(res.msg, "middle");
                    }
                    this.$vux.loading.hide();
                });
        },
        pay() {
            if (!this.agree) {
                this.$vux.toast.text(
                    "请先勾选同意《一号家政服务协议书》",
                    "middle"
                );
                return;
            }
            this.addOrder().then(res => {
                this.$vux.loading.show({
                    text: "正在调起支付"
                });
                if (this.isWeixin() && res.data.payPrice > 0) {
                    //微信内打开，直接调起微信公众号支付
                    this.$vux.loading.hide();
                    location.href = res.authUrl.replace(
                        "state=2",
                        "state=2" + "," + res.data.orderNo
                    );
                } else {
                    var paymentType = res.data.payPrice > 0 ? 7 : 10;
                    orderApi
                        .payMoney(
                            qs.stringify({
                                orderNo: res.data.orderNo,
                                paymentType: paymentType
                            })
                        )
                        .then(data => {
                            this.$vux.loading.hide();
                            if (data && data.code === 0) {
                                if (paymentType === 7) {
                                    var transMessageOutput = data.response;
                                    var transInfo =
                                        transMessageOutput.weixinTransParameters;
                                    if (
                                        transInfo.prepayid !== "" &&
                                        transInfo.prepayid !== undefined &&
                                        transInfo.prepayid != null
                                    ) {
                                        window.location.href =
                                            transInfo.mwebUrl;
                                    }
                                } else {
                                    setTimeout(function() {
                                        window.location.href =
                                            "/mall/web/order_index";
                                    }, 2000);
                                }
                            } else if (data.code === 666) {
                                this.$vux.toast.text(
                                    "您还没登录，请先登录!",
                                    "middle"
                                );
                                setTimeout(function() {
                                    window.location.href =
                                        "/mall/web/user/login";
                                }, 2000);
                            } else {
                                if (
                                    data.msg === "" ||
                                    data.msg === undefined ||
                                    data.msg == null
                                ) {
                                    data.msg = "操作失败，请刷新重试";
                                }
                                this.$vux.toast.text(data.msg, "middle");
                                setTimeout(function() {
                                    window.location.href =
                                        "/mall/web/order_index";
                                }, 2000);
                            }
                        });
                }
            });
        },
        addOrder() {
            let goodsNo = this.getParam("goodsNo");
            let couponNo = this.getParam("couponNo");
            let goodsVersion = this.getParam("goodsVersion");
            let promise = new Promise((resolve, reject) => {
                orderApi
                    .addOrder(
                        qs.stringify({
                            goodsNo: goodsNo,
                            couponNo: couponNo,
                            goodsVersion: goodsVersion,
                            remark: this.info.text
                        })
                    )
                    .then(res => {
                        if (res.code == 0) {
                            resolve(res.response);
                        } else if (res.code === 666) {
                            this.$vux.toast.text(
                                "您还没登录，请先登录!",
                                "middle"
                            );
                            setTimeout(function() {
                                window.location.href = "/mall/web/user/login";
                            }, 2000);
                        } else if (res.code === 200) {
                            this.$vux.toast.text(res.msg, "middle");
                            setTimeout(function() {
                                window.location.href = "/mall/web/order_index";
                            }, 2000);
                        } else {
                            if (
                                res.msg === "" ||
                                res.msg === undefined ||
                                res.msg == null
                            ) {
                                res.msg = "操作失败，请刷新重试";
                            }
                            this.$vux.toast.text(res.msg, "middle");
                        }
                    });
            });
            return promise;
        },
        wechat(merchantNo) {
            this.wxShare({
                merchantNo: merchantNo,
                source: 0,
                url: encodeURIComponent(location.href),
                shareUrl: encodeURIComponent(
                    "http://" + location.host + "/v2/home"
                )
            });
        },
        go() {
            let goodsVersion = this.getParam("goodsVersion") || "";
            sessionStorage.setItem("goodsVersion", goodsVersion);
            location.href = "/mall/web/coupon_index?" + this.goodsNo + "&";
        }
    },
    mounted() {
        this.getData();
        this.goodsNo = this.getParam("goodsNo");
    }
};
</script>
<style lang="scss" scoped>
.top {
    margin-bottom: 0.18rem;
}
.goods-container {
    width: 100%;
    color: #333;
    height: 1.6rem;
    margin-top: 0.1rem;
    background-color: white;
    display: flex;
    box-sizing: border-box;
    padding: 0.16rem 0.37rem 0rem 0.22rem;
    img {
        width: 1.28rem;
        height: 1.28rem;
        margin-right: 0.33rem;
    }
    .goods-main-container {
        flex-grow: 1;
        display: flex;
        flex-direction: column;
        padding-top: 0.06rem;
        .name {
            line-height: 1.1;
            font-size: 0.3rem;
            text-overflow: ellipsis;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            word-wrap: break-word;
            // margin-top: 0.1rem;
            white-space: normal;
            word-break: break-all;
            margin-bottom: 0.15rem;
        }
        .box {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            .box-text {
                font-size: 0.24rem;
                line-height: 1.1;
                text-overflow: ellipsis;
                overflow: hidden;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 1;
                word-wrap: break-word;
                white-space: normal;
                word-break: break-all;
                color: #999;
                height: 0.24rem;
                margin-right: 0.2rem;
            }
            .box-price {
                line-height: 1;
                font-weight: 400;
                font-size: 0.38rem;
                color: #333;
            }
        }
    }
}
.gift-container {
    width: 100%;
    box-sizing: border-box;
    padding: 0.2rem;
    background-color: #fff;
    border-top: 0.01rem solid rgba(243, 243, 243, 1);
    display: flex;
    flex-direction: column;
    color: #666;
    font-size: 0.24rem;
    .gift-item {
        display: flex;
        line-height: 1.2;
        align-items: center;
        .text {
            flex-shrink: 0;
        }
        .name {
            text-overflow: ellipsis;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            word-wrap: break-word;
            white-space: normal;
            margin-right: 0.1rem;
            word-break: break-all;
        }
        .num {
            flex-shrink: 0;
        }
    }
}
.service-area {
    padding: 0.36rem 0.2rem;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    color: #666;
    margin-bottom: 0.1rem;
    .title {
        display: flex;
        font-size: 0.3rem;
        align-items: center;
        line-height: 1;
        margin-bottom: 0.2rem;
        font-weight: normal;
        img {
            width: 0.32rem;
            height: 0.3rem;
            margin-right: 0.08rem;
        }
    }
    .area-text {
        padding-left: 0.4rem;
        font-size: 0.26rem;
    }
}
.coupon-container {
    width: 100%;
    height: 1rem;
    display: flex;
    align-items: center;
    padding-left: 0.2rem;
    font-size: 0.3rem;
    line-height: 1.1;
    background-color: #fff;
    box-sizing: border-box;
    justify-content: space-between;
    margin-bottom: 0.1rem;
    .coupon-left {
        flex-grow: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-right: 0.43rem;
        img {
            flex-grow: 0;
            width: 0.32rem;
            flex-shrink: 0;
            height: 0.3rem;
            margin-right: 0.08rem;
        }
        .text {
            flex-grow: 0;
            flex-shrink: 0;
            margin-right: 0.5rem;
        }
        .name {
            flex-grow: 1;
            // flex-shrink: 1;
            text-overflow: ellipsis;
            overflow: hidden;
            font-size: 0.28rem;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
            word-wrap: break-word;
            white-space: normal;
            // max-width: 3rem;
            word-break: break-all;
        }
        .price {
            color: #f42525;
            flex-shrink: 0;
            font-size: 0.34rem;
        }
    }
    .left-box {
        display: flex;
        align-items: center;
        flex-grow: 1;
        margin-right: 0.2rem;
    }
    .icon {
        width: 0.16rem;
        margin-right: 0.37rem;
        height: 0.27rem;
    }
}
.message-container {
    width: 100%;
    background-color: #fff;
    height: 2.48rem;
    margin-bottom: 0.1rem;
    padding: 0.36rem 0.3rem 0 0.2rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    color: #666;
    .title {
        display: flex;
        font-size: 0.3rem;
        align-items: center;
        line-height: 1;
        margin-bottom: 0.17rem;
        font-weight: normal;
        img {
            width: 0.32rem;
            height: 0.3rem;
            margin-right: 0.08rem;
        }
    }
    textarea {
        border-style: none;
        text-shadow: none;
        -webkit-appearance: none;
        -webkit-user-select: text;
        outline-color: transparent;
        font-family: "AvantGardGothEF-Book";
        padding-left: 0.4rem;
        height: 1.5rem;
        box-shadow: none;
        outline: none;
        outline-style: none;
        border: none;
        background-color: white;
    }
}
.pay-container {
    width: 100%;
    height: 1rem;
    box-sizing: border-box;
    padding: 0 0.3rem 0 0.2rem;
    background-color: white;
    font-size: 0.3rem;
    line-height: 1;
    justify-content: space-between;
    margin-bottom: 0.3rem;
    display: flex;
    align-items: center;
    .pay {
        display: flex;
        align-items: center;
        img {
            width: 0.63rem;
            margin-right: 0.2rem;
            height: 0.52rem;
        }
    }
}
.clause {
    // width: 100%;
    padding-left: 0.36rem;
    display: flex;
    font-size: 0.28rem;
    align-items: center;
    margin-bottom: 0.66rem;
    line-height: 1;
    .true-box {
        width: 0.23rem;
        height: 0.23rem;
        margin-right: 0.11rem;
        border: 0.02rem solid #d0d0d0;
        border-radius: 0.03rem;
        position: relative;
        z-index: 1;
    }
    img {
        position: absolute;
        left: 0;
        // right: 0;
        width: 0.31rem;
        bottom: 0;
        z-index: 2;
        // width: 0.29rem;
        // height: 0.23rem;
    }
    .tips {
        color: #00ba86;
    }
}
.button {
    width: 100%;
    height: 1rem;
    display: flex;
    position: fixed;
    bottom: 0;
    justify-content: space-between;
    background-color: white;
    .button-text {
        flex-grow: 1;
        display: flex;
        font-size: 0.3rem;
        align-items: center;
        line-height: 1;
        padding-left: 0.3rem;
        colo: #333;
        .num {
            font-size: 0.36rem;
            color: #f42525;
        }
    }
    button {
        width: 3.5rem;
        height: 1rem;
        background: #00ba86;
        color: #fff;
        border-radius: 0px;
        border: none;
        color: white;
        font-size: 0.36rem;
    }
    .disabled {
        opacity: 0.6;
    }
}
</style>
