/**
 *
 * @author BOGON
 * @version 1.0
 */

var page = {
    dataGrid: {},
    submitFlag: 0,
    validKey: ['supplierLevel', 'supplierWeakness', 'supplierStrength', 'qpaScore', 'qsaScore', 'purchasePriceScore', 'purchaseScore', 'nonManufacturerPurchaseScore', 'rdScore'],
    initTable: function () {
        var columns = [
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'supplierLevel',
                title: "供应商等级",
                width: 130,
                align: 'left',
                editor: {
                    type: 'combobox', options: {
                        valueField: 'code',
                        textField: 'code',
                        panelHeight: 'auto',
                        required: true,
                        editable: false,
                        data: [{code: "A"}, {code: "B"}, {code: "C"}, {code: "D"}]
                    }
                }
            },
            {
                field: 'supplierWeakness',
                title: "供应商劣势",
                width: 130,
                align: 'left',
                editor: {
                    type: 'textbox', options: {
                        required: true
                    }
                }
            },
            {
                field: 'supplierStrength',
                title: "供应商优势",
                width: 130,
                align: 'left',
                editor: {
                    type: 'textbox', options: {
                        required: true
                    }
                }
            }
        ];
        if (supplierGenre === "0") {
            columns.push({
                    field: 'qpaScore',
                    title: "QPA评分",
                    width: 130,
                    align: 'left',
                    editor: {
                        type: 'numberbox', options: {
                            required: true, max: 100,
                            // onChange: page.calTotalScore,
                            min: 0, missingMessage: '请输入0~100的数字',
                            precision:2,
                            decimalSeparator:'.'
                        }
                    }
                },
                {
                    field: 'qsaScore',
                    title: "QSA评分",
                    width: 130,
                    align: 'left',
                    editor: {
                        type: 'numberbox', options: {
                            required: true, max: 100,
                            // onChange: page.calTotalScore,
                            min: 0, missingMessage: '请输入0~100的数字'
                            ,
                            precision:2,
                            decimalSeparator:'.'
                        }
                    }
                },
                {
                    field: 'purchasePriceScore',
                    title: "采购评分(价格)",
                    width: 130,
                    align: 'left',
                    editor: {
                        type: 'numberbox', options: {
                            required: true, max: 100,
                            // onChange: page.calTotalScore,
                            min: 0, missingMessage: '请输入0~100的数字'
                            ,
                            precision:2,
                            decimalSeparator:'.'
                        }
                    }
                },
                {
                    field: 'purchaseScore',
                    title: "采购评分(交期)",
                    width: 130,
                    align: 'left',
                    editor: {
                        type: 'numberbox', options: {
                            required: true, max: 100,
                            // onChange: page.calTotalScore,
                            min: 0, missingMessage: '请输入0~100的数字'
                            ,
                            precision:2,
                            decimalSeparator:'.'
                        }
                    }
                },
                {
                    field: 'rdScore',
                    title: "CE/研发评分(技术)",
                    width: 130,
                    align: 'left',
                    editor: {
                        type: 'numberbox', options: {
                            required: true,
                            max: 100,
                            // onChange: page.calTotalScore,
                            min: 0, missingMessage: '请输入0~100的数字',
                            precision:2,
                            decimalSeparator:'.'
                        }
                    }
                })
        } else {
            columns.push({
                field: 'nonManufacturerPurchaseScore',
                title: "采购评分",
                width: 130,
                align: 'left',
                editor: {
                    type: 'numberbox', options: {
                        required: true, max: 100,
                        // onChange: page.calTotalScore,
                        min: 0, missingMessage: '请输入0~100的数字'
                        ,
                        precision:2,
                        decimalSeparator:'.'
                    }
                }
            })
        }
        columns.push({
                field: 'totalScore',
                title: "总评分",
                width: 130,
                align: 'left',
                formatter: function (value) {
                    if (value === '' || value === undefined) {
                        return '保存后系统计算'
                    }
                    return value
                }
            },
            {
                field: 'createName',
                title: "评分人",
                width: 130,
                align: 'left'
            })
        page.dataGrid = $('#tb').edatagrid({
            autoSave: true,
            iconCls: 'icon-list',
            width: '100%',
            method: 'GET',
            height: $(window).height() - 20,
            url: g.ctx + 'pc/supplier/sqescore/rest/list?supplierId=' + supplierId,
            singleSelect: false,
            pagination: false,
            loadMsg: commonMsg.loadData,
            rownumbers: true,
            remoteSort: true,
            striped: true,
            columns: [columns],
            toolbar: [{
                text: '添加',
                id: 'btn-add',
                iconCls: 'icon-add',
                handler: function () {
                    page.dataGrid.datagrid("appendRow", {supplierId: supplierId})
                }
            }, {
                text: '删除',
                id: 'btn-add',
                iconCls: 'icon-add',
                handler: function () {
                    $('#tb').edatagrid('saveRow')
                    let rows = page.dataGrid.datagrid("getChecked")
                    if (rows.length <= 0) {
                        topWin.layer.msg("请至少选中一条数据!")
                    }
                    let rowIdx = []
                    $.each(rows, function (idx, itm) {
                        rowIdx.push(page.dataGrid.datagrid("getRowIndex", itm))
                    })
                    rowIdx.reverse();
                    $.each(rowIdx, function (idx, itm) {
                        rowIdx.push(page.dataGrid.datagrid("deleteRow", itm))
                    })
                }
            }],
            onEdit: function (index, row) {
                page.editIndex = index
            }
        });
    },
    calTotalScore: function () {
        let key = ['qpaScore', 'qsaScore', 'purchasePriceScore', 'purchaseScore', 'nonManufacturerPurchaseScore', 'rdScore']
        let total = 0;
        if (page.editIndex == undefined) {
            return
        }
        $.each(key, function (idx, itm) {
            let tmp = $('#tb').edatagrid('getEditor', {
                index: page.editIndex,
                field: itm
            }).target;
            let sc = tmp.numberbox("getValue")
            total += parseFloat(sc) || 0;
            total.toFixed(4)
        })
        let totalScore = $('#tb').edatagrid('getEditor', {
            index: page.editIndex,
            field: 'totalScore'
        }).target;
        totalScore.numberbox("setValue", total)
    },
    submit: function (call) {
        if (page.submitFlag === 1) {
            return
        }
        var isValid = $('#tb').datagrid('validateRow', page.editIndex);
        if (!isValid) {
            topWin.layer.msg("信息填写不完整")
            return
        }
        $('#tb').edatagrid('saveRow')
        let rows = $('#tb').datagrid("getRows");
        let tipMsg = []
        $.each(rows, function (idx, itm) {
            $.each(page.validKey, function (iidx, iitm) {
                if (itm[iitm] === undefined || itm[iitm] === '') {
                    tipMsg.push(1)
                }
            })
        })
        if (tipMsg.length > 0) {
            topWin.layer.msg("信息填写不完整")
            return
        }
        $.each(rows, function (idx, itm) {
            itm['supplierId'] = supplierId
        })
        page.submitFlag = 1
        let manufacturerFlag = supplierGenre === "0" ? 1 : 0
        $.ajax({
            url: g.ctx + "pc/supplier/sqescore/rest/saveOrUpdate?supplierId=" + supplierId + "&manufacturerFlag=" + manufacturerFlag,
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(rows),
            complete: function () {
                page.submitFlag = 0
            },
            success: function () {
                topWin.layer.alert("操作成功", {
                    icon: 1,
                    closeBtn: 0,
                    yes: function (iindex) {
                        topWin.layer.close(iindex);
                        $('#tb').edatagrid("reload")
                    }
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
$(function () {
    page.initTable()
    if (supplierGenre === "0") {
        page.validKey = ['supplierLevel', 'supplierWeakness', 'supplierStrength', 'qpaScore', 'qsaScore', 'purchasePriceScore', 'purchaseScore', 'rdScore']
    } else {
        page.validKey = ['supplierLevel', 'supplierWeakness', 'supplierStrength', 'nonManufacturerPurchaseScore']
    }
})