/*
 * The Hefei Ceshi RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2019
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of Ceshi Company and its suppliers,
 *    if any.
 */

/**
 *
 * @author BOGON
 * @version 1.0
 */
var page = {
    dataGrid: {},
    initTable: function () {
        var columns = [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'supplierCode',
                title: commonTxt.code,
                width: 130,
                align: 'left'

            },
            {
                field: 'supplierName',
                title: commonTxt.name,
                width: 260,
                align: 'left'

            },
            {
                field: 'shortName',
                title: "简称",
                width: 130,
                align: 'left'
            }
        ]];
        var singleSelect = false;
        if (single == 1) {
            singleSelect = true;
        }
        let query = {
            companyCode: companyCode,
            purchaseOrgCode: purchaseOrgCode,
            state: state,
            code: "-9999",
        }
        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: 'GET',
            idField: 'id',
            height: $(window).height() - $('#searchForm').outerHeight() - 20,
            url: g.ctx + 'pc/supplier/rest/listForSelect?categories=' + categories ,
            singleSelect: singleSelect,
            pagination: true,
            loadMsg: commonMsg.loadData,
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: query,
            columns: columns
        });
    },
    tableReload: function () {
        var params = $("#searchForm").serializeArray();
        var query = {};
        $.each(params, function (index, item) {
            query[item.name] = item.value;
        });
        page.dataGrid.datagrid('load', {
            page: 1,
            rows: 20,
            q: JSON.stringify(query)
        })
    },
    bindEvent: function () {
        $("#search_btn").bind('click', page.tableReload);
        $("#reset_btn").bind('click', function () {
            $("#searchForm").form('reset');
        });
    },
    getChecked: function () {
        return page.dataGrid.datagrid('getChecked');
    }
};

$(function () {
    page.bindEvent();
    page.initTable();
});

/**
 * 选择指定供应商
 * @param callback  选择指定供应商数据的回调
 */
function chooseRow(callback) {
    let rows = $('#tb').datagrid('getSelections');
    if (single) {
        callback(rows[0]);
    } else {
        callback(rows);
    }

}

function selfSearch() {
    let params = searchParams();
    if (categories !== '') {
        searchInfo()
    } else if (params.code !== '' || params.name !== '' || params.category !== '') {
        searchInfo()
    }
}

function selfReset() {
    $('#searchForm').form("clear");
    $('#tb').datagrid('load', {code: '-999999'});
}