<template>
    <div class="app-container">
        <!-- 搜索区域 -->
        <el-card class="search-wrapper" shadow="never">
            <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="80px">
                <el-form-item label="活动标题" prop="title">
                    <el-input v-model="queryParams.title" placeholder="请输入活动标题" clearable style="width: 240px" @keyup.enter.native="handleQuery">
                        <i slot="prefix" class="el-input__icon el-icon-search"></i>
                    </el-input>
                </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-card>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" 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>

        <!-- 列表区域 -->
        <el-card shadow="never">
            <Page uri="/mapi/activity/recharge/list" :request-params="queryParams" ref="pagination">
              <el-table-column label="活动编号" align="center" prop="id" width="100" />
              <el-table-column label="活动信息" width="350" align="left">
                    <template slot-scope="scope">
                        <div class="activity-card">
                            <div class="activity-main">
                                <image-preview :src="scope.row.imgUrl" :width="80" :height="80" class="activity-img" />
                                <div class="activity-content">
                                    <div class="activity-title">
                                        <span>{{ scope.row.title }}</span>
                                    </div>
                                    <div class="activity-desc">{{ scope.row.description }}</div>
                                </div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="活动时间" align="center" width="250">
                    <template slot-scope="scope">
                        {{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }} 至 {{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}
                    </template>
                </el-table-column>
                <el-table-column label="适用门店" align="center" width="120">
                    <template slot-scope="scope">
                        <el-tag size="medium" type="success">{{ scope.row.storeNum }}家门店</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="活动状态" align="center" width="120">
                    <template slot-scope="scope">
                        <div class="status-column">
                            <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime" width="200" />

              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
                <template slot-scope="scope">
                  <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >修改</el-button>
                  <el-button type="text" icon="el-icon-s-shop"  @click="handleSetRechargeItems(scope.row)" >设置规则</el-button>
                  <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" >删除</el-button>
                </template>
              </el-table-column>

            </Page>
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body class="activity-dialog" :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
                <el-tabs v-model="activeTab">
                    <el-tab-pane label="基本信息" name="basic">
                        <el-row :gutter="20">
                            <el-col :span="14">
                                <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" :rows="3" placeholder="请输入活动描述" maxlength="500" show-word-limit />
                                </el-form-item>
                                <el-form-item label="活动时间" prop="duringDate" required>
                                    <el-date-picker v-model="form.duringDate" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 100%"> </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                <el-form-item label="宣传图片" prop="imgUrl">
                                    <image-upload v-model="form.imgUrl" :limit="1" :is-show-tip="true" :file-size="5" accept=".jpg,.jpeg,.png" tip-text="建议尺寸：800x400px，大小不超过5MB" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-tab-pane>

                    <el-tab-pane label="使用规则" name="rules">
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="适用门店" prop="selectStoreIds">
                                    <el-transfer v-model="form.selectStoreIds" :data="form.storeVOS" :titles="['可选门店', '已选门店']" :button-texts="['移除', '选择']" filterable> </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-row>
                    </el-tab-pane>

                    <el-tab-pane label="活动详情" name="content">
                        <el-form-item label="活动详情" prop="content">
                            <editor v-model="form.content" :min-height="400" />
                        </el-form-item>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" :loading="loading">确 定</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: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 活动明细表格数据
            infoList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 当前激活的标签页
            activeTab: 'basic',
            // 查询参数
            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',
                selectStoreIds: [],
                storeVOS: []
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.loading = false
            this.activeTab = 'basic'
            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()
            this.loading = false
            this.activeTab = 'basic'
            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() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.loading = true
                    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
                    }

                    const submitRequest = this.form.id != null ? updateInfo(this.form) : addInfo(this.form)

                    submitRequest
                        .then((response) => {
                            this.$modal.msgSuccess(this.form.id != null ? '修改成功' : '新增成功')
                            this.open = false
                            this.getList()
                        })
                        .catch((error) => {
                            console.error('提交失败:', error)
                            this.$modal.msgError('操作失败：' + (error.message || '未知错误'))
                        })
                        .finally(() => {
                            this.loading = false
                        })
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除充值活动编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delInfo(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .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'
                })
        },
        // 处理下拉菜单命令
        handleCommand(command, row) {
            switch (command) {
                case 'edit':
                    this.handleUpdate(row)
                    break
                case 'setRules':
                    this.handleSetRechargeItems(row)
                    break
                case 'delete':
                    this.handleDelete(row)
                    break
            }
        },
        handleSetRechargeItems(row) {
            this.$tab.openPage(row.title + '的充值规则', '/activity/rechargeItem/' + row.id)
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    .search-wrapper {
        margin-bottom: 10px;
    }

    .activity-card {
        padding: 15px;
        background: #fff;
        border-radius: 4px;

        .activity-main {
            display: flex;
            align-items: flex-start;

            .activity-img {
                margin-right: 20px;
                border-radius: 4px;
                box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            }

            .activity-content {
                flex: 1;
                min-width: 0;

                .activity-title {
                    display: flex;
                    align-items: center;
                    margin-bottom: 10px;

                    span {
                        font-size: 16px;
                        font-weight: 500;
                        color: #303133;
                    }
                }

                .activity-desc {
                    font-size: 14px;
                    color: #606266;
                    line-height: 1.4;
                }
            }
        }
    }

    .status-column {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }
}

.activity-dialog {
    .el-dialog__body {
        padding: 20px 30px;
    }

    .radio-group-block {
        // display: flex;
        width: 100%;

        .el-radio-button {
            flex: 1;

            &__inner {
                width: 100%;
            }
        }
    }

    .el-transfer {
        width: 100%;

        .el-transfer-panel {
            width: 45%;

            &__body {
                height: 300px;
            }
        }
    }
}

// 下拉菜单样式
.dropdown-danger {
    color: #f56c6c;
}

:deep(.el-table) {
    border: none;

    &::before {
        display: none;
    }

    .el-table__header-wrapper {
        display: none;
    }

    .el-table__row {
        background-color: transparent;
    }

    .el-table__cell {
        background-color: transparent;
    }
}
</style>
