/*
 * 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: {},
    dictCodes: ['payment_method', 'controlling_accounts', 'currency','supplier_accetp_deadline'],
    loadNecessaryData: function () {
        initNecessaryData("pc/sys/tax/rest/listAll")
        initNecessaryData("pc/sys/purchaseGroup/rest/listAll")
        initNecessaryData("pc/sys/company/rest/listAll", 'companyCode', 'companyName')
        initNecessaryData("pc/sys/purchaseOrg/rest/listAll", 'purchaseOrgCode', 'purchaseOrgDesc')
        initDict(page.dictCodes.join(','))
    },
    initTable: function () {
        var columns = [[{
            field: 'ck', checkbox: true
        }, {
            field: 'isFreeze',
            title: '冻结标记',
            width: '70',
            align: 'center',
            formatter: function (value) {
                switch (value) {
                    case 1:
                        return '<span style="color: red">冻结</span>';
                    case 0:
                        return '<span style="color: darkgreen">正常</span>';
                    default:
                        return '';
                }
            }
        }, {
            field: 'firstFlag',
            title: '优先标记',
            width: '70',
            align: 'center',
            formatter: function (value, row, index) {
                if (value) {
                    return "<span style='color: darkgreen'>是</span>"
                }
                return "否"
            }
        }, {
            field: 'state',
            title: '状态',
            width: '65',
            align: 'center',
            formatter: function (value) {
                switch (value) {
                    case 1:
                        return '注册';
                    case 2:
                        return '潜在';
                    case 3:
                        return '正式';
                    case 4:
                        return '临时';
                    case 5:
                        return '退出';
                    case 6:
                        return '合格';
                    case 7:
                        return '小批量';
                    default:
                        return '';
                }
            }
        }, {
            field: 'companyCode', title: '公司', width: '19%', align: 'center', formatter: function (value) {
                return  "(" + value + ")" + getNecessaryDataName("pc/sys/company/rest/listAll", value)
                // return getNecessaryDataName("pc/sys/company/rest/listAll", value)
            }
        },
        //     {
        //     field: 'companyCode', title: '公司编码', width: '19%', align: 'center'
        // },
            {
            field: 'supplierName', title: '供应商名称', width: '15%', align: 'center'
        }, {
            field: 'supplierCode', title: '供应商编号', width: '15%', align: 'center'
        }, {
            field: 'purchaseOrgCode', title: ' 采购组织', width: '170', align: 'center', formatter: function (value) {
                return getNecessaryDataName('pc/sys/purchaseOrg/rest/listAll', value)
            }
        }, {
                field: 'purchaseGroupCode', title: ' 采购组', width: '170', align: 'center', formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll', value)
            }
        }, {
            field: 'purchaseCategoryCode', title: '品类编码', width: '15%', align: 'center'
        }, {
            field: 'purchaseCategoryName', title: '品类名称', width: '15%', align: 'center'
        }, {
            field: 'paymentTerms', title: '付款条件', width: '12%', align: 'center', formatter: function (value) {
                return getDicName("payment_method", value)
            }
        }, {
                field: 'acceptanceMethod', title: '承兑期限', width: '125',
                formatter: function (value) {
                    return getDicName('supplier_accetp_deadline', value)
                }
            },{
            field: 'currencyCode', title: '币种', width: '5%', align: 'left', formatter: function (value, row) {
                return getDicName("currency", value)
            }
        }, {
            field: 'taxCode', title: '税率', width: '10%', align: 'center', formatter: function (value) {
                return getNecessaryDataName('pc/sys/tax/rest/listAll', value)
            }
        }, {
            field: 'controlSubject', title: ' 统御科目', width: '10%', align: 'center', formatter: function (value) {
                return getDicName("controlling_accounts", value)
            }
        }, {
            field: 'smallTime', title: '转小批量时间', width: 160, align: 'center', formatter: function (value, row) {
                if (value) {
                    return new Date(value).format("yyyy-MM-dd hh:mm")
                }
                return value
            }
        }, {
            field: 'businessRegisterNo', title: '营业执照注册号', width: '15%', align: 'center'
        },{
            field: 'qualifiedTime', title: '转合格时间', width: 160, align: 'center', formatter: function (value, row) {
                if (value) {
                    return new Date(value).format("yyyy-MM-dd hh:mm")
                }
                return value
            }
        }]]
        var singleSelect = false
        if (single === 1) {
            singleSelect = true
        }
        let query = {}
        if (supplierId === '') {
            query['supplier'] = "-9999999"
        }
        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: 'GET',
            idField: 'id',
            height: $(window).height() - $('#searchForm').outerHeight() - 20,
            url: g.ctx + 'pc/supplier/purchaseCategory/rest/listSupplier?state=' + state + "&supplierId=" + supplierId,
            singleSelect: singleSelect,
            pagination: true,
            loadMsg: commonMsg.loadData,
            rownumbers: true,
            fitColumns: true,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: query,
            columns: columns
        })
    },
    getChecked:

        function () {
            return page.dataGrid.datagrid('getChecked')
        }
}

$(function () {
    page.loadNecessaryData()
    page.initTable()
})

/**
 * 选择指定供应商
 * @param callback  选择指定供应商数据的回调
 */
function chooseRow(callback) {
    let row = page.dataGrid.datagrid('getChecked')
    callback(row)
}

function selfSearch() {
    let params = searchParams();
    if (params.supplier !== '' || params.category !== '') {
        searchInfo()
    }
}

function selfReset() {
    $('#searchForm').form("clear");
    if (supplierId === '') {
        $('#tb').datagrid('load', {supplier: '-999999'});
    } else {
        resetSearch()
    }


}