<template>
    <div class="page" :class="{ 'page--iphoneX': iphoneX }">
        <div class="form-card">
            <section class="form-item dis-item">
                <div class="label">
                    <span class="required">*</span>
                    活动名称：
                </div>
                <div class="box">
                    <input
                        type="text"
                        class="inp-dis"
                        v-model="form.activityName"
                        :disabled="true"
                    />
                </div>
            </section>

            <section class="form-item dis-item">
                <div class="label">
                    <span class="required hide">*</span>
                    活动备注：
                </div>
                <div class="box">
                    <input
                        type="digit"
                        v-model="form.activityRemark"
                        :disabled="true"
                    />
                </div>
                <div class="tool">
                    <!-- <span class="">元</span> -->
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    开始时间：
                </div>
                <div class="box">
                    <date-time-picker
                        @change="pickerDateChange('startReceiveTime', $event)"
                        :end="pickerDateRange.endReceiveTime"
                        placeholder="请选择"
                        :defaultValue="form.startReceiveTime"
                    ></date-time-picker>
                </div>
                <div class="tool">
                    <!-- <span class="">元</span> -->
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required">*</span>
                    结束时间：
                </div>
                <div class="box">
                    <date-time-picker
                        @change="pickerDateChange('endReceiveTime', $event)"
                        :start="pickerDateRange.startReceiveTime"
                        placeholder="请选择"
                        :defaultValue="form.endReceiveTime"
                    ></date-time-picker>
                </div>
                <div class="tool">
                    <!-- <span class="">元</span> -->
                </div>
            </section>

            <section class="form-item">
                <div class="label">
                    <span class="required hide hide">*</span>
                    数量：
                </div>
                <div class="box">
                    <input type="digit" v-model="form.total" />
                </div>
            </section>
            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    发放方式：
                </div>
                <div class="box">
                    <picker
                        :mode="'selector'"
                        :range="opts.grantMethod"
                        :range-key="'key'"
                        @change="pickerChange('grantMethod', $event)"
                    >
                        <input
                            type="text"
                            :disabled="true"
                            :value="pickerView('grantMethod', form.grantMethod)"
                            placeholder="请选择"
                        />
                    </picker>
                </div>
                <div class="tool"></div>
            </section>

            <section class="form-item">
                <div class="label">
                    <span class="required hide">*</span>
                    进行状态：
                </div>
                <div class="box">
                    <picker
                        :mode="'selector'"
                        :range="opts.status"
                        :range-key="'key'"
                        @change="pickerChange('status', $event)"
                    >
                        <input
                            type="text"
                            :disabled="true"
                            :value="pickerView('status', form.status)"
                            placeholder="请选择"
                        />
                    </picker>
                </div>
                <div class="tool"></div>
            </section>

            <section class="form-item dis-item" stype>
                <div class="label">
                    <span class="required hide">*</span>
                    使用有效期：
                </div>
                <div class="box">
                    <div v-if="form.couponTime === 0">
                        <div>
                            {{ form.startReceiveTime }} ——
                            {{ form.endReceiveTime }}
                        </div>
                    </div>
                    <div v-if="form.couponTime === 1">
                        <div>
                            {{ form.startUseTime }} ——
                            {{ form.endUseTime }}
                        </div>
                    </div>
                    <div v-if="form.couponTime === 2">
                        <div>领券后{{ form.expiredDays }}天有效</div>
                    </div>
                </div>
            </section>
            <section class="form-item dis-item">
                <div class="label">
                    <span class="required hide">*</span>
                    重复领取：
                </div>
                <div class="box">
                    {{ pickerView("repeatClaim", form.repeatClaim) }}
                </div>
                <div class="tool"></div>
            </section>
            <section class="form-item dis-item">
                <div class="label">
                    <span class="required hide">*</span>
                    活动范围：
                </div>
                <div class="box">
                    {{ pickerView("isCurrency", form.isCurrency) }}
                </div>
            </section>
            <section class="form-item dis-item">
                <div class="label">
                    <span class="required hide">*</span>
                    面向人群：
                </div>
                <div class="box">
                    {{ pickerView("facingForCrowd", form.facingForCrowd) }}
                </div>
            </section>
            <section class="form-item dis-item">
                <div class="label">
                    <span class="required hide">*</span>
                    支持赠送：
                </div>
                <div class="box">
                    {{ pickerView("giveToFriends", form.giveToFriends) }}
                </div>
            </section>
        </div>
        <div class="btn" @click="saveOk">保存</div>
    </div>
</template>

<script>
import MyImage from "../../../components/image/index";
import DateTimePicker from "../../../components/biaofun-datetime-picker/biaofun-datetime-picker";

export default {
    name: "",
    components: { MyImage, DateTimePicker },
    // 数据
    data() {
        return {
            form: {
                activityName: "",
                activityRemark: "",
                startReceiveTime: "",
                endReceiveTime: "",
                couponTime: 0, //用券时间 0-同步发券时间，1-自定义时间，2-领取成功后多少天内有效
                startUseTime: "", // -自定义时间使用
                endUseTime: "", // -自定义时间使用
                expiredDays: 0, // 领取成功后多少天内有效使用
                grantMethod: 0,
                repeatClaim: true,
                isCurrency: true,
                facingForCrowd: 0,
                giveToFriends: true,
            },
            pickerDateRange: {
                startReceiveTime: "",
                endReceiveTime: "",
                startUseTime: "",
                endUseTime: "",
            },
            opts: {
                grantMethod: [
                    { key: "公开发放", value: 0 },
                    { key: "自行发放", value: 1 },
                ],
                repeatClaim: [
                    { key: "可以", value: true },
                    { key: "不可以", value: false },
                ],
                isCurrency: [
                    { key: "全店商品", value: true },
                    { key: "部分商品", value: false },
                ],
                facingForCrowd: [
                    { key: "所有用户", value: 0 },
                    { key: "所有会员", value: 2 },
                    { key: "新人专享", value: 3 },
                ],
                giveToFriends: [
                    { key: "支持", value: true },
                    { key: "不支持", value: false },
                ],
                status: [
                    { key: "未开始", value: 0 },
                    { key: "进行中", value: 1 },
                    { key: "已结束", value: 2 },
                ],
            },
            oldData: {},
        };
    },

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

    onShow() {},

    // 函数
    methods: {
        getData(id) {
            uni.showLoading({
                title: "加载中...",
                mask: true,
            });
            this.api.get("/Coupon/GetCoupon", { id }).then((res) => {
                uni.hideLoading();
                this.form = { ...res.data };
                this.oldData = res.data;
            });
        },
        couponTimeClick(v) {
            this.form.couponTime = v;
            switch (v) {
                case 0:
                    this.startUseTime = "";
                    this.endUseTime = "";
                    this.expiredDays = "";
                    break;
                case 1:
                    this.expiredDays = "";
                    break;
                case 2:
                    this.startUseTime = "";
                    this.endUseTime = "";
                    break;
            }
        },

        pickerChange(name, e) {
            let index = e.detail.value;
            this.form[name] = this.opts[name][index].value;
        },
        pickerDateChange(name, e) {
            console.log(e);
            this.form[name] =e.f3;
            this.pickerDateRange[name] = e.f3;
        },
        pickerView(name, v) {
            for (let item of this.opts[name]) {
                if (item.value === v) {
                    return item.key;
                }
            }
        },

        saveOk() {
            if (!this.form.total) {
                return this.fn.showToast("请输入数量");
            }

            let data = { ...this.form };
            this.api
                .post("/Coupon/EditCoupon", data, { pass: true })
                .then((res) => {
                    this.submitLoading = false;
                    uni.hideLoading();
                    if (res.success) {
                        this.fn.showToast("保存成功");
                        this.router.back();
                    } 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);
    &.dis-item {
        color: #999;
        background: #f9f9f9;
    }
    & ~ .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);
    }
}

.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;
}
</style>