<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="标题" 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:groupInfo:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['activity:groupInfo:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['activity:groupInfo:remove']">删除</el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--           -->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['activity:info:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" fixed="left" />
            <el-table-column label="编号" align="center" prop="id" width="80" fixed="left" />
            <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" width="180" fixed="left" />
            <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="50" :height="50" />
                </template>
            </el-table-column>
            <el-table-column label="开始时间" align="center" prop="startDate" width="180">
                <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="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="80">
                <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="groupPrice" />
            <el-table-column label="拼团人数" align="center" prop="groupNum" />
            <el-table-column label="活动最大可参与次数" align="center" prop="maxBuyCount" width="180">
                <template slot-scope="scope">
                    <span v-if="scope.row.maxBuyCount == 0">不限制</span>
                    <span v-else>{{ scope.row.maxBuyCount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="是否可多张券使用" align="center" prop="canMultiUse" width="180">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.canMultiUse" />
                </template>
            </el-table-column>
            <el-table-column label="一次订单最多可使用券数" align="center" prop="maxUseCount" width="180">
                <template slot-scope="scope">
                    <span v-if="scope.row.maxUseCount == 0">不限制</span>
                    <span v-else>{{ scope.row.maxUseCount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="是否展示在app购买页" align="center" prop="isShowAppBuyPage" width="180">
                <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="180">
                <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="180">
                <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:groupInfo:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-s-shop" @click="handleSetCouponItems(scope.row)" v-hasPermi="['activity:groupInfo:list']">设置优惠券</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:groupInfo:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改活动明细对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="65%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="200px">
                    <el-col :span="24">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="form.title" placeholder="请输入标题" maxlength="200" show-word-limit />
                        </el-form-item>
                        <el-form-item label="描述" prop="description">
                            <el-input v-model="form.description" type="textarea" placeholder="请输入描述" maxlength="500" show-word-limit />
                        </el-form-item>
                        <el-form-item label="宣传图片">
                            <image-upload v-model="form.imgUrl" />
                        </el-form-item>
                        <el-form-item label="内容">
                            <editor v-model="form.content" :min-height="192" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <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="12">
                        <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="12">
                        <el-form-item prop="maxUseCount">
                            <span slot="label">
                                <el-tooltip content="为0时不限制数量" placement="top">
                                    <i class="el-icon-question"></i>
                                </el-tooltip>
                                单次订单最多可用券数量
                            </span>
                            <el-input-number v-model="form.maxUseCount" placeholder="请输入一次订单最多可使用券数量" :precision="0" :min="0" :max="99999" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <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="12">
                        <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="12">
                        <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="12">
                        <el-form-item prop="maxBuyCount">
                            <span slot="label">
                                <el-tooltip content="为0时不限制参与次数" placement="top">
                                    <i class="el-icon-question"></i>
                                </el-tooltip>
                                活动最大可参与次数
                            </span>
                            <el-input-number v-model="form.maxBuyCount" placeholder="请输入活动最大可参与次数" :precision="0" :min="0" :max="99999" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="拼团价格" prop="groupPrice">
                            <el-input-number v-model="form.groupPrice" placeholder="请输入拼团价格" :precision="2" :min="0" :max="9999999" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="拼团人数" prop="groupNum">
                            <el-input-number v-model="form.groupNum" placeholder="请输入拼团人数" :precision="0" :min="0" :max="9999" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="适用工厂" prop="selectFactoryIds">
                            <el-transfer v-model="form.selectFactoryIds" :data="form.factoryVOS" filterable="true" :titles="['待选择的工厂', '已选择的工厂']"></el-transfer>
                        </el-form-item>
                    </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-col :span="24">
                        <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-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 { listGroupInfo, getGroupInfo, delGroupInfo, addGroupInfo, updateGroupInfo, updateGroupInfoStatus } from '@/api/activity/groupInfo'
export default {
    name: 'ActivityDiscountInfo',
    dicts: ['activity_user_scope', 'activity_type', 'activity_pay_scope', 'sys_normal_disable', 'sys_yes_no'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 活动明细表格数据
            infoList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                title: null,
                activityType: null
            },
            // 表单参数
            form: {
                duringDate: [],
                status: '0'
            },
            // 表单校验
            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' }],
                activityType: [{ required: true, message: '活动类型不能为空', trigger: 'change' }],
                status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
                canMultiUse: [{ required: true, message: '是否可多张券使用不能为空', trigger: 'blur' }],
                maxUseCount: [{ required: true, message: '一次订单最多可使用券数量不能为空', trigger: 'blur' }],
                isShowAppBuyPage: [{ required: true, message: '是否展示在app购买页不能为空', trigger: 'blur' }],
                isShowWxBuyPage: [{ required: true, message: '是否展示在小程序购买页不能为空', trigger: 'blur' }],
                isShowWxBuyPageFromQrcode: [{ required: true, message: '是否展示在小程序购买页不能为空', trigger: 'blur' }],
                maxBuyCount: [{ required: true, message: '活动最大可参与次数不能为空', trigger: 'blur' }],
                groupPrice: [{ required: true, message: '拼团价格不能为空', trigger: 'blur' }],
                groupNum: [{ required: true, message: '拼团人数不能为空', trigger: 'blur' }],
                payScope: [{ required: true, message: '支付范围不能为空', trigger: 'change' }]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询活动明细列表 */
        getList() {
            this.loading = true
            listGroupInfo(this.queryParams).then((response) => {
                this.infoList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                title: null,
                description: null,
                imgUrl: null,
                content: null,
                startDate: null,
                endDate: null,
                userScope: null,
                payScope: null,
                activityType: null,
                delFlag: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                duringDate: [],
                status: '0'
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        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()
            getGroupInfo().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
            getGroupInfo(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) {
                        updateGroupInfo(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addGroupInfo(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 delGroupInfo(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/activity/groupInfo/export',
                {
                    ...this.queryParams
                },
                `info_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$confirm('确认要' + text + ' ' + row.name + ' 吗？')
                .then(function () {
                    return updateGroupInfoStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        },
        /** 设置价格 */
        handleSetCouponItems(row) {
            this.$tab.openPage(row.title + '的优惠券', '/activity/couponItem/' + row.id)
        }
    }
}
</script>
