<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88x">
            <el-form-item prop="batchNo">
                <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="batchName">
                <el-input v-model="queryParams.batchName" placeholder="请输入批次名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="lastExpireTime">
                <el-date-picker v-model="queryParams.lastExpireTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="激活过期日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <!-- <el-form-item prop="lastUseLevel">
                <el-radio-group v-model="queryParams.lastUseLevel">
                    <el-radio-button :key="0" :label="0">正常</el-radio-button>
                    <el-radio-button :key="1" :label="1">优先扣款</el-radio-button>
                </el-radio-group>
            </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="['recharge:batchRecord: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="['recharge:batchRecord: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="['recharge:batchRecord:remove']"-->
            <!--        >删除</el-button>-->
            <!--      </el-col>-->
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--           -->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['recharge:batchRecord:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/recharge/card/batchRecord/findPage" :request-params="queryParams" ref="pagination">
            <!--      <el-table-column type="selection" width="55" align="center" fixed="left"/>-->
            <el-table-column label="编号" align="center" prop="id" />
            <el-table-column label="批次号" align="center" prop="batchNo" />
            <el-table-column label="批次名称" align="center" prop="batchName" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="激活过期时间" align="center" prop="lastExpireTime" width="180" />
            <!-- <el-table-column label="优先消费" align="center" prop="lastUseLevel">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.lastUseLevel" :active-value="1" :inactive-value="0" @change="handleUpdateUseLevel(scope.row)"></el-switch>
                </template>
            </el-table-column> -->
            <!-- <el-table-column label="客户星级" align="center" prop="starLevel" width="150">
                <template slot-scope="scope">
                    <el-rate v-model="scope.row.starLevel" disabled disabled-void-color="#DCDFE6"></el-rate>
                </template>
            </el-table-column> -->
            <el-table-column label="创建人" align="center" prop="createBy" width="120" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
            <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="handleUpdateExpireTime(scope.row)" v-hasPermi="['recharge:batchRecord:edit']">修改过期时间</el-button>
                    <el-button type="text" icon="el-icon-edit" @click="handleCardDetail(scope.row)" v-hasPermi="['recharge:batchRecord:edit']">充值卡明细</el-button>
                    <el-button type="text" icon="el-icon-edit" @click="handleUseRecord(scope.row)" v-hasPermi="['recharge:batchRecord:edit']">充值卡使用记录</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改充值批次记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
            <el-row :gutter="15" style="margin-bottom: 80px; margin-left: 20px; width: calc(100% - 20px)">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <el-col :span="24">
                        <el-descriptions class="margin-top" :column="1" border>
                            <el-descriptions-item>
                                <template slot="label"> 批次号 </template>
                                {{ form.batchNo }}
                            </el-descriptions-item>
                            <el-descriptions-item>
                                <template slot="label"> 批次名称 </template>
                                {{ form.batchName }}
                            </el-descriptions-item>
                        </el-descriptions>
                    </el-col>
                    <el-col :span="24" style="margin-top: 15px">
                        <el-form-item label="激活过期时间" prop="lastExpireTime">
                            <el-date-picker clearable v-model="form.lastExpireTime" type="date" value-format="yyyy-MM-dd" placeholder="到期时间"> </el-date-picker>
                        </el-form-item>

                        <!--                        <el-form-item label="优先扣款" prop="lastUseLevel" label-width="140px">-->
                        <!--                            <el-radio-group v-model.number="form.lastUseLevel">-->
                        <!--                                <el-radio-button label="0">默认</el-radio-button>-->
                        <!--                                <el-radio-button label="1">优先</el-radio-button>-->
                        <!--                            </el-radio-group>-->
                        <!--                        </el-form-item>-->
                        <el-form-item label="是否有洗衣权限" label-width="140px">
                            <template>
                                <el-radio-group v-model="form.isSupportCloth">
                                    <el-radio-button label="Y">有</el-radio-button>
                                    <el-radio-button label="N">无</el-radio-button>
                                </el-radio-group>
                            </template>
                        </el-form-item>
                        <el-form-item label="是否有商品权限" label-width="140px">
                            <template>
                                <el-radio-group v-model="form.isSupportGoods">
                                    <el-radio-button label="Y">有</el-radio-button>
                                    <el-radio-button label="N">无</el-radio-button>
                                </el-radio-group>
                            </template>
                        </el-form-item>
                        <el-form-item label="是否有生活服务权限" label-width="140px">
                            <template>
                                <el-radio-group v-model="form.isSupportLife">
                                    <el-radio-button label="Y">有</el-radio-button>
                                    <el-radio-button label="N">无</el-radio-button>
                                </el-radio-group>
                            </template>
                        </el-form-item>
                        <el-form-item label="是否有汽车美容权限" label-width="140px">
                            <template>
                                <el-radio-group v-model="form.isSupportCar">
                                    <el-radio-button label="Y">有</el-radio-button>
                                    <el-radio-button label="N">无</el-radio-button>
                                </el-radio-group>
                            </template>
                        </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 { findBatchRecordPage, getOneByBatchNo, updateExpireTimeByBatchNo, updateUseLevelByBatchNo } from '../../../api/recharge/batchRecord'
import { getGoods } from '@/api/core/goods'

export default {
    name: 'BatchRecord',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 充值批次记录表格数据
            batchRecordList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                batchNo: null,
                batchName: null,
                lastExpireTime: null,
                lastUseLevel: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                lastExpireTime: [{ 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,
                batchNo: null,
                batchName: null,
                lastExpireTime: null,
                lastUseLevel: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            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
        },
        /** 过期时间按钮操作 */
        handleUpdateExpireTime(row) {
            getOneByBatchNo(row.batchNo).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改批次号信息'
            })
        },
        /** 使用情况按钮操作 */
        handleUseRecord(row) {
            this.$tab.openPage(row.batchName + '批次的使用情况', '/recharge/useRecord/' + row.batchNo)
        },
        /** 卡明细按钮操作 */
        handleCardDetail(row) {
            this.$tab.openPage(row.batchName + '批次的充值卡明细', '/recharge/cardDetail/' + row.batchNo)
        },
        handleUpdateUseLevel(row) {
            var form = { batchNo: row.batchNo, useLevel: row.lastUseLevel }
            let text = row.lastUseLevel == '1' ? '启用' : '停用'
            this.$confirm('确认要' + text + ' ' + row.batchName + ' 优先扣款吗？')
                .then(function () {
                    return updateUseLevelByBatchNo(form)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status == '0' ? '1' : '0'
                })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    var params = {
                        batchNo: this.form.batchNo,
                        expireTime: this.form.lastExpireTime,
                        useLevel: this.form.lastUseLevel,
                        starLevel: this.form.starLevel,
                        isSupportCloth: this.form.isSupportCloth,
                        isSupportGoods: this.form.isSupportGoods,
                        isSupportLife: this.form.isSupportLife,
                        isSupportCar: this.form.isSupportLife
                    }
                    updateExpireTimeByBatchNo(params).then((response) => {
                        this.$modal.msgSuccess('修改成功')
                        this.open = false
                        this.getList()
                    })
                }
            })
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
