var windowId, proName = window.location.pathname.split("/")[1],
    sysUrl = "http://" + window.location.host + "/" + proName, toEdit;

$(function () {
    getmonth("pjyf");
    initGrid();
});
function  getmonth(id) {
    var db1 = document.getElementById(id);
    var db = $(db1);
    db.datebox({
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            //fix 1.3.x不选择日期点击其他地方隐藏在弹出日期框显示日期面板
            if (p.find('div.calendar-menu').is(':hidden')) p.find('div.calendar-menu').show();
            if (!tds) setTimeout(function () {//延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                    var year = /\d{4}/.exec(span.html())[0];//得到年份
                    var month = parseInt($(this).attr('abbr'), 10); //月份，这里不需要+1
                    month = month < 10 ? '0' + month : month;
                    db.datebox('hidePanel')//隐藏日期对象
                        .datebox('setValue', year + '-' + month); //设置日期的值
                });
            }, 0);
            yearIpt.unbind();//解绑年份输入框中任何事件
        },
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },

        formatter: function (d) {
            return d.getFullYear() + '-' + (d.getMonth() + 1);
        }
    });
    var p = db.datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        aToday = p.find('a.datebox-current'),
        yearIpt = p.find('input.calendar-menu-year'),//年份输入框
//显示月份层的触发控件
        span = aToday.length ? p.find('div.calendar-title span') ://1.3.x版本
            p.find('span.calendar-text'); //1.4.x版本
    if (aToday.length) {//1.3.x版本，取消Today按钮的click事件，重新绑定新事件设置日期框为今天，防止弹出日期选择面板
        aToday.unbind('click').click(function () {
            var now = new Date();
            now = now < 10 ? '0' + now : now;
            db.datebox('hidePanel').datebox('setValue', now.getFullYear() + '-' + (now.toString() + 1));
        });
    }
}
//加载列表
function initGrid() {
    var toolbar = [{
        text: '查询',
        iconCls: 'icon-search',
        handler: doSearch
    }, {
        text: '增加',
        iconCls: 'icon-add',
        handler: doAdd
    }, {
        text: '修改',
        iconCls: 'icon-cut',
        handler: doEdit
    }, {
        text: '删除',
        iconCls: 'icon-delete',
        handler: doDelete
    }];

    $('#tableGrid').datagrid({
            toolbar: toolbar,
            rownumbers: true,
            singleSelect: true,
            queryParams: getQueryParams(),
            url: 'ywglbmkhAction!listYwglbmkhByPage.do',
            columns: [[
                {
                    field: 'A',
                    checkbox: 'true',
                    width: 30
                },
                {field: 'rybh', title: '人员编号', hidden: false, width: 120},
                {field: 'rymc', title: '人员名称', hidden: false, width: 120},
                {field: 'pjyf', title: '评价月份', hidden: false, width: 75},
                {field: 'cjrq', title: '创建时间', hidden: false, width: 50},
                {field: 'cjry', title: '创建人', hidden: false, width: 75},
                {field: 'xgrq', title: '修改时间', hidden: false, width: 70},
                {field: 'xgr', title: '修改人', hidden: false, width: 70},
                {field: 'score', title: '得分', hidden: false, width: 70},
                {field: 'bz', title: '备注', hidden: false, width: 80}
            ]]
        }
    );
}

//查询条件
function getQueryParams() {
    var sParam = {};
    sParam.LIKEryzh = $('#LIKEryzh').textbox("getValue");
    sParam.LIKErymc = $('#LIKErymc').textbox("getValue");
    sParam.pjyf = $('#pjyf').datebox("getValue");
    return sParam;
}


//增加
function doAdd() {
    toEdit = false;
    windowId = top.$.createWin({
        title: '增加信息',
        width: 380,
        height: 300,
        buttons: [{
            text: $.fn.textbox.defaults.ok,
            handler: saveNodeInfo
        }],
        url: sysUrl + "/yw/operationcount/videoarchivecount/orderpartmentcount/orderpartmentcount_show.jsp"
    });
}

//修改
function doUpdate(index) {
    toEdit = true;
    var selRows = $('#tableGrid').datagrid('getRows')[index];
    windowId = top.$.createWin({
        title: '修改信息',
        width: 380,
        height: 300,
        data: selRows,
        buttons: [{
            text: $.fn.textbox.defaults.ok,
            handler: saveNodeInfo
        }],
        url: sysUrl + "/yw/operationcount/videoarchivecount/orderpartmentcount/orderpartmentcount_show.jsp"
    });
}

//查询
function doSearch() {
    $('#tableGrid').datagrid('load', getQueryParams());
}

function doDelete() {
    var selRows = $('#tableGrid').datagrid('getSelected');
    if (!selRows) {
        $.showMsg('请先选择记录! ');
        return;
    }
    top.$.dlg.confirm('确认删除?', function () {
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'ywglbmkhAction!delete_Ywglbmkh.do',
            data: {
                xh: selRows.xh
            },
            success: function (flag) {
                if (flag == "success") {
                    $.showMsg("操作成功！");
                    doSearch();
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    });
}

//保存
function saveNodeInfo() {
    var win = $.getTopWindowById(windowId);
    var jq = win.$;
    var model = $.getFormValues("childform", windowId);
    var insertmodel = {};
//	console.log(model);
    if (!win.$('#childform').form('enableValidation').form('validate')) {
        return;
    }
    insertmodel = model;
    insertmodel.rybh = model.rymc;
    insertmodel.rymc = model.rymc_text;
    insertmodel.XGTJxh = jq("#xh").val();

    if (toEdit) {//修改
        model.XGTJxh = jq('#xh').val();
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'ywglbmkhAction!update_Ywglbmkh.do',
            data: model,
            success: function (value) {
                if (value == "success") {
                    $.showMsg("操作成功！");
                    top.$("#" + windowId).dialog('close');
                    doSearch();
                } else if (value == "same") {
                    $.showMsg("已存在！");
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    } else {
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'ywglbmkhAction!insert_Ywglbmkh.do',
            data: model,
            success: function (value) {
                if (value == "success") {
                    $.showMsg("操作成功！");
                    top.$("#" + windowId).dialog('close');
                    doSearch();
                } else if (value == "same") {
                    $.showMsg("已存在！");
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    }
}

//打开修改
function doEdit() {
    var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
    if (selIndex == -1) {
        $.showMsg('请先选择一行! ');
        return;
    } else {
        doUpdate(selIndex);
    }
}