/**
 * 数据管理-采购组织管理首页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/order/purchaseOrderBom/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: 'materialCode',
                    title: '套件物料编码',
                    width: 120
                },
                {
                    field: 'materialName',
                    title: '套件物料名称',
                    width: 120
                },
                {
                    field: 'componentCode',
                    title: '子件物料编码',
                    width: 120
                },
                {
                    field: 'componentName',
                    title: '子件物料名称',
                    width: 120
                },
                {
                    field: 'componentNumber',
                    title: '组件数量',
                    width: 120
                },
                {
                    field: 'unit',
                    title: '基本单位',
                    width: 120
                },
                {
                    field: 'componentDosage',
                    title: '子件用量',
                    width: 120
                },
                {
                    field: 'componentUnit',
                    title: '子件单位',
                    width: 120
                },
                {
                    field: 'factoryCode',
                    title: '工厂',
                    width: 120
                },
                {
                    field: 'mesStorageLocation',
                    title: 'MES储位',
                    width: 120
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 120
                },
                {
                    field: 'updateTime',
                    title: '修改时间',
                    width: 120
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                id: 'add',
                handler: function () {
                    addOrEdit(0);
                }
            },
            "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id: 'edit',
                handler: function () {
                    addOrEdit(1);
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                id: 'delete',
                handler: function () {
                    delFactory();
                }
            }
        ],
        onLoadSuccess:function (){
            displayButtonByUserRole('purchaseOrderBom');
        },
    });
}


/**
 * 新增或编辑采购组
 *
 * @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()
    }
    var purchaseOrgDialog = openDialog('purchaseOrg', g.ctx + 'pc/order/purchaseOrderBom/detail?id=' + id, {
        title: type===0?'新增':'编辑',
        width: '800px',
        height: '500px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        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;
    }

    var id = cbox.id;
    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/order/purchaseOrderBom/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);
            }
        });
    });
}


