/*
 * 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.
 */

let el = {
    searchForm: '#searchForm',
    bodyHeight: document.documentElement.clientHeight
}
/**
 *
 * @author BOGON
 * @version 1.0
 */
var page = {
    dataGrid: {},
    initTable: function () {
        var columns = [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'purchasingBusinessType',
                title: "采购类型",
                width: 80,
                align: 'left',
                exportEnum:getDictList('purchasing_business_type'),
                formatter: function (value) {
                    return getDicName('purchasing_business_type',value);
                }

            },
            {
                field: 'applyProjectType',
                title: '申请样品制作项目类型',
                width: 150,
                formatter: function (value) {
                    var vv =  getDicName('apply_project_type',value);
                    if (vv === undefined || vv === "") {
                        return value
                    } else {
                        return vv;
                    }
                }
            },
            {
                field: 'billCode',
                title: "资产申请单号",
                width: 150,
                align: 'left'

            },
            {
                field: 'itemCode',
                title: "行项目",
                width: 50,
                align: 'left'

            },
            {
                field: 'subjectTypeCode',
                title: "采购类别",
                width: 100,
                align: 'left',
                formatter: function (value) {
                    if (value === "01") {
                        return "（01）办公用品";
                    } else if (value === "02") {
                        return "（02）资产";
                    } else {
                        return "低耗类资产";
                    }
                }

            },
            {
                field: 'companyCode',
                title: "公司编码",
                width: 300,
                align: 'left',
                exportEnum:getNecessaryDataList('pc/sys/company/rest/listAll'),
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/company/rest/listAll', value);
                }
            },
            {
                field: 'materialName',
                title: "物品名称/开模件名称",
                width: 180,
                align: 'left'

            },

            {
                field: 'applyProjectType',
                title: "申请样品制作项目类型",
                width: 130,
                align: 'left'
            },
            {
                field: 'modelApply',
                title: "模具申请",
                width: 150,
                align: 'left'
            },
            {
                field: 'projectProcess',
                title: "项目所处阶段",
                width: 80,
                align: 'left',
                hidden: true
            },
            {
                field: 'modelType',
                title: "模具类型",
                width: 100,
                align: 'left',
                formatter: function (value) {
                    if (value === "01") {
                        return "（01）冲压";
                    } else if (value === "02") {
                        return "（02）压铸";
                    } else if (value === "03") {
                        return "（03）注塑";
                    } else if (value === "04") {
                        return "（04）组件模具";
                    } else if (value === "05") {
                        return "（05）型材模具";
                    } else if (value === "06") {
                        return "（06）硅胶模具";
                    } else if (value === "07") {
                        return "（07）刀模";
                    } else {
                        return "";
                    }
                }
            },
            {
                field: 'modelNumber',
                title: "模具编号",
                width: 100,
                align: 'left'

            },
            {
                field: 'materialSpec',
                title: "型号规格/参数",
                width: 300,
                align: 'left'

            },
            {
                field: 'brand',
                title: "品牌",
                width: 120,
                align: 'left'

            },
            {
                field: 'requireNum',
                title: "数量",
                width: 60,
                align: 'left'

            },
            {
                field: 'enablePurchaseNum',
                title: "资产请购可用数量",
                width: 100,
                align: 'left'

            },
            {
                field: 'unitCode',
                title: "单位",
                width: 60,
                align: 'left'

            },
            {
                field: 'price',
                title: "预估单价",
                width: 60,
                align: 'right',
                formatter: function (value) {
                    return formatNumber(value, 2, 1);
                }

            },
            {
                field: 'totalAmount',
                title: "申请金额",
                width: 80,
                align: 'right',
                formatter: function (value) {
                    return formatNumber(value, 2, 1);
                }

            },            {
                field: 'applyUserName',
                title: "申请人名称",
                width: 80,
                align: 'left'

            },            {
                field: 'applyDeptName',
                title: "申请部门",
                width: 220,
                align: 'left'

            },            {
                field: 'applyTime',
                title: "申请时间",
                width: 80,
                align: 'left',
                exportDateFormatter:'dateTime',
                formatter: function (value) {
                    if (!value) {
                        return '';
                    }
                    return moment(value).format('YYYY-MM-DD');
                }
            },
            {
                field: 'userName',
                title: "使用人姓名",
                width: 80,
                align: 'left'

            },
            {
                field: 'userCode',
                title: "使用人工号",
                width: 80,
                align: 'left',
                hidden: true
            },
            {
                field: 'useDeptName',
                title: "使用部门名称",
                width: 220,
                align: 'left'

            },
            {
                field: 'useDeptCode',
                title: "使用部门编码",
                width: 80,
                align: 'left',
                hidden: true
            },
            {
                field: 'costCenterCode',
                title: "成本中心",
                width: 80,
                align: 'left',
                hidden: true

            },
            {
                field: 'costCenterName',
                title: "成本中心名称",
                width: 320,
                align: 'left'

            },
            {
                field: 'customerCode',
                title: "客户代码",
                width: 80,
                align: 'left',
                hidden: true
            },
            {
                field: 'customerName',
                title: "客户名称",
                width: 80,
                align: 'left',
                hidden: true
            },
            {
                field: 'stockPoint',
                title: "存放地点",
                width: 80,
                align: 'left',
                hidden: true

            },
            {field: 'stockPointName', title: '存放地点',width:'200',
                exportEnum:getDictList('stock_point'),
                formatter: function (value, row, index) {
                    return getDicName('stock_point', row.stockPoint);
                }
            },
            {
                field: 'mainUses',
                title: "用途",
                width: 80,
                align: 'left',
                hidden: true

            },
            {
                field: 'purchaseGroupCode',
                title: "采购组",
                width: 80,
                align: 'left',
                exportEnum:getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
                }

            }
        ]];
        const searchHeight = $(el.searchForm).outerHeight() + 25;
        const pager = initGridPagerNew(el.bodyHeight - searchHeight - 20, 37, 40);
        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: 'GET',
            idField: 'id',
            height: el.bodyHeight - searchHeight,
            url: g.ctx + 'pc/inquiry/purchase/apply/bill/detail/rest/selectPurchaseForContractList',
            singleSelect: true,
            pagination: true,
            loadMsg: commonMsg.loadData,
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            pageSize: pager.pageSize,
            pageList: pager.pageList,
            queryParams: {
                purchasingBusinessType: purchasingBusinessType,
                contractType: contractType,
                companyCode: companyCode,
                supplierCode: supplierCode
            },
            columns: columns
        });
    },
};

$(function () {
    initDict('purchasing_business_type,stock_point,apply_project_type');
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll,pc/sys/company/rest/listAll');
    page.initTable();
});

/**
 * 选择指定申请单
 * @param callback  选择指定申请单数据的回调
 */
function getChecked(callback) {
    let rows = $('#tb').datagrid('getChecked');
    if (rows.length !== 1) {
        topWin.layer.msg("请选中一条数据")
        return
    }
    callback(rows[0])
}
function confirmChose(callback) {
    let rows = $('#tb').datagrid('getChecked');
    if (rows.length !== 1) {
        topWin.layer.msg("请选中一条数据")
        return
    }
    callback(rows);
}