<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px"
            size="medium" class="ry_form">
            <el-form-item label="任务ID" prop="id">
                <el-input v-model="queryParams.id" placeholder="请输入任务ID" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small" filterable>
                    <el-option label="优惠券" value="0" />
                    <el-option label="现金" value="1" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="类型名称" prop="taskName">
                <el-input v-model="queryParams.taskName" placeholder="请输入类型名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item label="标题" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入标题" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <template v-if="showMoreCondition"> -->
                <el-form-item label="主题" prop="theme">
                    <el-input v-model="queryParams.theme" placeholder="请输入主题" clearable size="small"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="结算优惠券" prop="taskCoupon">
                    <el-select v-model="queryParams.taskCoupon" placeholder="请选择结算优惠券" clearable size="small" filterable>
                        <el-option
                            v-for="coupon in couponList"
                            :key="coupon.id"
                            :label="coupon.stockName"
                            :value="coupon.stockName">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable size="small" v-model="queryParams.startTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="截至时间" prop="endTime">
                    <el-date-picker clearable size="small" v-model="queryParams.endTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择截至时间">
                    </el-date-picker>
                </el-form-item>
            <!-- </template> -->
            <el-form-item class="flex_one tr">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <!-- <el-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini"
                    @click="showMoreCondition = !showMoreCondition">{{ showMoreCondition ? '收起条件' : '展开条件' }}</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" size="mini" @click="handleAdd"
                    v-hasPermi="['model:appletTask:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['model:appletTask:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['model:appletTask:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
                    @click="handleExport" v-hasPermi="['model:appletTask:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="appletTaskList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="任务ID" align="center" prop="id" width="80" />


            <el-table-column label="标题" align="center" prop="title" />
            <el-table-column label="主题" align="center" prop="theme" />

            <el-table-column label="类型" align="center" prop="type">
                <template slot-scope="scope">
                    <span>{{ scope.row.type == '0' ? '优惠券' : '现金' }}</span>
                </template>
            </el-table-column>

            <el-table-column label="结算优惠券" align="center" prop="taskCouponTitle" />
<!--            <el-table-column label="类型名称" align="center" prop="taskName" />-->
            <!--      <el-table-column label="小图" align="center" prop="taskIcon" v-if="columns[3].visible"/>-->

            <el-table-column label="小图" align="center" prop="taskIcon">
                <template slot-scope="{ row }">
                    <el-image v-if="row.taskIcon" :src="row.taskIcon" :preview-src-list="[row.taskIcon]"
                        class="small-img circle-img" />
                </template>
            </el-table-column>

            <el-table-column label="报酬" align="center" prop="taskMoney" />
            <!--      <el-table-column label="图片" align="center" prop="image" v-if="columns[5].visible"/>-->

            <el-table-column label="图片" align="center" prop="image">
                <template slot-scope="{ row }">
                    <el-image v-if="row.image" :src="row.image" :preview-src-list="[row.image]"
                        class="small-img circle-img" />
                </template>
            </el-table-column>


            <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="截至时间" align="center" prop="endTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="最大领取数量" align="center" prop="maxGetNum" />
            <el-table-column label="已领取数量" align="center" prop="giveNum" />


            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['model:appletTask:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['model:appletTask: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" width="50%" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="108px">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="主题" prop="theme">
                    <el-input v-model="form.theme" placeholder="请输入主题" />
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择类型" filterable>
                        <el-option label="优惠券" value="0"></el-option>
                        <el-option label="现金" value="1"></el-option>
                    </el-select>
                </el-form-item>
<!--                <el-form-item label="类型名称" prop="taskName">-->
<!--                    <el-input v-model="form.taskName" placeholder="请输入类型名称" />-->
<!--                </el-form-item>-->
                <el-form-item label="图标">
                    <oss-image-upload v-model="form.taskIcon" :limit="1"></oss-image-upload>
                </el-form-item>
                <el-form-item label="报酬" prop="taskMoney">
                    <el-input v-model="form.taskMoney" placeholder="请输入报酬" />
                </el-form-item>
                <el-form-item label="图片">
                    <oss-image-upload v-model="form.image" :limit="1"></oss-image-upload>
                </el-form-item>
                <el-form-item label="结算优惠券" prop="taskCoupon">
                    <el-select v-model="form.taskCoupon" placeholder="请选择结算优惠券" clearable filterable>
                        <el-option
                            v-for="coupon in couponList"
                            :key="coupon.id"
                            :label="coupon.stockName"
                            :value="coupon.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable size="small" v-model="form.startTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="截至时间" prop="endTime">
                    <el-date-picker clearable size="small" v-model="form.endTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择截至时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="最大领取数量" prop="maxGetNum">
                    <el-input v-model="form.maxGetNum" placeholder="请输入最大领取数量" type="number" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAppletTask, getAppletTask, delAppletTask, addAppletTask, updateAppletTask, exportAppletTask } from "@/api/model/AppletTask";
import { listWechatStock } from "@/api/marketing/wechatStock";

export default {
    name: "AppletTask",
    dicts: [],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 中心表格数据
            appletTaskList: [],
            // 优惠券列表
            couponList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                id: null,
                type: null,
                taskName: null,
                taskIcon: null,
                taskMoney: null,
                taskCoupon: null,
                image: null,
                title: null,
                theme: null,
                startTime: null,
                endTime: null,
                maxGetNum: null,
                giveNum: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                type: [
                    { required: true, message: "类型不能为空", trigger: "change" }
                ],
                taskName: [
                    { required: true, message: "类型名称不能为空", trigger: "blur" }
                ],
                taskIcon: [
                    { required: true, message: "小图不能为空", trigger: "blur" }
                ],
                taskMoney: [
                    // { required: true, message: "报酬不能为空", trigger: "blur" }
                ],
                image: [
                    { required: true, message: "图片不能为空", trigger: "blur" }
                ],
                title: [
                    { required: true, message: "标题不能为空", trigger: "blur" }
                ],
                theme: [
                    { required: true, message: "主题不能为空", trigger: "blur" }
                ],
                startTime: [
                    { required: true, message: "开始时间不能为空", trigger: "blur" }
                ],
                endTime: [
                    { required: true, message: "截至时间不能为空", trigger: "blur" }
                ],
                maxGetNum: [
                    { required: true, message: "最大领取数量不能为空", trigger: "blur" }
                ],
            },
            columns: [
                { key: 1, label: "类型", visible: true },
                { key: 2, label: "结算优惠券", visible: true },
                { key: 3, label: "类型名称", visible: true },
                { key: 4, label: "小图", visible: false },
                { key: 5, label: "报酬", visible: true },
                { key: 6, label: "图片", visible: false },
                { key: 7, label: "标题", visible: true },
                { key: 8, label: "主题", visible: true },
                { key: 9, label: "开始时间", visible: true },
                { key: 10, label: "截至时间", visible: true },
                { key: 11, label: "最大领取数量", visible: true },
                { key: 12, label: "已领取数量", visible: true },
            ],
            showMoreCondition: false
        };
    },
    created() {
        this.getList();
        this.getCouponList();
    },
    methods: {
        /** 查询中心列表 */
        getList() {
            this.loading = true;
            listAppletTask(this.queryParams).then(response => {
                this.appletTaskList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 获取优惠券列表 */
        getCouponList() {
            listWechatStock({ pageNum: 1, pageSize: 999999999 }).then(response => {

                response.rows.forEach(item => {
                    item.id = item.id + "";
                })

                this.couponList = response.rows || [];
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                type: null,
                taskName: null,
                taskIcon: null,
                taskMoney: null,
                taskCoupon: null,
                image: null,
                title: null,
                theme: null,
                startTime: null,
                endTime: null,
                maxGetNum: null,
                giveNum: null,
                createTime: null,
                createBy: null,
                updateTime: null,
                updateBy: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.id = null;
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加中心";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getAppletTask(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改中心";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateAppletTask(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAppletTask(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 delAppletTask(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$modal.confirm('是否确认导出所有中心数据项？').then(() => {
                this.exportLoading = true;
                return exportAppletTask(queryParams);
            }).then(response => {
                this.download(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        }
    }
};
</script>
