/**
 * 交付模块-标准预测计划首页JS
 *
 * Created by jt on 2019/9/16.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    isSupplier: '#isSupplier',
    fileId: '#fileId',
    title: '#title',
    createName: '#createName',
    tb: '#tb',
    // chooseDate: '#chooseDate',
    // excelFile: '#excelFile',
    // uploadBoxHtml: '#uploadBoxHtml',
    planReleaseDialog: null,
    supplierReplyDialog: null,
    purchaseConfirmDialog: null,
    byDateShowDetailDialog: null,
    lookReplyProgressDialog: null,
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    // 是否为供应商用户 1:是 0:否
    var isSupplier = $(el.isSupplier).val();
    initDataGrid(isSupplier);
    // 初始化文件上传控件
    // $(el.excelFile).filebox({
    //     buttonText: '选择文件',
    //     buttonAlign: 'right',
    //     accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel',
    //     multiple: false,
    //     prompt: ''
    // });
    // 获取本月的第一天
    // var firstDayOfMonth = moment().startOf('month');
    // // 格式化为 yyyy-MM-dd
    // var formattedDate = firstDayOfMonth.format('YYYY-MM-DD');
    // $("#chooseDate").datebox("setValue", formattedDate)
});


/**
 * 初始化表格
 */
function initDataGrid(isSupplier) {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 40, 30);

    $(el.tb).datagrid({
        loadMsg: '数据远程载入中,请等待...',
        url: g.ctx + 'pc/order/shortagePlan/rest/fileLogList',
        height: el.bodyHeight - searchHeight,
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'id',
                    hidden: true
                },
                {
                    field: 'createTime',
                    title: itableTitlei18n.cjsj,
                    width: 170
                },
                {
                    field: 'createName',
                    title: itableTitlei18n.cjr,
                    width: 120
                },
                {
                    field: 'fileName',
                    title: "文件名",
                    width: 350,
                    formatter: function (value, row) {
                        if (row.fileType === 1 ||　row.fileType === 3) {
                            return '<a href="javascript:void(0);" onclick="downloadFile(' + row.id + ')" data-type="basic" data-pid="' + row.id + '">' + value + '</a>'
                        } else if (row.fileType === 2 || row.fileType === 4) {
                            //2：欠料计划发布导入 4:欠料计划回复导入 不提供下载 返回文件名
                            return value;
                        }
                        return "";
                    }
                },
                {
                    field: 'fileType',
                    title: "文件类型",
                    width: 120,
                    formatter: function (value) {
                        if (value === 1) {
                            return '发布导出';
                        } else if (value === 2) {
                            return '发布导入'
                        } else if (value === 3) {
                            return '回复导出'
                        } else if (value === 4) {
                            return '回复导入'
                        }
                        return ''
                    }
                },
                {
                    field: 'fileSizeStr',
                    title: "文件大小",
                    width: 100
                },
                {
                    field: 'msg',
                    title: "操作信息",
                    width: 220
                },
                {
                    field: 'status',
                    title: "执行结果",
                    width: 100,
                    formatter: function (value, row) {
                        //1：发布或回复数据导出中 2 发布或回复数据导出成功,  3 发布数据导入成功 4:发布或回复数据导出（入）失败  5: 回复数据正在导入中   6:回复数据导入成功，等待提交sap 7：没有要推送sap的欠料数据 8：推送sap成功  9:推送sap失败'
                        if (value === "5" ||　value === "1") {
                            return '执行中';
                        } else if (value === "2"  || value === "3" || value === "4" || value === "7" || value === "8") {
                            return '执行完成'
                            //仅支持，导入成功，推送sap失败的重推， status如果==
                        } else if (row.status === "9" || row.status === "6") {
                            // return '<a href="javascript:void(0);" onclick="rePushConfirmReplyImport(' + row.id + ')" data-type="basic" data-pid="' + row.id + '">提交</a>'
                            return '<button id="reconfirm" type ="button" onclick = "rePushConfirmReplyImport('+row.id+')" class= "btn btn-sm pull-left u-ml-18 mr-5 easyui-linkbutton"  data-options="iconCls:\'icon-search\'">提 交</button>'
                        }
                        return "";
                    }
                }
            ]
        ],
        toolbar: [
            // {
            //     text: '删除',
            //     id: 'btn-remove',
            //     iconCls: 'icon-remove',
            //     handler: function () {
            //         deleteDetail();
            //     }
            // }
        ],
        onBeforeLoad: function () {
            displayButtonByUserRole("shortagePlan");
        },
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        }
    });
}

/**
 * 删除
 */

function deleteDetail() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert(itablei8n.plselect, {icon: 0});
        return false;
    }

    topWin.layer.confirm(tipsi18n.sfsc, {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/order/shortagePlan/rest/remove',
            data: {id: row.id},
            type: "POST",
            success: function (data) {
                topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    $(el.tb).datagrid('load');
                });
            },
            error: function (data) {
                topWin.layer.close(idx);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        })
    })
}

/**
 * 下载文件
 *
 * @param ele 当前操作DOM节点
 */
function downloadFile(pid) {
    // var type = $(ele).data('type'), pid = $(ele).data('id');
    var href = g.ctx + 'pc/order/shortagePlan/rest/download/' + pid;
    window.open(href);
}


function rePushConfirmReplyImport(id) {
    topWin.layer.confirm('确认提交?', {icon: 3}, function (index) {
        $.ajax({
            url: g.ctx + 'pc/order/shortagePlan/rest/rePushConfirmReplyImport',
            data: {logId: id},
            // contentType: 'application/json',
            type: 'POST',
            complete: function () {
                //topWin.layer.close(loadingIndex);
            },
            success: function (data) {
                topWin.layer.alert('正在提交,请耐心等待...', {icon: 1}, function (index) {
                    topWin.layer.close(index)
                    $(el.tb).datagrid('load');
                });
            },
            error: function (data) {
                $(el.tb).datagrid('load');
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    })
}


/**
 * 数据查询
 */
function dataQuery() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.title = $.trim($(el.title).val());
    queryParams.createName = $.trim($(el.createName).val());
    $(el.tb).datagrid('load');
}

