<template>
    <app-layout title="确认订单">
        <view class="tips-container">
            <widget-tips text="保险公司承保，人身财产，双重保障，下单无忧！" />
        </view>
        <view class="create-order-container">
            <view class="common-form-widget-group">
                <view class="widget-item" @click="selectAddress">
                    <text class="iconfont icon-dingwei icon"></text>
                    <text class="title">
                        <text class="text limit-line clamp-1">
                            {{ addressText ? addressText : "请选择上门地址" }}
                        </text>
                    </text>
                    <text class="iconfont icon-jinru more"></text>
                </view>
                <view class="widget-item">
                    <text class="iconfont icon-shijian icon"></text>
                    <picker mode="date" :value="datetime" @change="bindDateChange" class="picker">
                        <text class="title">
                            <text class="text limit-line clamp-1">
                                {{ datetime ? datetime : "选择期望上门时间" }}
                            </text>
                        </text>
                    </picker>
                    <text class="iconfont icon-jinru more"></text>
                </view>
            </view>
            <view class="service-view">
                <view class="service-item" v-for="(item, idx) in serviceList" :key="idx">
                    <service-preview-item :data="item" />
                </view>
            </view>
            <view class="insurance-box">
                <service-insurance :price="insurancePrice" :insurance="insurance" @change="changeInsuranceState" />
            </view>
            <view class="demand">
                <text class="title">留言</text>
                <textarea
                    class="text-area"
                    v-model="content"
                    placeholder="补充描述您的需求，或其他特殊情况"
                    placeholder-class="placeholder-style-2"
                />
            </view>
            <view class="agreement-container">
                <agreement v-model="isAgree" />
            </view>
        </view>
        <view class="common-bottom-components" :style="{ bottom: pageConfig.safeAreaInsets.bottom + 'px' }">
            <text class="price">¥ {{ utils.formatNumber(total) }}</text>
            <view class="pay" @click="pay">
                <div class="text">去支付</div>
            </view>
        </view>
    </app-layout>
</template>

<script>
import AppLayout from "@/components/layout/layout";
import ServicePreviewItem from "@/components/service/preview-item";
import Agreement from "@/components/auth/agreement";
import WidgetTips from "@/components/widgets/tips";
import ServiceInsurance from "@/components/service/insurance";
import { mapState } from "vuex";
export default {
    name: "order-create",
    data() {
        return {
            utils: this.$utils,
            pageConfig: {},
            addressId: 0,
            addressText: "",
            datetime: "",
            content: "",
            insurance: false,
            isAgree: false,
            total: 0,
            orderData: [],
            serviceList: [],
        };
    },
    components: {
        AppLayout,
        ServicePreviewItem,
        Agreement,
        WidgetTips,
        ServiceInsurance,
    },
    computed: {
        ...mapState({
            insurancePrice: (state) => state.service.insurancePrice,
        }),
    },
    onLoad(e) {
        this.pageConfig = getApp().globalData.pageConfig;
        this.$store.dispatch("service/insurancePrice");
        if (!e.orderData) {
            this.$utils.toast("参数错误");
            return;
        }
        this.orderData = JSON.parse(decodeURIComponent(e.orderData));
        this.orderData.forEach((item) => {
            this.$models.service.getServiceDetail(item.id).then((response) => {
                let cover = "";
                if (response.goods.image.length > 0) {
                    cover = response.goods.image[0];
                }
                this.serviceList.push({
                    id: response.goods.id,
                    name: response.goods.post_title,
                    times: response.goods.post_hits,
                    cover: cover,
                    content: response.goods.post_content,
                    price: response.goods.money,
                });
                this.total += parseFloat(response.goods.money * item.count);
            });
        });
    },
    onShow() {},
    onReady() {},
    onReachBottom() {},
    onPullDownRefresh() {},
    methods: {
        bindDateChange(e) {
            this.datetime = e.detail.value;
        },
        selectAddress() {
            const that = this;
            this.$utils.toPage("/pages/address/address?openType=choose&id=" + that.addressId, {
                events: {
                    setAddress(address) {
                        that.addressId = address.id;
                        that.addressText = address.address + address.detail;
                    },
                },
            });
        },
        changeInsuranceState(state) {
            if (state) {
                this.total += this.insurancePrice;
            } else {
                this.total -= this.insurancePrice;
            }
            this.insurance = state;
        },
        pay() {
            if (!this.isAgree) {
                uni.showToast({
                    title: "请先阅读并同意《服务协议》《隐私政策》",
                    icon: "none",
                });
                return;
            }
            this.$models.order
                .createOrder({
                    ids: this.orderData,
                    addid: this.addressId,
                    times: this.datetime,
                    baoxian: this.insurance ? 1 : 0,
                    desc: this.content,
                })
                .then((id) => {
                    this.$models.order.payOrder(id);
                })
                .catch((e) => {
                    this.$utils.toast(e);
                });
        },
    },
};
</script>

<style lang="less" scoped>
.tips-container {
    width: 710rpx;
    margin: 0 auto;
}
.create-order-container {
    width: 100%;
    margin-top: 16rpx;
    padding-bottom: 120rpx;
}
.service-view {
    margin-top: 16rpx;
    padding: 24rpx 40rpx;
    background-color: #ffffff;
    .service-item {
        margin-top: 28rpx;
    }
    .service-item:first-child {
        margin-top: 0;
    }
}
.insurance-box {
    width: 100%;
}
.demand {
    width: 100%;
    box-sizing: border-box;
    padding: 38rpx 40rpx;
    .title {
        font-size: 30rpx;
        font-weight: bold;
        color: #010101;
    }
    .text-area {
        width: 100%;
        height: 130rpx;
        box-sizing: border-box;
        background-color: #ffffff;
        padding: 24rpx;
        font-size: 26rpx;
        color: #333333;
        margin-top: 12rpx;
        border-radius: 10rpx;
    }
}
.agreement-container {
    width: 100%;
    box-sizing: border-box;
    padding: 0 40rpx 40rpx 40rpx;
}
.common-bottom-components {
    .price {
        font-size: 42rpx;
        font-weight: bold;
        color: #ec7655;
    }
    .pay {
        width: 223rpx;
        height: 71rpx;
        background: #8194f2;
        border-radius: 31rpx;
        text-align: center;
        .text {
            line-height: 71rpx;
            font-size: 32rpx;
            font-weight: bold;
            color: #ffffff;
        }
    }
}
.common-form-widget-group {
    .widget-item {
        background-color: #ffffff;
    }
}
</style>