/**
 * SRM_ZZ_021 制造商基本资质调查表
 * SRM_ZZ_020 代理商准入门槛Checklist
 * @author BOGON
 * @version 1.0
 */

var page = {
    editIndex: -1,
    submitFlag: 0,
    initDatagrid: function () {
        $("#tb").edatagrid({
            singleSelect: true,
            height: document.documentElement.clientHeight -80,
            autoSave: true,
            striped: true,
            method: "GET",
            onEdit: function (index) {
                page.editIndex = index
            },
            url: g.ctx + 'pc/supplier/supplierQualification/rest/reportList?id=' + id + '&purchaseCategoryCode=' + purchaseCategoryCode,
            columns: [[
                {
                    field: 'qualificationName', title: '资质名称', width: '145', align: 'left'
                }, {
                    field: 'fileName',
                    title: '自评报告',
                    width: '285',
                    align: 'left',
                    formatter: function (value, row) {
                        if (!row.fileName) {
                            return ''
                        }
                        return "<a href='" + g.ctx + "pc/sys/file/rest/download/" + row.fileId + "'>" + row.fileName + "</a>"
                    },
                    editor: {
                        type: 'textbox', options: {
                            editable: false,
                            buttonText: '选择文件',
                            onClickButton(e) {
                                $("#fileUpload1 .filebox-label").click();
                            }
                        }
                    }
                }, {
                    field: 'sqeFileName',
                    title: '智微复核报告',
                    width: '290',
                    align: 'left',
                    formatter: function (value, row) {
                        console.log(row)
                        if (!row.sqeFileName) {
                            return ''
                        }
                        return "<a href='" + g.ctx + "pc/sys/file/rest/download/" + row.sqeFileId + "'>" + row.sqeFileName + "</a>"
                    },
                    editor: {
                        type: 'textbox', options: {
                            editable: false,
                            buttonText: '选择文件',
                            onClickButton(e) {
                                $("#fileUpload .filebox-label").click();
                            }
                        }
                    }
                }
            ]]
        });
    },
    fileUpload: function () {
        var files = $("#fileInput").filebox("files");
        debugger;
        if (files.length <= 0) {
            return
        }
        var formData = new FormData();
        formData.append('file', files[0]);//默认的文件数据名为“Filedata”
        var index_wait;
        $.ajax({
            url: g.ctx + 'pc/sys/file/rest/upload/file', //单文件上传
            type: 'POST',
            processData: false,
            contentType: false,
            data: formData,
            beforeSend: function () {
                index_wait = topWin.layer.load(0, {  //发送请求前调用load方法
                    shade: [0.5, '#fff'],  //0.5透明度的白色背景
                });
            },
            complete: function () {
                topWin.layer.close(index_wait);
                $("#fileInput").filebox("clear");
            },
            success: function (result) {
                $("#tb").edatagrid("endEdit", page.editIndex);
                $("#tb").edatagrid('updateRow', {
                    index: page.editIndex,
                    row: {
                        sqeFileId: result.id,
                        sqeFileName: result.fileName
                    }
                });
                topWin.layer.msg('上传成功', {icon: 1});
            },
            error: function (xhr, status, error) {
                topWin.layer.alert('文件上传失败', {icon: 2})
            }
        });
    }, fileUploadNew: function () {
        var files = $("#fileInputNew").filebox("files");
        if (files.length <= 0) {
            return
        }
        var formData = new FormData();
        formData.append('file', files[0]);//默认的文件数据名为“Filedata”
        var index_wait;
        $.ajax({
            url: g.ctx + 'pc/sys/file/rest/upload/file', //单文件上传
            type: 'POST',
            processData: false,
            contentType: false,
            data: formData,
            beforeSend: function () {
                index_wait = topWin.layer.load(0, {  //发送请求前调用load方法
                    shade: [0.5, '#fff'],  //0.5透明度的白色背景
                });
            },
            complete: function () {
                topWin.layer.close(index_wait);
                $("#fileInputNew").filebox("clear");
            },
            success: function (result) {
                $("#tb").edatagrid("endEdit", page.editIndex);
                $("#tb").edatagrid('updateRow', {
                    index: page.editIndex,
                    row: {
                        fileId: result.id,
                        fileName: result.fileName
                    }
                });
                topWin.layer.msg('上传成功', {icon: 1});
            },
            error: function (xhr, status, error) {
                topWin.layer.alert('文件上传失败', {icon: 2})
            }
        });
    },
    submit: function (callback) {
        if (page.submitFlag === 1) {
            return
        }
        $("#tb").edatagrid("saveRow")
        let rows = $("#tb").edatagrid("getRows")
        let load = topWin.layer.load(0);
        page.submitFlag = 1
        $.ajax({
            type: "post",
            url: g.ctx + "pc/supplier/supplierQualification/rest/reportSave",
            contentType: "application/json",
            data: JSON.stringify(rows),
            complete: function () {
                topWin.layer.close(load);
                page.submitFlag = 0
            },
            success: function (data) {
                addOrupdateApprovalOpinion(id);
                topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    callback();
                });
            },
            error: function (data) {
                topWin.layer.alert(JSON.parse(data.responseText).message, {icon: 2});
            }
        });
    }
}
$(function () {
    page.initDatagrid()
})

//保存SEQ准入审批原因
function addOrupdateApprovalOpinion(id) {
    debugger;
   var approvalOpinion =  $('#approvalOpinion').val();
    $.ajax({
        type: "post",
        url: g.ctx + "pc/supplier/rest/updateApprovalOpinion",
        data: {id: id, approvalOpinion: approvalOpinion},
        success: function (data) {

        },
        error: function (data) {
            topWin.layer.alert(JSON.parse(data.responseText).message, {icon: 2});
        }
    });

}