<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:recharge:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/activity/recharge/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" width="250" />
            <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="status" width="150">
                <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="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="storeNum">
                <template slot-scope="scope">
                    <el-button type="text" @click="handleUpdate(scope.row)">{{ scope.row.storeNum }}家门店</el-button>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="300">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['activity:recharge:edit']">修改</el-button>
                    <el-button type="text" icon="el-icon-s-shop" @click="handleSetRechargeItems(scope.row)" v-hasPermi="['activity:recharge:list']">设置充值规则</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['activity:recharge:remove']">删除</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改活动明细对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="50%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <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-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-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 { listInfo, getInfo, delInfo, addInfo, updateInfo, updateInfoStatus } from '@/api/activity/rechargeInfo'

export default {
    name: 'RechargeInfo',
    dicts: ['activity_user_scope', 'activity_type', 'activity_pay_scope', 'sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 活动明细表格数据
            infoList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                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' }]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询活动明细列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                title: null,
                description: null,
                imgUrl: null,
                content: null,
                duringDate: [],
                activityType: null,
                delFlag: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                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()
            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) {
                        updateInfo(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addInfo(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 delInfo(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/activity/recharge/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 updateInfoStatus(row.id, row.status)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        },
        handleSetRechargeItems(row) {
            this.$tab.openPage(row.title + '的充值规则', '/activity/rechargeItem/' + row.id)
        }
    }
}
</script>
