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

var page = {
    initTemplateItemData: function () {
        let columns = [[{field: 'ck', checkbox: true}, {
            field: 'code', title: '资质编码', width: '100', align: 'left'
        }, {
            field: 'name', title: '资质名称', width: '180', align: 'left'
        }, {
            field: 'fileId', title: '样例文件', width: '180', align: 'left', formatter: function (value, row) {
                if (value) {
                    var fileHtml = '<a href="' + g.ctx + 'pc/sys/file/rest/download/' + value + '">' + row.fileName + '</a>';
                    return fileHtml;
                }
            }
        }, {
            field: 'requery', title: '是否必填', width: '100', align: 'left', formatter: function (value) {
                if ("1" == value) {
                    return "是"
                }
                if ("0" == value) {
                    return "否"
                }
            }, editor: {
                type: 'combobox', options: {
                    editable: false,
                    required: true,
                    data: [{k: "1", v: "是"}, {k: "0", v: "否"}], valueField: 'k', textField: 'v', panelHeight: 'auto'
                }
            }
        }, {
            field: 'validity', title: '是否有效期校验', width: '100', align: 'left', formatter: function (value) {
                if ("1" == value) {
                    return "是"
                }
                if ("0" == value) {
                    return "否"
                }
            }, editor: {
                type: 'combobox', options: {
                    editable: false,
                    required: true,
                    data: [{k: "1", v: "是"}, {k: "0", v: "否"}], valueField: 'k', textField: 'v', panelHeight: 'auto'
                }
            }
        }, {
            field: 'effectiveYear', title: '资质时效(年)', align: 'left', width: "100", editor: {
                type: 'numberbox', options: {
                    min: 0, missingMessage: '请输入0~99的数字'
                }
            }, formatter: function (value) {
                if (value) {
                    return parseFloat(Number(value));
                } else {
                    return value;
                }
            }
        }, {
            field: 'sort', title: '排序', width: '100', align: 'left',
            editor: {type: 'numberbox', options: {required: true}}
        }]]
        let toolbar = [{
            text: '选择资质', id: 'btn_add', iconCls: 'icon-add', handler: function () {
                //
                const url = g.ctx + 'pc/supplier/qualification/index?selected=1'
                const qualificationItemDialog = openDialog('qualificationItemDialog', url, {
                    title: '资质选择',

                    width: '90%',
                    height: '90%',
                    cache: false,
                    modal: true,
                    border: false,
                    draggable: true,
                    shadow: false,
                    onClose: function () {
                        qualificationItemDialog.dialog('destroy');
                    },
                    buttons: [{
                        text: itablei8n.confirm, handler: function () {
                            topWin.frames['qualificationItemDialog'].getChecked(function (rows) {
                                $.each(rows, function (idx, itm) {
                                    let tmpRow = {
                                        code: itm.code,
                                        name: itm.name,
                                        fileId: itm.fileId,
                                        sort: 1,
                                        fileName: itm.fileName
                                    }
                                    $('#tb-item').datagrid("appendRow", tmpRow)
                                })
                                qualificationItemDialog.dialog('close');
                            });
                        }
                    }, {
                        text: itablei8n.close, handler: function () {
                            qualificationItemDialog.dialog('close');
                        }
                    }]
                });
            }
        }, {
            text: '删除', id: 'btn_del', iconCls: 'icon-remove', handler: function () {
                var rows = $('#tb-item').datagrid('getChecked');
                if (rows.length <= 0) {
                    topWin.layer.msg("请选中至少一条记录");
                    return;
                }
                topWin.layer.confirm('是否删除？',{icon: 3}, function (index) {
                    let rowIndex = [];
                    $.each(rows, function (idx, itm) {
                        rowIndex.push($('#tb-item').datagrid("getRowIndex", itm))
                    })
                    rowIndex.reverse()
                    $.each(rowIndex, function (idx, itm) {
                        $('#tb-item').datagrid("deleteRow", itm);
                    })
                    topWin.layer.close(index);
                })
            }
        }]
        let url = g.ctx + 'pc/supplier/qualification/rest/template/item/list?templateId=' + $("#id").val()
        $('#tb-item').edatagrid({
            method: "GET",
            singleSelect: false,
            pagination: false,
            nowrap: true,
            autoSave: true,
            idField: 'id',
            loadMsg: "数据加载中......",
            rownumbers: true,
            striped: true,
            url: url,
            columns: columns,
            height: $(window).height() - $('#form').outerHeight() - 90,
            width: '100%',
            toolbar: toolbar
        });
    },
    saveOrUpdate: function (call) {
        $('#tb-item').edatagrid("saveRow")
        var formData = $("#form").serializeJson();
        var data = {}
        $.each(formData, function (idx, itm) {
            data[idx] = itm
        })
        data['item'] = $("#tb-item").datagrid("getRows");
        $.ajax({
            url: g.ctx + 'pc/supplier/qualification/rest/template/saveOrUpdate',
            type: "POST",
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function (data) {
                topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    call();
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
$(function () {
    page.initTemplateItemData()
})