<template>
    <div class="app-container">
        <div class="flexBet searchTop">
            <div class="title flex m20">
                <div class="bar"></div>
                <div class="text">{{ $route.meta.title }}</div>
            </div>

        </div>
        <div class="bg m20">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="86px"
                class="flex form" style="padding:0 20px">
                <el-form-item label="商户名称" prop="storeName">
                    <el-input size="medium" v-model="queryParams.storeName" placeholder="请输入商户名称"
                        @keyup.enter.native="handleQuery" style="width: 215px;border-color: #E6F1FF;" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="" size="medium" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table stripe v-loading="loading" :data="infoList" ref="multipleTable" @select-all="onSelectAll"
                @selection-change="selectItem" @row-click="onSelectOp">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="'商户编号'" align="center" prop="id" />
                <el-table-column :label="'商户名称'" align="center" prop="storeName" />
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>

        <div class="center m20">
            <p>正在发起批量内充，请下载批量内充模板填写上传。</p>
            <p style="color:red">请按照模板填写，不要修改模板格式。</p>
            <a :href="url" style="color:#197CFE">批量内充模板.xls</a>
        </div>

        <p style="color:#317EAC;margin-left: 10px;font-size: 14px;">批量代付<span
                style="color:red">(1、下载内充模板->2、上传批量内充excel文件->3、系统解析文件并生成内充订单->4、平台进行订单审核加款->5、查看审核及加款</span></p>


        <div class="bottom" style="width:25%">
            <el-form ref="form" :model="form" :rules="rules" label-width="130px">
                <el-form-item label="内充渠道" prop="channelContent">
                    <span style="color:red" v-if="form.channelContent" v-hasPermi="['record:manual_query_channel']">{{
                        form.channelContent }}</span>
                </el-form-item>

                <el-form-item label="上传批量内充文件" prop="channelContent">
                    <el-upload class="upload-demo" action="#" v-model="fileList" accept=".xls, .xlsx"
                        :show-file-list="false" :multiple="false" :on-change="onChange" :auto-upload="false">
                        <el-button slot="trigger" size="medium" type="primary">选择文件</el-button><span
                            style="margin-left:5px">{{ fileName }}</span>
                    </el-upload>

                </el-form-item>

                <el-form-item label="收款账号" prop="acctName">
                    <el-select v-model="form.acctName" placeholder="请选择收款账号" size="medium" v-if="bankAccount.length"
                        @change="getValue" v-hasPermi="['record:manual_query_bank']">
                        <el-option v-for="dict in bankAccount" :key="dict.acctName" :label="dict.acctName"
                            :value="dict.acctName" />
                    </el-select>
                </el-form-item>

                <div class="footer" style="margin-left:100px">
                    <el-button type="primary" icon="" size="small" @click="submitForm"
                        v-hasPermi="['record:manual_batch_save']">提交</el-button>
                    <el-button plain size="small" @click="back">取消</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>
  
<script>
import { common_api, manual_recharge_list } from "@/api/form";

export default {
    name: "PkAdWeiList",
    data() {
        return {
            multipleSelection: [],
            fileName: '',
            bankAccount: [],
            fileList: [],
            url: '',
            detailData: {},
            isShowDetail: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            dateRange: [],
            // 新增广告图片地址
            imageUrl: "",
            // 遮罩层
            loading: false,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 广告表格数据
            infoList: [
            ],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                storeName: null,
            },

            // 表单参数
            form: {},
            // 表单校验
            rules: {
                // acctName: [
                //     { required: true, message: "请选择收款账号", trigger: "blur" },
                // ],
            },
        };
    },
    created() {
    },
    methods: {
        getValue(e) {
            for (var i = 0; i < this.bankAccount.length; i++) {
                if (e == this.bankAccount[i].acctName) {
                    this.form.bankId = this.bankAccount[i].bankId
                }
            }
            this.$forceUpdate()
        },
        onChange(file, fileList) {
            if (fileList.length > 0) {
                this.fileList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
                this.fileName = this.fileList[0].name

            }
        },
        // 上传成功，回调
        success(res, file) {
            console.log(res, file.name, '上传成功')
        },
        back() {
            this.$router.go(-1)
        },
        // 单选操作
        onSelectAll() {
            this.$refs.multipleTable.clearSelection();
        },
        selectItem(rows) {
            if (rows.length > 1) {
                const newRows = rows.filter((it, index) => {
                    if (index == rows.length - 1) {
                        this.$refs.multipleTable.toggleRowSelection(it, true);
                        return true;
                    } else {
                        this.$refs.multipleTable.toggleRowSelection(it, false);
                        return false;
                    }
                });
                this.multipleSelection = newRows;

            } else {
                this.multipleSelection = rows;

            }
            this.form.storeId = this.multipleSelection[0].id
            this.init(this.multipleSelection[0])

        },
        onSelectOp(row) {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleRowSelection(row, true);
            this.multipleSelection = [];
            this.multipleSelection.push(row);
        },

        init(val) {

            this.form.userId = val.userId
            //  单笔内充-内充渠道(根据用户id查询)
            manual_recharge_list
                .manual_query_channel({
                    storeUserId: val.userId
                })
                .then((response) => {
                    this.form.channelContent = response.data.extends1
                    this.form.channelType = response.data.extends2
                    this.$forceUpdate()
                });
            //   单笔内充-内充商户收款账号(根据用户id查询)
            manual_recharge_list
                .manual_query_bank({
                    storeUserId: val.userId
                })
                .then((response) => {
                    if (response.data.length) {
                        this.bankAccount = response.data
                        this.form.acctName = this.bankAccount[0].acctName
                        this.form.bankId = this.bankAccount[0].bankId
                    }
                });
            // 内充模板
            manual_recharge_list
                .downloadManualTemplate()
                .then((response) => {
                    this.url = response.msg
                });

        },

        getList() {
            this.loading = true;
            manual_recharge_list
                .manual_query_store(this.addSESDateRange(this.queryParams, this.dateRange))
                .then((response) => {
                    this.infoList = response.data;
                    this.total = Number(response.total);
                    this.loading = false;
                });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {};
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 提交按钮 */
        submitForm() {

            if (!this.fileList.length) {
                this.$message.error('请上传内充文件')
            }
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    let formData = new FormData();
                    formData.append("file", this.fileList[0].raw);
                    formData.append("userId", this.form.userId)
                    formData.append("storeId", this.form.storeId)
                    formData.append("channelType", this.form.channelType)
                    formData.append("bankId", this.form.bankId)
                    const loading = this.$loading({
                        lock: true,
                        text: `正在提交内充`,
                        spinner: "el-icon-loading",
                        background: "rgba(0, 0, 0, 0.7)",
                    });
                    manual_recharge_list.manual_batch_save(formData).then((response) => {
                        loading.close();
                        this.msgSuccess("内充提交成功");
                        this.getList();
                        this.back()
                    }).catch(() => {
                        loading.close();
                    })
                }
            });
        },

    },
};
</script>
<style scoped lang="scss">
::v-deep .el-table__header-wrapper .el-checkbox {
    //找到表头那一行，然后把里面的复选框隐藏掉
    display: none
}

.center {
    padding: 8px 15px;
    border-radius: 15px;
    background: #F5F5F5;
    font-size: 13px;
}

::v-deep .el-dialog__body {
    padding: 20px 20px;
}

::v-deep .el-dialog__headerbtn {
    top: 10px;
}

::v-deep .el-dialog__header {
    background: #F8F8F8;
    padding: 8px;

    .el-dialog__title {
        font-size: 14px;
    }
}

::v-deep .el-dialog:not(.is-fullscreen) {
    margin-top: 35vh !important;
}</style>