<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="realName">
                <el-input v-model="queryParams.realName" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="storeName">
                <el-input v-model="queryParams.storeName" 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="['recharge:order: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:order: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:order:remove']"-->
            <!--        >删除</el-button>-->
            <!--      </el-col>-->
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--           -->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['recharge:order:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <Page uri="/mapi/recharge/refund/list" :request-params="queryParams" ref="pagination">
            <!--      <el-table-column type="selection" width="55" align="center" fixed="left"/>-->
            <!--      <el-table-column label="主键ID" align="center" prop="id" />-->
            <el-table-column label="订单编号" align="center" prop="orderNo" />
            <el-table-column label="储值卡号" align="center" prop="cardNo" />
            <el-table-column label="储值门店" align="center">
                <template slot-scope="scope">
                    {{ scope.row.storeName }}
                </template>
            </el-table-column>
            <el-table-column label="客户姓名" align="center" prop="appUserName" />
            <el-table-column label="客户手机号" align="center" prop="appUserPhone" />
            <el-table-column label="退款金额" align="center" prop="refundAmount" />
            <el-table-column label="退款方式" align="center" prop="refundType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.recharge_pay_type" :value="scope.row.refundType" />
                </template>
            </el-table-column>
            <el-table-column label="退款金额（元）" align="center" prop="payAmount" width="120">
                <template slot-scope="scope">
                    <span style="display: block; text-align: left">现金退款: {{ scope.row.cashRefundAmount }}</span>
                    <span style="display: block; text-align: left">微信退款: {{ scope.row.wxRefundAmount }}</span>
                    <span style="display: block; text-align: left">支付宝退款: {{ scope.row.aliRefundAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="卡内余额（元）" align="center" prop="rechargeAmount" width="120">
                <template slot-scope="scope">
                    <span style="display: block; text-align: left">现金余额: {{ scope.row.rechargeBalance }}</span>
                    <span style="display: block; text-align: left">赠送余额: {{ scope.row.giveBalance }}</span>
                    <span style="display: block; text-align: left">福利余额: {{ scope.row.welfareBalance }}</span>
                </template>
            </el-table-column>
            <el-table-column label="退款描述" align="center" prop="refundReason" />
            <el-table-column label="退款申请时间" align="center" prop="refundApplyTime" width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.refundApplyTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
                <template slot-scope="scope">
                    <span>{{ scope.row.refundTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="退卡状态" align="center" prop="refundStatus" width="120">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.recharge_refund_status" :value="scope.row.refundStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
                <template slot-scope="scope" v-if="scope.row.refundStatus == '1'">
                    <el-button type="text" icon="el-icon-close" @click="handleApprovalNo(scope.row)" v-hasPermi="['recharge:order:edit']">拒绝退卡</el-button>
                    <el-button type="text" icon="el-icon-check" @click="handleApprovalYes(scope.row)" v-hasPermi="['recharge:order:edit']">同意退卡</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改充值订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="30%" 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="orderNo">
                            <el-input v-model="form.orderNo" placeholder="请输入订单编号" maxlength="50" show-word-limit />
                        </el-form-item>
                        <el-form-item label="组织ID" prop="orgId">
                            <el-input v-model="form.orgId" placeholder="请输入组织ID" />
                        </el-form-item>
                        <el-form-item label="客户ID" prop="appUserId">
                            <el-input v-model="form.appUserId" placeholder="请输入客户ID" />
                        </el-form-item>
                        <el-form-item label="支付时间" prop="payTime">
                            <el-date-picker clearable v-model="form.payTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="支付金额" prop="payAmount">
                            <el-input v-model="form.payAmount" placeholder="请输入支付金额" />
                        </el-form-item>
                        <el-form-item label="支付宝/微信支付订单号" prop="outTradeOrderNo">
                            <el-input v-model="form.outTradeOrderNo" placeholder="请输入支付宝/微信支付订单号" maxlength="200" show-word-limit />
                        </el-form-item>
                        <el-form-item label="充值金额" prop="rechargeAmount">
                            <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
                        </el-form-item>
                        <el-form-item label="赠送金额" prop="giveAmount">
                            <el-input v-model="form.giveAmount" placeholder="请输入赠送金额" />
                        </el-form-item>
                        <el-form-item label="赠送积分" prop="givePointAmount">
                            <el-input v-model="form.givePointAmount" placeholder="请输入赠送积分" />
                        </el-form-item>
                        <el-form-item label="参与活动ID" prop="activityId">
                            <el-input v-model="form.activityId" placeholder="请输入参与活动ID" />
                        </el-form-item>
                        <el-form-item label="充值套餐ID" prop="rechargeTypeId">
                            <el-input v-model="form.rechargeTypeId" placeholder="请输入充值套餐ID" />
                        </el-form-item>
                        <el-form-item label="退款申请时间" prop="refundApplyTime">
                            <el-date-picker clearable v-model="form.refundApplyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择退款申请时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="退款时间" prop="refundTime">
                            <el-date-picker clearable v-model="form.refundTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择退款时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="退款金额" prop="refundAmount">
                            <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
                        </el-form-item>
                        <el-form-item label="退款描述" prop="refundDescribe">
                            <el-input v-model="form.refundDescribe" type="textarea" placeholder="请输入退款描述" maxlength="500" show-word-limit />
                        </el-form-item>
                        <el-form-item label="删除状态" prop="delFlag">
                            <el-input v-model="form.delFlag" placeholder="请输入删除状态" maxlength="1" show-word-limit />
                        </el-form-item>
                        <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="orderSource">
                            <el-input v-model="form.orderSource" placeholder="请输入下单渠道" maxlength="10" show-word-limit />
                        </el-form-item>
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" maxlength="50" show-word-limit />
                        </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 { listRefund, refundApprovalNo, refundApprovalYes } from '@/api/recharge/refund'

export default {
    name: 'Refund',
    dicts: ['recharge_use_status', 'recharge_refund_status', 'recharge_pay_type', 'recharge_order_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 充值订单表格数据
            refundList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                orderNo: null,
                orgId: null,
                storeName: null,
                phoneNumber: null,
                realName: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询充值订单列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                orderNo: null,
                orgId: null,
                sourceType: null,
                appUserId: null,
                payTime: null,
                payAmount: null,
                payType: null,
                outTradeOrderNo: null,
                rechargeAmount: null,
                giveAmount: null,
                givePointAmount: null,
                activityId: null,
                rechargeTypeId: null,
                orderStatus: null,
                refundApplyTime: null,
                refundTime: null,
                refundAmount: null,
                refundDescribe: null,
                delFlag: null,
                title: null,
                orderSource: 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
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加充值订单'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getOrder(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改充值订单'
            })
        },

        handleApprovalNo(row) {
            const id = row.id
            this.$modal
                .confirm('确定拒绝该客户的退卡申请嘛”')
                .then(function () {
                    return refundApprovalNo(id)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('审核完成')
                })
                .catch(() => {})
        },

        handleApprovalYes(row) {
            const id = row.id
            this.$modal
                .confirm('该卡剩余余额' + row.rechargeBalance + '元，赠送金额' + row.giveBalance + '元，可退金额' + row.rechargeBalance + '元，请确定同意该客户的退卡申请，同意后，金额将退回支付账户”')
                .then(function () {
                    return refundApprovalYes(id)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('审核完成')
                })
                .catch(() => {})
        },

        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateOrder(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addOrder(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 delOrder(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/recharge/order/export',
                {
                    ...this.queryParams
                },
                `order_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
