<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
            <el-form-item label="平台订单号" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入平台订单号" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="商户订单号" prop="outTradeNo">
                <el-input v-model="queryParams.outTradeNo" placeholder="请输入商户订单号" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="付款地址" prop="payAddress">
                <el-input v-model="queryParams.payAddress" placeholder="请输入付款地址" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="支付商户名" prop="merchantName">
                <el-input v-model="queryParams.merchantName" placeholder="请输入支付商户名" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="订单金额" prop="tranAmt">
                <el-input v-model="queryParams.tranAmt" placeholder="请输入订单金额" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="订单状态" prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择状态" clearable size="small">
                    <el-option v-for="dict in sysOrderStatus" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="dict.dictValue" />
                </el-select>
            </el-form-item>

            <!-- <el-form-item label="订单来源" prop="sourceType">
                <el-select v-model="queryParams.sourceType" placeholder="请选择订单来源" clearable size="small">
                    <el-option v-for="dict in sourceTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                        :value="dict.dictValue" />
                </el-select>
            </el-form-item> -->

            <el-form-item label="创建时间">
                <el-date-picker v-model="dateRange" size="small" style="width: 340px" value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                    :unlink-panels="true" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
            </el-form-item>

            <el-form-item>
                <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-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain size="mini" @click="handleTest">测试下单</el-button>
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="otcCoinList">
            <el-table-column label="平台订单号" align="center" prop="orderNo" />
            <el-table-column label="商户订单号" align="center" prop="outTradeNo" />
            <el-table-column label="商户编号" align="center" prop="merchantNo" />
            <el-table-column :label="'订单金额'" align="center" prop="tranAmt">
                <template slot-scope="scope">
                    {{ (NumberDiv(scope.row.tranAmt, 100)) }}
                </template>
            </el-table-column>
            <el-table-column :label="'实付金额'" align="center" prop="payAmt">
                <template slot-scope="scope">
                    {{ (NumberDiv(scope.row.payAmt, 100)) }}
                </template>
            </el-table-column>
            <el-table-column label="地址" align="center" prop="address" />
            <el-table-column label="付款地址" align="center" prop="payAddress" />
            <el-table-column label="支付商户名" align="center" prop="merchantName" />
            <el-table-column label="商户费率（%）" align="center" prop="merchantRate" />
            <el-table-column :label="'商户单笔手续费'" align="center" prop="merchantSingleFee">
                <template slot-scope="scope">
                    {{ (NumberDiv(scope.row.merchantSingleFee, 100)) }}
                </template>
            </el-table-column>
            <el-table-column label="异步通知状态" align="center" prop="notifyStatus" :formatter="notifyStatusFormat" />

            <el-table-column label="订单来源" align="center" prop="sourceType" :formatter="sourceTypeFormat" />
            <el-table-column label="变动来源" align="center" prop="source" :formatter="sysOrderStatusFormat" />
            <el-table-column label="创建时间" align="center" prop="createTime" min-width="180" />
            <!-- <el-table-column label="更新时间" align="center" prop="updateTime" min-width="180" /> -->

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="small" type="text" style="color: #006EFF;" v-if="scope.row.orderStatus == 1"
                        @click="handleOrder(scope.row)">手工处理</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 v-dialogDrag title="手工处理" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false"
            :before-close="cancel">
            <el-form ref="formDeal" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="订单状态" prop="status">
                    <el-select v-model="form.status" placeholder="请选择订单状态" clearable size="medium">
                        <el-option v-for="(item, i) in orderStatusType" :key="i" :label="item.label"
                            :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="谷歌动态验证码" prop="googleCode">
                    <el-input v-model="form.googleCode" placeholder="请输入谷歌动态验证码" style="width:217px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :disabled="!form.status || !form.googleCode"
                    :type="form.status && form.googleCode ? 'primary' : 'info'" icon="" size="small"
                    @click="submitForm('processsuccess', 'formDeal')">确定</el-button>
                <el-button plain size="small" @click="cancel">取消</el-button>
            </div>
        </el-dialog>


        <!-- 测试下单 -->
        <el-dialog v-dialogDrag title="测试下单" :visible.sync="openTest" width="500px" append-to-body
            :close-on-click-modal="false" :before-close="cancel">
            <el-form ref="formTest" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="商户" prop="merchantId">
                    <el-select v-model="form.merchantId" placeholder="请选择商户" clearable size="medium">
                        <el-option v-for="(item, i) in merchantCon" :key="i" :label="item.username"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="金额" prop="amount">
                    <el-input v-model="form.amount" placeholder="请输入金额" style="width:217px" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" icon="" size="small" @click="submitForm('test', 'formTest')">确定</el-button>
                <el-button plain size="small" @click="cancel">取消</el-button>
            </div>
        </el-dialog>

    </div>
</template>
  
<script>
import { payRecordRecharge, common_api } from "@/api/form";
export default {
    name: "otcStoreRechargeOrder",
    components: {
    },
    data() {
        return {
            orderStatusType: [
                {
                    label: '成功',
                    value: 2,
                },
                {
                    label: '失败',
                    value: 3,
                },
            ],
            // 所有商户
            merchantCon: [],
            dateRange: [],
            sourceTypeOptions: [],
            sysOrderStatus: [],
            notifyStatusOptions: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // OTC币种表格数据
            otcCoinList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            openTest: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderNo: null,
                outTradeNo: null,
                payAddress: null,
                merchantName: null,
                tranAmt: null,
                orderStatus: null,
                notifyStatus: null,
                sourceType: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                merchantId: [
                    { required: true, message: "请选择商户", trigger: "blur" },
                ],
                amount: [
                    { required: true, message: "请输入金额", trigger: "blur" },
                ],
            },
        };
    },
    created() {
        this.getList();

        this.getDicts("notify_status").then(response => {
            this.notifyStatusOptions = response.data;
        });
        this.getDicts("sys_source_type").then(response => {
            this.sourceTypeOptions = response.data;
        });
        this.getDicts("sys_order_status").then(response => {
            this.sysOrderStatus = response.data;
        });
        this.init();
    },
    methods: {
        init() {
            common_api.allmerchant().then((response) => {
                this.merchantCon = response.data
            })
        },
        /** 测试下单 */
        handleTest() {
            this.reset()
            this.openTest = true
        },
        /** 手工处理 */
        handleOrder(row) {
            this.reset()
            this.form = row
            this.open = true
        },
        /** 查询OTC币种列表 */
        getList(v) {
            this.loading = true;
            let queryParams = {
                pageNum: this.queryParams.pageNum,
                pageSize: this.queryParams.pageSize,
                orderNo: this.queryParams.orderNo,
                outTradeNo: this.queryParams.outTradeNo,
                payAddress: this.queryParams.payAddress,
                merchantName: this.queryParams.merchantName,
                tranAmt: v,
                orderStatus: this.queryParams.orderStatus,
            }
            payRecordRecharge.listInfo(this.addSESDateRange(queryParams, this.dateRange)).then(response => {
                this.otcCoinList = response.rows;
                this.total = Number(response.total);
                this.loading = false;
            });
        },
        notifyStatusFormat(row, column) {
            return this.selectDictLabel(this.notifyStatusOptions, row.notifyStatus);
        },
        sourceTypeFormat(row, column) {
            return this.selectDictLabel(this.sourceTypeOptions, row.sourceType);
        },
        sysOrderStatusFormat(row, column) {
            return this.selectDictLabel(this.sysOrderStatus, row.orderStatus);
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.openTest = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            let tranAmt;
            this.queryParams.tranAmt ? tranAmt = this.NumberMul(this.queryParams.tranAmt, 100) : tranAmt = null
            this.getList(tranAmt);
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 提交按钮 */
        submitForm(t, f) {
            this.$refs[f].validate(valid => {
                if (valid) {
                    if (t == 'test') {
                        let amount = this.NumberMul(this.form.amount, 100)
                        payRecordRecharge.recharge({
                            amount: amount,
                            merchantId: this.form.merchantId
                        }).then((response) => {
                            this.openTest = false;
                            this.msgSuccess("操作成功");
                            this.getList();
                        }).catch(() => {
                        })
                    } else {
                        if (!this.form.status) {
                            return;
                        }
                        let form = {
                            id: this.form.keyId,
                            status: this.form.status,
                            googleCode: this.form.googleCode
                        }
                        payRecordRecharge.payHandle(form).then((response) => {
                            this.open = false;
                            this.msgSuccess("处理成功");
                            this.getList();
                        }).catch(() => {
                        })
                    }
                }

            });
        },

        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return payRecordRecharge.exportList(queryParams);
            }).then(response => {
                this.download(response.msg);
            })
        }
    }
};
</script>
  