<template>
    <view class="container">
        <form @submit="onSubmit">
            <view class="page-block">
                <view class="content">
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>活动名称</text>
                        </view>
                        <view class="desc">
                            <input type="text" name="activity_name" class="input-box" placeholder="请输入活动名称"
                                placeholder-class="desc-placeholder">
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>码模式</text>
                        </view>
                        <view class="desc">
                            <picker :range="codeModelList" range-key="name" @change="onChooseCode">
                                <input type="text" :value="activeCode.name" class="input-box" placeholder="请选择码模式"
                                    placeholder-class="desc-placeholder" disabled>
                            </picker>
                            <input type="text" :value="activeCode.code" name="code_mode"
                                style="position: fixed;left:10000px;top:-10000px;" />
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>卡券类型</text>
                        </view>
                        <view class="desc">
                            <picker :range="cardTypeList" range-key="name" @change="onChooseCard">
                                <input type="text" :value="activeCardType.name" class="input-box" placeholder="请选择卡券类型"
                                    placeholder-class="desc-placeholder" disabled>
                            </picker>
                            <input type="text" :value="activeCardType.code" name="voucher_type"
                                style="position: fixed;left:10000px;top:-10000px;" />
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>发放时间</text>
                        </view>
                        <view class="desc">
                            <uni-datetime-picker v-model="fafangTime" type="datetimerange" rangeSeparator="至" />
                            <view class="tips">券发放结束时间与 券发放开始时间 间隔必须小于等于180天</view>
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>商户品牌名称</text>
                        </view>
                        <view class="desc">
                            <input type="text" name="brand_name" class="input-box" placeholder="请输入商户品牌名称"
                                placeholder-class="desc-placeholder">
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>商户logo</text>
                        </view>
                        <view class="desc">
                            <image :src="baseURL+logo.brand_logo" mode="aspectFit" class="logo-img"
                                v-if="logo.brand_logo" @click="uploadLogo"></image>
                            <image src="@/static/images/upimg.png" mode="aspectFit" class="logo-img" v-else
                                @click="uploadLogo"></image>
                            <view class="tips">上传图片尺寸600*600，支持格式：png、jpg、jpeg、bmp，大小不超过2MB</view>
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>券详细使用说明</text>
                        </view>
                        <view class="desc">
                            <input type="text" name="voucher_description" class="input-box" placeholder="请输入券详细使用说明"
                                placeholder-class="desc-placeholder">
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>券使用时间</text>
                        </view>
                        <view class="desc">
                            <picker :range="useTimeList" range-key="name" @change="onChooseUseTime">
                                <input type="text" :value="activeUseTime.name" class="input-box" placeholder="请选择券使用时间"
                                    placeholder-class="desc-placeholder" disabled>
                            </picker>
                            <input type="text" :value="activeUseTime.code" name="type"
                                style="position: fixed;left:10000px;top:-10000px;" />
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>有效期</text>
                        </view>
                        <view class="desc">
                            <uni-datetime-picker v-model="youxiaoTime" type="datetimerange" rangeSeparator="至" />
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>卡券面额</text>
                        </view>
                        <view class="desc">
                            <input type="text" name="amount" class="input-box" placeholder="请输入卡券面额"
                                placeholder-class="desc-placeholder">
                            <view class="tips">面额，每张代金券可以抵扣的金额。限制：币种为人民币，单位为元。小数点以后最多保留两位。取值范围:0.1 ~ 3000</view>
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>门槛金额</text>
                        </view>
                        <view class="desc">
                            <input type="text" name="floor_amount" class="input-box" placeholder="请输入门槛金额"
                                placeholder-class="desc-placeholder">
                            <view class="tips">门槛金额。该字段不填写，认为无门槛。 限制： 只有当订单金额大于等于门槛金额时券才能使用。 币种为人民币，单位为元。小数点以后最多保留两位。
                                设置门槛：取值范围:0.1 ~ 50000。 如果设置了门槛金额，则必须大于等于0.1，不可以等于0</view>
                        </view>
                    </view>
                    <view class="cell flex  box-pack-between">
                        <view class="title-block required">
                            <text>发行券数量</text>
                        </view>
                        <view class="desc">
                            <input type="number" name="voucher_quantity" class="input-box" placeholder="请输入发行券数量"
                                placeholder-class="desc-placeholder">
                            <view class="tips">必须是大于0的整数</view>
                        </view>
                    </view>
                </view>
            </view>
            <view class="footer-bar">
                <view class="content flex flex-center" style="background: #fff;">
                    <!-- <view class="btn">立即添加</view> -->
                    <button class="btn" hover-class="none" form-type="submit">
                        立即添加
                    </button>
                </view>
            </view>
        </form>
    </view>
</template>

<script>
    var siteinfo = require('@/siteinfo.js'); //require这个js模块  
    export default {
        data() {
            return {
                store_id: "",
                admin_id: "",
                codeModelList: [{
                    name: '自定义',
                    code: 'MERCHANT_UPLOAD'
                }],
                activeCode: {},
                cardTypeList: [{
                    name: "满减券",
                    code: "FIX_VOUCHER"
                }],
                activeCardType: {},
                useTimeList: [{
                    name: "绝对时间",
                    code: "ABSOLUTE"
                }],
                activeUseTime: {},
                fafangTime: [],
                youxiaoTime: [],
                logo: {
                    brand_logo: "",
                    brand_logo_resource_id: "",
                },
                baseURL: siteinfo.siteroot
            }
        },
        onLoad(e) {

            this.store_id = uni.getStorageSync("store_id");
            this.admin_id = uni.getStorageSync("admin_id")

        },
        methods: {
            onChooseCode(e) {
                this.activeCode = this.codeModelList[e.detail.value]
            },
            onChooseCard(e) {
                this.activeCardType = this.cardTypeList[e.detail.value]
            },
            onChooseUseTime(e) {
                this.activeUseTime = this.useTimeList[e.detail.value]
            },
            // 开始时间
            bindDateChangeStart1(e) {
                this.startDates = e.target.value;
            },
            //结束时间
            bindDateChangeStart2(e) {
                this.endDates = e.target.value;
            },
            // 开始时间
            bindDateChangeStart3(e) {
                this.startDates2 = e.target.value;
            },
            //结束时间
            bindDateChangeStart4(e) {
                this.endDates2 = e.target.value;
            },
            //上传logo
            uploadLogo() {
                uni.chooseMedia({
                    count: 1,
                    mediaType: ["image"],
                    success: r => {
                        console.log(r)
                        let tempFilePath = r.tempFiles[0].tempFilePath
                        wx.showLoading({
                            title:"上传中..."
                        })
                        uni.uploadFile({
                            url: siteinfo.siteroot +
                                '/index.php?s=/api/alipaycoupon/uplmarketingad',
                            filePath: tempFilePath,
                            name: 'file',
                            formData: {
                                admin_id: this.admin_id,
                                type: "PROMO_BRAND_LOGO"
                            },
                            success: (res) => {
                                console.log(res);
                                 wx.hideLoading()
                                if (res.statusCode == 200) {
                                    let data = JSON.parse(res.data)
                                    console.log(data)
                                    if (data.code == 200) {
                                        this.logo = {
                                            brand_logo: data.data.src,
                                            brand_logo_resource_id: data.data.resource_id
                                        }
                                    } else {
                                        uni.showToast({
                                            title: data.message,
                                            icon: "none"
                                        })
                                    }
                                } else {
                                    uni.showToast({
                                        title: "上传失败请重试",
                                        icon: "none"
                                    })
                                }
                            },
                            fail:res=>{
                                wx.hideLoading()
                            }
                        });
                    }
                })
            },
            onSubmit(e) {
                console.log(e)
                let params = {
                    admin_id: this.admin_id,
                    publish_start_time: this.fafangTime[0],
                    publish_end_time: this.fafangTime[1],
                    brand_logo: this.logo.brand_logo,
                    brand_logo_resource_id: this.logo.brand_logo_resource_id,
                    valid_begin_time: this.youxiaoTime[0],
                    valid_end_time: this.youxiaoTime[1],
                }
                params = Object.assign(params, e.detail.value)
                console.log(params)
                let errMsg = ""
                if (!params.activity_name) {
                    errMsg = "请输入活动名称"
                } else if (!params.code_mode) {
                    errMsg = "请选择码模式"
                } else if (!params.voucher_type) {
                    errMsg = "请选择卡券类型"
                } else if (!params.publish_start_time || !params.publish_end_time) {
                    errMsg = "请选择发放时间"
                } else if (!params.brand_name) {
                    errMsg = "请输入商户品牌名称"
                } else if (!params.brand_logo || !params.brand_logo_resource_id) {
                    errMsg = "请上传商户logo"
                } else if (!params.voucher_description) {
                    errMsg = "请输入券详细使用说明"
                } else if (!params.type) {
                    errMsg = "请选择券使用时间"
                } else if (!params.valid_begin_time || !params.valid_end_time) {
                    errMsg = "请选择有效期"
                } else if (!params.amount) {
                    errMsg = "请输入卡券面额"
                } else if (!params.floor_amount) {
                    errMsg = "请输入门槛金额"
                } else if (!params.voucher_quantity) {
                    errMsg = "请输入发行券数量"
                }
                if (errMsg) {
                    uni.showToast({
                        title: errMsg,
                        icon: 'none'
                    })
                    return false
                }
                this.request({
                    url: "alipaycoupon/edit_merchant_coupon",
                    data: params
                }).then(res => {
                    if (res.code == 200) {
                        uni.showToast({
                            title: res.message,
                            icon: 'none'
                        })
                        setTimeout(() => {
                            uni.navigateBack()
                        }, 2000)
                    }
                }).catch((res) => {
                    uni.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                });
            },


        }
    }
</script>

<style>
    @import url("../static/css/common.css");

    page {
        background-color: #F4F5F7;
    }

    .timeSelete-left {
        margin: 0 20rpx;
    }

    .timeSelete {
        font-size: 28rpx;
    }

    .desc {
        text-align: right;
    }

    .footer-bar {
        height: 200rpx;
    }

    .footer-bar .content {
        z-index: 1
    }

    .page-block .cell {
        min-height: 60rpx;
        padding: 20rpx 0;
    }

    .page-block .cell .title-block {
        line-height: 60rpx;
    }

    .page-block .cell .input-box {
        height: 60rpx;
        line-height: 60rpx;
    }

    .tips {
        font-size: 24rpx;
        color: #f00;
        text-align: left;
        margin-top: 10rpx;
    }

    .logo-img {
        display: inline-block;
        width: 160rpx;
        height: 160rpx;
    }
</style>
