<template>
    <div class="page" :class="{ 'page--iphoneX': iphoneX }">
        <div class="form-card">
            <section class="form-item">
                <div class="label">到店自提设置：</div>
                <div class="box"></div>
            </section>
            <section class="form-item">
                <div class="label">到店自提：</div>
                <div class="box">
                    <switch
                        :value="true"
                        :checked="form.deliveryMethod.storePickUp"
                        @change="switchChange($event, 'storePickUp')"
                    ></switch>
                </div>
            </section>
            <section class="form-item" v-if="form.deliveryMethod.storePickUp">
                <div class="label">最低自提下单金额：</div>
                <div class="box">
                    <input type="number" v-model="form.pickupMinAmount" />
                </div>
                <div class="tool">元</div>
            </section>

            <section class="form-item">
                <div class="label">送货上门设置：</div>
                <div class="box"></div>
            </section>
            <section class="form-item">
                <div class="label">送货上门：</div>
                <div class="box">
                    <switch
                        :value="true"
                        :checked="form.deliveryMethod.homeDelivery"
                        @change="switchChange($event, 'homeDelivery')"
                    ></switch>
                </div>
            </section>
            <block v-if="form.deliveryMethod.homeDelivery">
                <section class="form-item">
                    <div class="label">
                        <!-- <span class="required">*</span> -->
                        最低起送金额：
                    </div>
                    <div class="box">
                        <input
                            type="number"
                            v-model="form.deliveryMinAmount"
                        />
                    </div>
                    <div class="tool">元</div>
                </section>
                <section class="form-item">
                    <div class="label">
                        <!-- <span class="required">*</span> -->
                        最远配送距离：
                    </div>
                    <div class="box">
                        <input
                            type="number"
                            v-model="form.deliveryDistance"
                            placeholder="距离门店步行距离"
                        />
                    </div>
                    <div class="tool">公里</div>
                </section>
                <section class="form-item">
                    <div class="label">
                        <!-- <span class="required">*</span> -->
                        配送费：
                    </div>
                    <div class="box">
                        <input
                            type="digit"
                            v-model="form.deliveryFee"
                            placeholder="按单收取配送费"
                        />
                    </div>
                    <div class="tool">元</div>
                </section>
                <section class="form-item">
                    <div class="label">
                        <!-- <span class="required">*</span> -->
                        打包费：
                    </div>
                    <div class="box">
                        <input
                            type="digit"
                            v-model="form.packageFee"
                            placeholder="按单收取打包费"
                        />
                    </div>
                    <div class="tool">元</div>
                </section>
            </block>
        </div>
        <div class="btns">
            <div class="btn" @click="saveOk">保存</div>
        </div>
    </div>
</template>

<script>
import MyImage from "../../../components/image/index";

export default {
    name: "",
    components: { MyImage },
    // 数据
    data() {
        return {
            form: {
                deliveryMethod:{
                    storePickUp:false, // 自提
                    homeDelivery:false  // 上门
                },
                pickupMinAmount:0,  // 最低自提金额

                deliveryMinAmount: 0, // 最低起送金额，
                deliveryDistance: 0,   // 最远配送距离
                deliveryFee: 0, // 配送费
                packageFee: 0,  // 打包费
            },
            submitLoading: false,
        };
    },

    onLoad(opts) {
        this.getData();
    },

    onShow() {},

    // 函数
    methods: {
        getData() {
            this.api.get("/Shop/GetDeliveryConfig", {}, { pass: true }).then((res) => {
                if (res.success && res.data) {
                    this.form.deliveryMethod = res.data.deliveryMethod;
                    this.form.pickupMinAmount = res.data.pickupMinAmount/100;
                    this.form.deliveryMinAmount = res.data.deliveryMinAmount/100;
                    this.form.deliveryDistance = res.data.deliveryDistance/1000;
                    this.form.deliveryFee = res.data.deliveryFee/100;
                    this.form.packageFee = res.data.packageFee/100;

                    this.form = { ...this.form };
                }
            });
        },
        switchChange(e, name) {
            let v = e.detail.value;
            this.form.deliveryMethod[name] = v ? true : false;
            this.form = { ...this.form };
        },
        saveOk() {
            if (this.submitLoading) {
                return;
            }
            if (!this.form.deliveryMethod.storePickUp && !this.form.deliveryMethod.homeDelivery){
                return this.fn.showToast("至少选择一种方式");
            }
            
            this.submitLoading = true;
            uni.showLoading({
                title: "提交中...",
            });
            let data = { ...this.form };
            if(data.pickupMinAmount){
                data.pickupMinAmount*=100;
                data.pickupMinAmount = parseInt(data.pickupMinAmount);
            }
            if(data.deliveryMinAmount){
                data.deliveryMinAmount*=100;
                data.deliveryMinAmount = parseInt(data.deliveryMinAmount);
            }
            if(data.deliveryDistance){
                data.deliveryDistance*=1000;
                data.deliveryDistance = parseInt(data.deliveryDistance);
            }
            if(data.deliveryFee){
                data.deliveryFee*=100;
                data.deliveryFee = parseInt(data.deliveryFee);
            }
            if(data.packageFee){
                data.packageFee*=100;
                data.packageFee = parseInt(data.packageFee);
            }
            this.api
                .post("/Shop/SetDeliveryConfig", data, { pass: true })
                .then((res) => {
                    this.submitLoading = false;
                    uni.hideLoading();
                    if (res.success) {
                        this.fn.showToast("设置成功");
                        this.router.back();
                    } else {
                        this.fn.showModal({
                            title: "设置错误",
                            content: res.message,
                            showCancel: false,
                        });
                    }
                });
        },
    },

    // 数据计算
    computed: {
        user() {
            return this.$store.state.user.user;
        },
    },

    // 数据监听
    watch: {},
};
</script>


<style lang="scss" scoped>
.page {
    padding-bottom: px(120);
    min-height: 100vh;
    background-color: #fff;
}
.page--iphoneX {
    padding-bottom: px(150);
    .btn {
        padding-bottom: px(30);
        background-color: rgb(0, 188, 38);
    }
}
.form-card {
    background-color: #fff;
    padding: 0 px(30);
    & ~ .form-card {
        margin-top: px(25);
    }
    .head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: px(140);
        background-color: #fbfbfb;
        border-bottom: 1px solid #f1f1f1;
        border-top: 1px solid #f1f1f1;
        font-size: px(44);
    }
}
.form-item {
    display: flex;
    min-height: px(140);
    padding: px(30) 0;
    box-sizing: border-box;
    align-items: center;
    font-size: px(44);
    border-top: 1px solid #f1f1f1;
    &.dis-item {
        color: #999;
        background: #f9f9f9;
    }
    .label {
        // width: px(400);
        flex-shrink: 0;
        color: #666;
        margin-right: px(20);
    }
    .box {
        width: 100%;
        position: relative;
        input {
            width: 100%;
        }
    }
    .tool {
        flex-shrink: 0;
        color: #999;
        margin-left: px(10);
    }
    textarea,
    input {
        max-width: 100%;
        width: 100%;
    }
}
.list {
    // min-height: 100vh;
    // background-color: #fff;
    // border-bottom: 1px solid #f1f1f1;
    li {
        padding: px(0) px(35);
        height: px(140);
        line-height: px(140);
        font-size: px(44);
        border-top: 1px solid #f1f1f1;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .tools {
        display: flex;
        align-items: center;
        flex-shrink: 0;
        span {
            display: inline-block;
            border: 1px solid #f1f1f1;
            font-size: px(36);
            height: px(80);
            width: px(120);
            display: flex;
            justify-content: center;
            align-items: center;
            & ~ span {
                margin-left: px(30);
            }
        }
        .remove {
            color: #f00;
        }
    }
}
.btn {
    width: 100%;
    height: px(120);
    background-color: rgb(0, 188, 38);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.btns {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
}
.layout {
    background-color: #eee;
    color: #666;
}
.add-btn {
    height: px(120);
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: px(50) px(35) 0;
    border: 1px solid #ccc;
    border-radius: px(10);
}
.code-tool {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .scan-img {
        flex-shrink: 0;
        width: px(80);
        height: px(80);
        /deep/ img {
            width: px(80);
            height: px(80);
        }
    }
    .generate {
        color: #0097d1;
        margin-right: px(30);
        flex-shrink: 0;
    }
}
.required {
    color: #f00;
    font-size: px(44);
    &.hide {
        visibility: hidden;
    }
}
.inp-dis {
    color: #999;
}

.img-card {
    padding-top: px(30);
    padding-bottom: px(30);
}
.form-img-item {
    .tit {
        padding: px(30);
        font-size: px(44);
    }
}

.spec-list {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    li {
        width: 33.333333%;
        font-size: px(40);
        height: px(100);
        border: 1px solid #f1f1f1;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #999;
        & ~ li {
            // border-left: none;
        }
        &.on {
            color: rgb(0, 188, 38);
            border-color: rgb(0, 188, 38);
        }
    }
}
.mo-date {
    margin-top: px(20);
    border-bottom: 1px solid #f1f1f1;
    background: #fbfbfb;
    font-size: px(40);
    padding: px(10);
}
.n-date-box {
    margin-top: px(20);
    font-size: px(40);
}
.n-date {
    border-bottom: 1px solid #f1f1f1;
    background: #fbfbfb;
    padding: 0 px(20);
    display: inline-block;
    width: px(200);
    font-size: px(44);
    vertical-align: bottom;
}
.address-icon {
    /deep/ img {
        width: px(60);
        height: px(60);
    }
}
.dis {
    background-color: #f6f6f6;
}
.tip {
    color: #999;
    margin-left: px(10);
}
</style>