var bomGridManager = null, technologyGridManager = null;

$(document).ready(function () {
    initializateUI();
    initializateBomGrid();
    initializateTechnologyGrid();
});


function initializateUI() {
    UICtrl.layout("#layout", {leftWidth: 3});
    appendTaskinstRelations(null);
}

function initializateBomGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({
        addHandler: function(){
            UICtrl.addGridRow(bomGridManager);
        },
    });
    bomGridManager = UICtrl.grid("#bomGrid", {
        columns: [
            {
                display: "产品编码", name: "productCode", width: 140, minWidth: 60, type: "string", align: "left",
                editor: {
                    data: {
                        type: 'system',
                        name: "productChoose",
                        back: {
                            productId: "productId",
                            productCode: "productCode",
                            productName: "productName",
                            productUnit: "productUnit",
                        }
                    },
                    required: true,
                    type: "select"
                }
            },
            {display: "产品名称", name: "productName", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "单位", name: "productUnit", width: 120, minWidth: 60, type: "string", align: "left"},
            {
                display: "币别", name: "currency", width: 140, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true,
                    type: 'text'
                }
            },
            {
                display: "单价", name: "price", width: 120, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true,
                    type: 'text',
                    mask: '9999999.99'
                }
            },
            {
                display: "备注", name: "remark", width: 140, minWidth: 60, type: "string", align: "left",
                editor: {
                    required: true,
                    type: 'text'
                }
            },
        ],
        dataAction: "server",
        url: web_app.name + '/quotationItem/queryQuotationItemList.ajax',
        parms: {quotationId: getId()},
        pageSize: 20,
        usePager: true,
        toolbar: toolbarOptions,
        enabledEdit: true,
        width: "100%",
        height: "400",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            updateHandler(data.proofingMakeId);
        },
        onAfterShowData: function (currentData) {
            setTimeout(function () {
                //加载工艺
                reloadTechnologyGrid();
        },200)
        },
        onAfterEdit:function (item) {
            //加载工艺
            if (item.column.name == "productCode") {
                reloadTechnologyGrid();
            }
        }
    });
    UICtrl.setSearchAreaToggle(bomGridManager);
}

function initializateTechnologyGrid() {
    var toolbarOptions = UICtrl.getDefaultToolbarOptions({});
    technologyGridManager = UICtrl.grid("#technologyGrid", {
        columns: [
            {display: "工艺", name: "technologyName", width: 140, minWidth: 60, type: "string", align: "left"},
            {display: "工序", name: "processName", width: 120, minWidth: 60, type: "string", align: "left"},
            {display: "资源", name: "resourceName", width: 120, minWidth: 60, type: "string", align: "left"},
        ],
        dataAction: "server",
        url: web_app.name + '/technology/queryTechnologyByProduct.ajax',
        //parms: {productId: getId()},
        pageSize: 20,
        usePager: true,
        toolbar: toolbarOptions,
        width: "100%",
        height: "400",
        heightDiff: -8,
        checkbox: true,
        fixedCellHeight: true,
        selectRowButtonOnly: true,
        onDblClickRow: function (data, rowindex, rowobj) {
            updateHandler(data.proofingMakeId);
        }
    });
    UICtrl.setSearchAreaToggle(technologyGridManager);
}

function reloadGrid() {
    bomGridManager.loadData();
}

function reloadTechnologyGrid(){
    var datas = bomGridManager.rows;
    var productIdList=[];
    if (datas.length >0){
        datas.forEach(data=>{
            var productId = data.productId;
            if (productId != null && productId !== ""){
                productIdList.push(productId);
            }
        })
        if (productIdList.length > 0){
            var param = {ids:$.toJSON(productIdList)};
            $('#technologyGrid').ligerGetGridManager().options.url =web_app.name + '/technology/queryTechnologyByProduct.ajax';
            _grid = UICtrl.getGridManager('#technologyGrid');
            UICtrl.gridSearch(_grid, param);
        }
    }
}

//获取数据保存的参数
function getExtendedData(processAction) {
    //回退、打回、转交时，不需要验证必填项是否已经填写；
    if (processAction == ProcessAction.BACK
        || processAction == ProcessAction.REPLENISH
        || processAction == ProcessAction.TRANSMIT) {
        //不验证
        $('#submitForm').attr('check', false);
    }
    const isAllData = $('#oldId') || !getId();
    var extendedData = {};
    var _grid = UICtrl.getGridManager('#bomGrid');
    if (_grid) {
        var datas = DataUtil.getGridData({
            gridManager: _grid,
            isAllData: isAllData
        });
        if (!datas) {
            return false;
        }
        extendedData.quotationItems = Public.encodeJSONURI(datas);
    }
    return extendedData;
}

function getId() {
    return $("#quotationId").val() || "";
}

function setId(value) {
    $("#quotationId").val(value);
    var _grid=UICtrl.getGridManager('#bomGrid');
    _grid.options.parms.quotationId = value;
}





