<template>
    <div class="page" :class="{ 'page--iphoneX': iphoneX }">
        <div class="form-card">
            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    满减券
                </div>
                <div class="box">
                    <!-- <input type="text" class="inp-dis" v-model="form.key" /> -->
                </div>
            </section>

            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    无门槛：
                </div>
                <div class="box">
                    <switch
                        :checked="form.noThreshold"
                        @change="noThresholdChange"
                    ></switch>
                </div>
                <div class="tool"></div>
            </section>

            <section class="form-item" v-if="form.noThreshold === false">
                <div class="label">
                    <span class="required hide">*</span>
                    满：
                </div>
                <div class="box">
                    <input type="digit" v-model="form.minimumAmount" />
                </div>
                <div class="tool">
                    <span class="">元</span>
                </div>
            </section>

            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    减：
                </div>
                <div class="box">
                    <input type="digit" v-model="form.value" />
                </div>
                <div class="tool">
                    <span class="">元</span>
                </div>
            </section>

            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    发行数量：
                </div>
                <div class="box">
                    <input type="digit" v-model="form.total" />
                </div>
                <div class="tool">
                    <span class="">张</span>
                </div>
            </section>
        </div>
        <div class="btn" @click="saveOk">提交</div>
    </div>
</template>

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

export default {
    name: "",
    components: { MyImage },
    // 数据
    data() {
        return {
            couponData: {},
            form: {
                type: 0,
                noThreshold: false,
                minimumAmount: null,
                value: null,
                total: null,
            },
        };
    },

    onLoad() {},

    onShow() {
        if (this.$store.state.common.couponData) {
            this.couponData = { ...this.$store.state.common.couponData };

            this.$store.commit("common/update", {
                couponData: null,
            });
        } else {
            this.router.back();
        }
    },

    // 函数
    methods: {
        noThresholdChange(e) {
            this.form.noThreshold = e.detail.value;
            if (this.form.noThreshold) {
                this.minimumAmount = null;
            }
        },
        saveOk() {
            if (this.submitLoading) {
                return;
            }
            if (this.form.noThreshold === true) {
                if (!this.form.value) {
                    return this.fn.showToast("请输入优惠金额");
                }
            } else {
                if (!this.form.minimumAmount) {
                    return this.fn.showToast("请输入门槛金额");
                }
                if (!this.form.value) {
                    return this.fn.showToast("请输入优惠金额");
                }
                if (
                    Number(this.form.minimumAmount) <= Number(this.form.value)
                ) {
                    return this.fn.showToast("优惠金额必须小于门槛金额");
                }
            }
            if (!this.form.total) {
                return this.fn.showToast("请输入发行数量");
            }
            let sendData = {
                ...this.couponData,
                ...this.form,
            };
            if (this.form.noThreshold === true) {
                delete sendData.minimumAmount;
            } else {
                sendData.minimumAmount = parseInt(sendData.minimumAmount * 100);
                sendData.value = parseInt(sendData.value * 100);
                sendData.total = parseInt(sendData.total);
            }

            this.submitLoading = true;
            uni.showLoading({
                title: "提交中...",
                mask: true,
            });
            console.log(sendData);
            this.api
                .put("/Coupon/AddCoupon", sendData, { pass: true })
                .then((res) => {
                    this.submitLoading = false;
                    uni.hideLoading();
                    if (res.success) {
                        this.fn.showToast("保存成功");
                        this.router.go(2);
                    } else {
                        this.fn.showModal({
                            content: res.message,
                            showCancel: false,
                        });
                    }
                });
        },
    },

    // 数据计算
    computed: {},

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


<style lang="scss" scoped>
.page {
    padding-bottom: px(120);
    min-height: 100vh;
}
.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);
    & ~ .form-item {
        border-top: 1px solid #f1f1f1;
    }
    .label {
        width: px(240);
        flex-shrink: 0;
        color: #999;
        margin-right: px(10);
    }
    .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 {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: px(120);
    background-color: rgb(0, 188, 38);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.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);
    }
}
</style>