<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['activity:couponInfo:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/activity/couponInfo/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="序号" align="center" prop="id" width="80" />
            <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
            <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
            <el-table-column label="宣传图片" align="center" prop="imgUrl" width="100">
                <template slot-scope="scope">
                    <image-preview :src="scope.row.imgUrl" :width="80" :height="80" />
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="100">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="开始时间" align="center" prop="startDate" width="120">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="endDate" width="120">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="活动价格" align="center" prop="price" width="100" />
            <el-table-column label="可购买张数" align="center" prop="maxBuyNum" width="100" />
            <el-table-column label="已选择券数量" align="center" prop="couponNum" width="120">
                <template slot-scope="scope">
                    <el-button type="text" @click="handleSetCouponItems(scope.row)">已选{{ scope.row.couponNum }}张</el-button>
                </template>
            </el-table-column>
            <el-table-column label="适用用户" align="center" prop="userScope" width="120">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.activity_user_scope" :value="scope.row.userScope" />
                </template>
            </el-table-column>
            <el-table-column label="适用门店" align="center" prop="storeScope" width="120">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.coupon_use_range" :value="scope.row.storeScope" />
                </template>
            </el-table-column>
            <el-table-column label="展示在app购买页" align="center" prop="isShowAppBuyPage" width="100">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isShowAppBuyPage" />
                </template>
            </el-table-column>
            <el-table-column label="展示在小程序购买页" align="center" prop="isShowWxBuyPage" width="100">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isShowWxBuyPage" />
                </template>
            </el-table-column>
            <el-table-column label="展示在扫码进入购买页" align="center" prop="isShowWxBuyPageFromQrcode" width="100">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isShowWxBuyPageFromQrcode" />
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['activity:couponInfo:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-s-shop" @click="handleSetCouponItems(scope.row)" v-hasPermi="['activity:couponInfo:list']">设置优惠券</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:couponInfo:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改送券活动对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="180px" size="small">
                    <el-col :span="24">
                        <el-divider content-position="left">基本信息</el-divider>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="form.title" placeholder="请输入标题" maxlength="200" show-word-limit />
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item label="描述" prop="description">
                            <el-input v-model="form.description" type="textarea" placeholder="请输入描述" maxlength="500" show-word-limit />
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item label="宣传图片" prop="imgUrl">
                            <image-upload v-model="form.imgUrl" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="内容" prop="content">
                            <editor v-model="form.content" :min-height="192" :height="400" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="有效期" prop="duringDate">
                            <el-date-picker v-model="form.duringDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-divider content-position="left">配置信息</el-divider>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="活动价格" prop="price">
                            <el-input-number v-model="form.price" placeholder="请输入活动价格" :precision="2" :min="0" :max="999999" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="适用用户" prop="userScope">
                            <el-select v-model="form.userScope" placeholder="请选择适用用户范围">
                                <el-option v-for="dict in dict.type.activity_user_scope" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="可购买数量" prop="maxBuyNum">
                            <el-input-number v-model="form.maxBuyNum" placeholder="请输入最大可购买数量" :precision="0" :min="1" :max="999999" />
                        </el-form-item>
                    </el-col>
                    <!--                    <el-col :span="8">-->
                    <!--                        <el-form-item label="单次最多使用券数" prop="maxUseCount">-->
                    <!--                            <el-input-number v-model="form.maxUseCount" placeholder="请输入单次订单最多使用券数" :precision="0" :min="1" :max="999999" />-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->
                    <!--                    <el-col :span="8">-->
                    <!--                        <el-form-item label="可多张使用" prop="canMultiUse">-->
                    <!--                            <el-radio-group v-model="form.canMultiUse">-->
                    <!--                                <el-radio-button v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>-->
                    <!--                            </el-radio-group>-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->
                    <el-col :span="8">
                        <el-form-item label="适用门店" prop="storeScope">
                            <el-radio-group v-model="form.storeScope">
                                <el-radio-button v-for="dict in dict.type.coupon_use_range" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="展示在app购买页" prop="isShowAppBuyPage">
                            <el-radio-group v-model="form.isShowAppBuyPage">
                                <el-radio-button v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="展示在小程序购买页" prop="isShowWxBuyPage">
                            <el-radio-group v-model="form.isShowWxBuyPage">
                                <el-radio-button v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="展示在扫码进入购买页" prop="isShowWxBuyPageFromQrcode">
                            <el-radio-group v-model="form.isShowWxBuyPageFromQrcode">
                                <el-radio-button v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>

                    <!--                    <el-col :span="8">-->
                    <!--                        <el-form-item label="优惠券来源" prop="couponFrom">-->
                    <!--                            <el-radio-group v-model="form.couponFrom">-->
                    <!--                                <el-radio-button v-for="dict in dict.type.coupon_from_type" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>-->
                    <!--                            </el-radio-group>-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->

                    <!--                    <el-col :span="16">-->
                    <!--                        <el-form-item label="仅限新用户参与" prop="canOnlyNewUser">-->
                    <!--                            <el-radio-group v-model="form.canOnlyNewUser">-->
                    <!--                                <el-radio-button v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>-->
                    <!--                            </el-radio-group>-->
                    <!--                            <span style="color: #999999"> （新用户是指注册时间一周之内，且没有消费记录的用户) </span>-->
                    <!--                        </el-form-item>-->
                    <!--                    </el-col>-->
                    <el-col :span="8">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio-button v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-divider content-position="left">参与门店信息</el-divider>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="适用门店" prop="selectStoreIds">
                            <el-transfer v-model="form.selectStoreIds" :data="form.storeVOS" filterable="true" :titles="['待选择的门店', '已选择的门店']"></el-transfer>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listCouponInfo, getInfo, delCouponInfo, addCouponInfo, updateCouponInfo } from '@/api/activity/couponInfo'
import { updateInfoStatus } from '@/api/activity/info'
export default {
    name: 'CouponInfo',
    dicts: ['sys_normal_disable', 'coupon_use_range', 'coupon_from_type', 'sys_yes_no', 'activity_user_scope', 'activity_pay_scope'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 送券活动表格数据
            couponInfoList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {},
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
                description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
                imgUrl: [{ required: true, message: '宣传图片不能为空', trigger: 'blur' }],
                content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
                duringDate: [{ required: true, message: '有效期不能为空', trigger: 'blur' }],
                userScope: [{ required: true, message: '适用用户范围不能为空', trigger: 'change' }],
                payScope: [{ required: true, message: '支付范围不能为空', trigger: 'change' }],
                activityType: [{ required: true, message: '活动类型不能为空', trigger: 'change' }],
                status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
                canMultiUse: [{ required: true, message: '是否可多张使用不能为空', trigger: 'change' }],
                maxUseCount: [{ required: true, message: '单次订单最多使用券数不能为空', trigger: 'blur' }],
                isShowAppBuyPage: [{ required: true, message: '是否展示在app购买页不能为空', trigger: 'change' }],
                isShowWxBuyPage: [{ required: true, message: '是否展示在小程序购买页不能为空', trigger: 'change' }],
                isShowWxBuyPageFromQrcode: [{ required: true, message: '是否展示在小程序购买页不能为空', trigger: 'change' }],
                storeScope: [{ required: true, message: '优惠券使用范围不能为空', trigger: 'change' }],
                couponFrom: [{ required: true, message: '优惠券来源不能为空', trigger: 'change' }],
                maxBuyNum: [{ required: true, message: '最大可购买数量不能为空', trigger: 'blur' }],
                price: [{ required: true, message: '活动价格不能为空', trigger: 'blur' }]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询送券活动列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                duringDate: [],
                activityType: '1',
                status: '0',
                canMultiUse: 'N',
                maxUseCount: 1,
                isShowAppBuyPage: null,
                isShowWxBuyPage: null,
                isShowWxBuyPageFromQrcode: null,
                storeScope: null,
                couponFrom: null,
                maxBuyNum: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 设置价格 */
        handleSetCouponItems(row) {
            this.$tab.openPage(row.title + '的优惠券', '/activity/couponItem/' + row.id)
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            getInfo().then((response) => {
                var duringDate = []
                response.data.duringDate = duringDate
                response.data.status = '0'
                this.form = response.data
                this.open = true
                this.title = '修改送券活动'
            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getInfo(id).then((response) => {
                var duringDate = []
                duringDate.push(response.data.startDate)
                duringDate.push(response.data.endDate)
                response.data.duringDate = duringDate
                this.form = response.data
                this.open = true
                this.title = '修改送券活动'
            })
        },
        /** 提交按钮 */
        submitForm() {
            if (null != this.form.duringDate && this.form.duringDate.length > 0) {
                this.form['startDate'] = this.form.duringDate[0]
                this.form['endDate'] = this.form.duringDate[1]
            } else {
                delete this.form.startDate
                delete this.form.endDate
            }
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCouponInfo(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addCouponInfo(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除送券活动编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delCouponInfo(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/activity/couponInfo/export',
                {
                    ...this.queryParams
                },
                `couponInfo_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$confirm('确认要' + text + ' ' + row.title + ' 吗？')
                .then(function () {
                    return updateInfoStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        }
    }
}
</script>
