var el = {
    tb: '#tb',
    businessId: '#businessId',
    ids: '#ids',
    businessTypeCode: '#businessTypeCode'
};
$(function () {
    initDict('sign_order,sign_type_two,sign_type')
    // 加载页面输入或下拉框数据
    loadGrid();

});

function loadGrid(){
    var bsids = $(el.ids).val();
    var businessTypeCode = $(el.businessTypeCode).val();
    var dowloadUrl = g.ctx + 'pc/sys/esign/rest/file/list?businessId=' + $(el.businessId).val() + '&businessTypeCode=' + $(el.businessTypeCode).val();
    var toolbar = [];
    //如果是采购订单签署附件下载
    if (businessTypeCode == "purchase_order" && bsids != undefined && bsids != '' && !isSupplier) {
        dowloadUrl = g.ctx + 'pc/sys/esign/rest/file/lists?businessId=' + $(el.ids).val() + '&businessTypeCode=' + $(el.businessTypeCode).val();
        toolbar.push({
            text: "一键下载", iconCls: 'icon-download', handler: function () {
                let rows = $(el.tb).datagrid("getRows");
                $.each(rows, function (idx, itm) {
                    if (itm.deleteFlag != 1) {
                        window.open(g.ctx + 'pc/sys/file/rest/download/' + itm.fileId);
                    }
                })
            }
        })
    } else {
        toolbar.push({
            text: '删除',
            iconCls: 'icon-remove',
            id: 'btn_remove',
            handler: function () {
                remove();
            }
        })
    }

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: dowloadUrl,
        height: '460px',
        width: '98%',
        pagination: false,
        autoRowHeight: false,
        remoteSort:false,
        fitColumns: true,
        checkbox: true,
        rownumbers: false,
        queryParams: {},
        toolbar: toolbar,
        // toolbar: [
        //     {
        //         text: '删除',
        //         iconCls: 'icon-remove',
        //         id: 'btn_remove',
        //         handler: function () {
        //             remove();
        //         }
        //     }
        // ],
        columns: [[
           {
                field: 'fileId',
                title: '签署文件',
                width: '40%',
                formatter: function (value, row, index) {
                    return '<div class="supplier-file-form custom-file-form-style" data-options=\'{"deleteEnable":false, "previewEnable":true, "uploadEnable": false, "moreFileUploadEnable": true, "fileSize": 50, "fileTypes": ".xls,.doc,.txt,.pdf"}\' data-fileIds = "'+value+'"></div>';
                }
            }, {
                field: 'signStatus',
                title: '状态',
                width: '10%',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return '<span style="color: grey;">草稿</span>';
                        case 1:
                            return '<span style="color: blue;">签署中</span>';
                        case 2:
                            return '<span style="color: green;">完成</span>';
                        case 3:
                            return '<span style="color: orange;">已过期</span>';
                        case 4:
                            return '<span style="color: red;">已拒签</span>';
                        case 5:
                            return '<span style="color: dimgrey;">已作废</span>';
                        case 6:
                            return '<span style="color: blue;">作废中</span>';
                        case 7:
                            return '<span style="color: red;">已删除</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'signOrder',
                title: '签章顺序',
                width: '10%',
                formatter: function (value) {
                    return getDicName('sign_order',value)
                }

            },
            {
                field: 'signatureType',
                title: '签章类别',
                width: '10%',
                formatter: function (value) {
                    return getDicName('sign_type',value)
                }

            },
            {
                field: 'deleteRemark',
                title: '删除原因',
                width: '15%'
            },
            {
                field: 'createTime',
                title: '上传时间',
                width: '15%',
                formatter: function (value) {
                    if (!value) {
                        return;
                    }
                    return moment(new Date(value)).format('YYYY-MM-DD HH:mm:ss');
                }

            },
        ]],
        onLoadSuccess: function (data) {
            initFileForm('supplier-file-form');
        },
    });
}

function remove(){
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }

    var id = row.id;
    var businessTypeCode = $(el.businessTypeCode).val();
    if (businessTypeCode === "account_statement" || businessTypeCode === "company_account_statement" || businessTypeCode === "purchase_sale_contract"||businessTypeCode === "purchase_sale_process_contract") {
        //电子印章签署 先根据对账单号 查询申请单中，是否存在且状态为发票已确认状态 select * from fin_invoiced_apply fia  where  account_statement_no = '24000091'
        //  and status = 9 如果存在则提示该单已经为发票已确认状态，不可删除附件操作。

        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/finance/invoiced/apply/rest/checkDelete?accountStatementNo=&type=2&accountStatementId=' + $(el.businessId).val()+'&businessTypeCode='+businessTypeCode+'&id='+id,
            type: 'GET',
            processData: false,
            contentType: false,
            success: function () {
                topWin.layer.prompt({title: "请填写删除原因", formType: 2, value: '', maxlength: 500}, function(text, index){
                    if(text ===''){
                        topWin.layer.msg('请填写删除原因', {icon: 2});
                        return;
                    }
                    var loading = topWin.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
                    $.ajax({
                        url: g.ctx + 'pc/sys/esign/rest/remove',
                        method: "post",
                        data: {id: id,deleteRemark:text},
                        success: function () {
                            topWin.layer.close(index);
                            topWin.layer.msg('数据保存成功', {icon: 1});
                            $(el.tb).datagrid('reload');
                            $(el.tb).datagrid('clearSelections');
                        },
                        complete: function(data) {
                            topWin.layer.close(loading);
                        },
                        error: function (data) {
                            parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
                        }
                    });
                });
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    } else {
        topWin.layer.prompt({title: "请填写删除原因", formType: 2, value: '', maxlength: 500}, function(text, index){
            if(text ===''){
                topWin.layer.msg('请填写删除原因', {icon: 2});
                return;
            }
            var loading = topWin.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
            $.ajax({
                url: g.ctx + 'pc/sys/esign/rest/remove',
                method: "post",
                data: {id: id,deleteRemark:text},
                success: function () {
                    topWin.layer.close(index);
                    topWin.layer.msg('数据保存成功', {icon: 1});
                    $(el.tb).datagrid('reload');
                    $(el.tb).datagrid('clearSelections');
                },
                complete: function(data) {
                    topWin.layer.close(loading);
                },
                error: function (data) {
                    parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
                }
            });
        });
    }
}