<template>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1 id="itemTitle"></h1>
                </section>
            </div>
        </div>
        <div id="table"></div>
    </section>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';
import { showLoading, hideLoading } from'@/utils/maintainer';

export default {
    name: "AnchorBillCheckView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                //获取模板列表
                //初始化表格
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        // {field: 'temp', title: 'id', align: 'center', checkbox: true, width: '5%'},
                        { field: 'id', title: 'ID', align: 'center', width: '5%' },
                        { field: 'adminName', title: '上传者', align: 'center', width: '5%' },
                        {
                            field: 'uploadTime',
                            title: '上传时间',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        },
                        { field: 'realName', title: '文件名', align: 'center', width: '5%' },
                        {
                            field: 'fileName', title: '下载', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                var downloadLink = '/admin/anchor/downloadExcel?id=' + row.id;
                                var alink = '<a href="' + downloadLink + '">' + row.realName + ' </a>    ';
                                // window.location.href=
                                return alink;
                            }
                        },
                        {
                            field: 'status', title: '状态', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                if (val == 1) {
                                    return "审核中";
                                } if (val == 2) {
                                    return "审核通过";
                                } if (val == 3) {
                                    return "驳回";
                                } if (val == 4) {
                                    return "已导入";
                                } else {
                                    return '-';
                                }
                            }
                        },
                        { field: 'checkAdminName', title: '审核人', align: 'center', width: '5%' },
                        {
                            field: 'checkTime',
                            title: '审核时间',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        }
                        ,
                        {
                            field: 'id',
                            title: '操作',
                            align: 'center',
                            width: '15%',
                            formatter: function (val, row, index) {
                                var result = '';
                                // console.log(row.status)
                                if (row.status == 1) {
                                    result += '<button id="agree" name="agree" class="btn btn-sm btn-success btn-agree" data-id=' + val + '>' +
                                        '<i class="glyphicon glyphicon-ok"></i>同意</button>&nbsp;&nbsp;';
                                    result += '<button id="reject" name="reject" class="btn btn-sm btn-success btn-reject" data-id=' + val + '>' +
                                        '<i class="glyphicon glyphicon-book"></i>驳回</button>&nbsp;&nbsp;'
                                }
                                return result;

                            }
                        }
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [20, 50, 100, 200, 300, 500],
                    search: false,
                    sidePagination: "server", //表示服务端请求
                    //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                    //设置为limit可以获取limit, offset, search, sort, order
                    queryParamsType: "undefined",
                    queryParams: function queryParams(params) {   //设置查询参数
                        var param = {
                            page: params.pageNumber,
                            pageSize: params.pageSize,
                            erbanNo: $('#erbanNo').val(),
                            templateId: $('#templateId').val(),
                            sendStatus: $('#sendStatus').val(),
                            createTime: $('#createTime').val(),
                            sendTime: $('#sendTime').val()
                        };
                        return param;
                    },
                    toolbar: '#toolbar',
                    url: '/admin/anchor/uploadList',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });
                $("#btnSearch").click(function () {
                    TableHelper.doRefresh('#table');
                })

                $("#btnImport").click(function () {
                    $("#uploadFile").val("");
                    $("#fileUpload").modal('show');
                });

                $("#upload").click(function () {
                    var file = $("#uploadFile").val();
                    if (file == null || file == undefined || file == '') {
                        $("#tipMsg").text("上传文件不能为空.");
                        $("#tipModal").modal('show');
                    }
                    debugger;
                    $("#fileUpload").modal('hide');
                    showLoading();
                    var option = ({
                        type: "POST",
                        url: "/admin/anchor/uploadExcel",
                        cache: false,
                        contentType: false,    //不可缺
                        processData: false,    //不可缺
                        dataType: "json",
                        success: function (json) {
                            hideLoading();
                            console.log(json)
                            if (json.code == 200) {
                                $("#tipMsg").text("上传成功");
                                $("#tipModal").modal('show');
                                TableHelper.doRefresh('#table');
                            } else {
                                $("#tipMsg").text("上传失败.");
                                $("#tipModal").modal('show');
                            }
                        },
                        error: function () {
                            hideLoading();
                            $("#tipMsg").text("上传失败.");
                            $("#tipModal").modal('show');
                        }
                    });
                    $("#uploadForm").ajaxSubmit(option);
                });

                //同意
                $('#table').on('click', '.btn-agree', function () {
                    var key = parseInt($(this).data('id'));
                    console.log(key, typeof key);
                    if (confirm("你确定要同意该流水账单？")) {
                        $.ajax({
                            type: 'post',
                            url: '/admin/anchor/check/agree',
                            data: { 'id': key },
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    $("#tipMsg").text("操作成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("操作失败，错误信息：" + data.message);
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                })

                //同意
                $('#table').on('click', '.btn-reject', function () {
                    var key = parseInt($(this).data('id'));
                    console.log(key, typeof key);
                    if (confirm("你确定要驳回该流水账单？")) {
                        $.ajax({
                            type: 'post',
                            url: '/admin/anchor/check/reject',
                            data: { 'id': key },
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    $("#tipMsg").text("操作成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("操作失败，错误信息：" + data.message);
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                })
            });
        }
    },
    unmounted() {
        $('#table').bootstrapTable('destroy');
    }
};
</script>

<style scoped></style>