<template>
    <app-layout title="确认订单">
        <view class="tips-container">
            <widget-tips text="保险公司承保，人身财产，双重保障，下单无忧！" />
        </view>
        <view class="create-order-container">
            <view class="common-form-container">
                <view class="input-item">
                    <view class="title-box">
                        <text>服务地址</text>
                    </view>
                    <view class="input-box map" @click="chooseLocation">
                        <view class="address-text limit-line clamp-1" :class="{ active: address.text }">
                            {{ address.text ? address.text : "点击选择服务地址" }}
                        </view>
                        <text class="iconfont icon-dingwei"></text>
                    </view>
                </view>
                <view class="input-item">
                    <view class="title-box">
                        <text>门牌号</text>
                    </view>
                    <view class="input-box">
                        <input
                            class="input"
                            v-model="address.detail"
                            placeholder="请输入详细地址，如8栋5单元18a"
                            placeholder-class="placeholder-style-3"
                        />
                    </view>
                </view>
                <view class="input-item">
                    <view class="title-box">
                        <text>联系人</text>
                    </view>
                    <view class="input-box">
                        <input
                            class="input"
                            v-model="address.name"
                            placeholder="请输入姓名"
                            placeholder-class="placeholder-style-3"
                        />
                    </view>
                </view>
                <view class="input-item">
                    <view class="title-box">
                        <text>手机号码</text>
                    </view>
                    <view class="input-box">
                        <input
                            class="input"
                            v-model="address.mobile"
                            placeholder="请输入手机号码"
                            placeholder-class="placeholder-style-3"
                        />
                    </view>
                </view>
            </view>
            <view class="common-form-widget-group">
                <view class="widget-item">
                    <text class="iconfont icon-shijian icon"></text>
                    <picker mode="date" @change="bindDateChange" class="picker">
                        <text class="title">
                            <text class="text limit-line clamp-1">
                                {{ date ? date : "选择期望上门日期" }}
                            </text>
                        </text>
                    </picker>
                    <text class="iconfont icon-jinru more"></text>
                </view>
                <view class="widget-item">
                    <text class="iconfont icon-shijian icon"></text>
                    <picker mode="time" @change="bindTimeChange" class="picker">
                        <text class="title">
                            <text class="limit-line clamp-1">
                                {{ time ? time : "选择期望上门时间" }}
                            </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" />
                    <text class="count">x{{ item.number }}</text>
                </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>
        <view class="common-bottom-components" :style="{ paddingBottom: 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 WidgetTips from "@/components/widgets/tips";
import ServiceInsurance from "@/components/service/insurance";
import { mapGetters, mapState } from "vuex";
export default {
    name: "order-create",
    data() {
        return {
            utils: this.$utils,
            pageConfig: {},
            address: {
                lng: 0.0,
                lat: 0.0,
                text: "",
                detail: "",
                name: "",
                mobile: "",
            },
            date: "",
            time: "",
            content: "",
            insurance: false,
            total: 0,
            orderData: [],
            serviceList: [],
        };
    },
    components: {
        AppLayout,
        ServicePreviewItem,
        WidgetTips,
        ServiceInsurance,
    },
    computed: {
        ...mapState({
            insurancePrice: (state) => state.service.insurancePrice,
        }),
        ...mapGetters({
            isLogin: "user/isLogin",
        }),
    },
    onLoad(e) {
        this.serviceList = [];
        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,
                    number: item.count,
                });
                this.total += parseFloat(response.goods.money * item.count);
            });
        });
    },
    onShow() {
        if (!this.isLogin) {
            this.$store.commit("user/showLoginModal", true);
        }
    },
    onReady() {},
    onReachBottom() {},
    onPullDownRefresh() {},
    onShareTimeline() {},
    onShareAppMessage() {},
    methods: {
        bindDateChange(e) {
            this.date = e.detail.value;
        },
        bindTimeChange(e) {
            this.time = e.detail.value;
        },
        /**
         * 选择地址
         */
        chooseLocation() {
            const that = this;
            uni.chooseLocation({
                success({ name, address, longitude, latitude }) {
                    that.address.text = address;
                    that.address.detail = name;
                    that.address.lng = longitude;
                    that.address.lat = latitude;
                },
                fail(error) {},
            });
        },
        /**
         * 保险开关
         */
        changeInsuranceState(state) {
            if (state) {
                this.total += this.insurancePrice;
            } else {
                this.total -= this.insurancePrice;
            }
            this.insurance = state;
        },
        /**
         * 下单支付
         */
        pay() {
            if (this.address.text == "" || this.address.detail == "") {
                return this.$utils.toast("请完善地址信息");
            }
            if (this.address.name == "") {
                return this.$utils.toast("请填写联系人");
            }
            if (!this.$test.mobile(this.address.mobile)) {
                return this.$utils.toast("请填写正确的手机号码");
            }
            if (!this.date || !this.time) {
                return this.$utils.toast("请选择时间和日期");
            }
            let datetime = this.date + " " + this.time;
            this.$models.order
                .createOrder({
                    ids: this.orderData,
                    address: {
                        address: this.address.text,
                        doorplate: this.address.detail,
                        name: this.address.name,
                        mobil: this.address.mobile,
                        lng: this.address.lng,
                        lat: this.address.lat,
                    },
                    times: datetime,
                    baoxian: this.insurance ? 1 : 0,
                    desc: this.content,
                })
                .then((id) => {
                    this.$store.dispatch("cart/update", true);
                    this.$store.commit("system/currentOrderTabIndex", 0);
                    this.$store.commit("system/refreshOrder", true);
                    this.$models.order
                        .pay({
                            request: {
                                api: "order.pay.t1",
                                data: {
                                    id: id,
                                },
                            },
                        })
                        .then(() => {
                            this.$utils.toPage("/pages/order/order", {}, "switch");
                        })
                        .catch(() => {
                            this.$utils.toPage("/pages/order/order", {}, "switch");
                        });
                })
                .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 {
        position: relative;
        margin-top: 28rpx;
        .count {
            position: absolute;
            right: 0;
            bottom: 0;
            color: #333333;
            font-size: 28rpx;
            line-height: 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;
    }
}
.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;
    }
}
.common-form-container {
    width: 100%;
    .title-box {
        font-weight: normal;
    }
    .input-box.map {
        .address-text {
            font-size: 28rpx;
            line-height: 38rpx;
            color: #c9c9c9;
            box-sizing: border-box;
            padding-right: 40rpx;
        }
        .address-text.active {
            color: #000000;
        }
        .iconfont {
            position: absolute;
            top: 28rpx;
            right: 0;
            font-size: 35rpx;
            color: #8194f2;
        }
    }
}
</style>