/**
 * 数据管理-采购组织管理首页JS
 *
 * Created by li.kui on 20203/17.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    pgCode: '#pgCode',
    pgName: '#pgName',
    poCode: '#poCode',
    orgCodeOrName: '#orgCodeOrName',
    tb: '#tb',
    addOrEditDialog: null,
    lookDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    loadGrid();
});


function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 40, 30);

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/supplier/supRelationBid/rest/list',
        singleSelect: true,
        height: el.bodyHeight - searchHeight,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'faSupplier',
                    title: '物流商编码',
                    width: 120
                },
                {
                    field: 'faSupplierName',
                    title: '物流商名称',
                    width: 200
                },
                {
                    field: 'supplierCode',
                    title: '供应商编码',
                    width: 200
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 240,
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(0);
                }
            },
            "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(1);
                }
            },
            {
                text: '导入',
                iconCls: 'icon-edit',
                handler: function () {
                    importDicItem();
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delFactory();
                }
            }
        ]
    });
}

/**
 * 导入
 */
function importDicItem() {
    var a = openImportExcelDialog("static/excel/supplier_relation_list.xlsx", '供应商关系导入', function (file) {
        let formData = new FormData();
        formData.append("file", file);
        //formData.append("dictTypeId", $('#id').val())
        //取到fileId然后进行数据导入操作
        $.ajax({
            url: g.ctx + 'pc/supplier/supRelationBid/rest/import',
            method: "POST",
            contentType: false,
            processData: false,
            data: formData,
            success: function (data) {
                topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    a.dialog('close');
                    $('#tb').datagrid('reload');
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}
/**
 * 新增或编辑采购组
 *
 * @param type 0:新增 1:编辑
 */
function addOrEdit(type) {
    var id= '';
    if(type===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
        console.log()
    }
    console.log(id)
    console.log(88888888888888888888)
    var purchaseOrgDialog = openDialog('purchaseOrg', g.ctx + 'pc/supplier/supRelation/detail?id=' + id, {
        title: type===0?'新增':'编辑',
        width: '700px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            purchaseOrgDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['purchaseOrg'].saveOrUpdate(function () {
                        topWin.layer.msg(tipsi18n.czcg, {icon: 1});
                        $(el.tb).datagrid('reload');
                        purchaseOrgDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    purchaseOrgDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 删除采购组
 */
function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }
        debugger
    var id = cbox.id;
    console.log(id)
    console.log(88888888888888888888)
    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/supplier/supRelationBid/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}


