/**
 * 考核列表
 */
layui.use(['form', 'table', 'laydate'], function () {
    var form = layui.form;
    var table = layui.table;
    var laydate = layui.laydate;

    //监听考核类型下拉选择
  /*  form.on('select', function (data) {
        var elem = data.elem;
        if (elem.id == 'type') {
            typeChange(data.value);
        }
        form.render('select');


    });*/

    // 考核年份选择器渲染
    laydate.render({
        elem: '#year' //指定元素
        , type: 'year' //年选择器（year，month，date，time，datetime）
        , format: 'yyyy' //自定义格式
        , min: '1900-1-1' //最小值
        , max: '9999-12-31' //最大值
    });

    table.render({
        elem: '#halfList'
        , url: '/jxp/getJxEntryQueryList' //异步接口
        , even: true // 开启隔行背景
        , size: 'sm' // 小尺寸
        , cols: [[{type: 'numbers'},{type: 'checkbox', align: 'center'}
            , {field: 'entry_cyclename', align: 'center', title: "考核周期"}
            , {field: 'entry_khpersonname', align: 'center', title: "考核对象"}
            , {field: 'entry_displayname', align: 'center', title: "被考核人部门"}
            , {field: 'entry_orgname', align: 'center', title: "考核组织"}

            , {field: 'entry_submittime', align: 'center', title: "考核开始时间"}
            , {field: 'entry_score', align: 'center', title: "得分",templet: function(d){
                        if(d.entry_score == null) return "";
                        else  return d.entry_score;
                }}
            , {field: 'entry_rating', align: 'center', title: "评级",templet: function(d){
                    if(d.entry_rating == null) return "";
                    else  return d.entry_rating;
                }}
            , {field: 'entry_apptype', align: 'center', title: "状态",templet: function(d){
                        if(d.entry_apptype=='0')return '编辑中';
                        if(d.entry_apptype=='8')return '待评级';
                        if(d.entry_apptype=='3')return '评级中';
                        if(d.entry_apptype=='9')return '已完成';
                        return '考核中';


                }}
            , {align: 'center', title: "操作",templet: function(d){
                    if(d.entry_apptype=='9')return '<button class="layui-btn  layui-btn-xs" lay-event="look">查看</button> <button class="layui-btn  layui-btn-xs" lay-event="interview">绩效面谈</button>';
                    return '<button class="layui-btn  layui-btn-xs" lay-event="look">查看</button>';

			}} 
            ]
        ]
        , id: 'halfListTb'
        , page: true
    });

    // 监听表格工具栏
    table.on('tool(halfListFilter)', function (obj) {
        var data = obj.data //当前行数据
            , event = obj.event //事件
            ,issuper =data.issuper
            ,apptype =data.entry_apptype
            ,interviewtime = data.entry_interviewtime
            ,entryno = data.entry_no;

        if (event == 'look') {// 查看
           /* if(type == "2"){ //查看下级考核人数据
                window.location.href  = "/jxp/toJxKpiQueryPage?entryid=" + data.entry_id + "&type=" + type;
            }else if(type == "1"){//查看打过分考核人数据
                window.location.href  = "/jxp/toJxKpiQueryPage?entryid="+data.entry_id + + "&type=" + type;
            }*/
            window.location.href  = "/jxp/toJxKpiQueryPage?entryid=" + data.entry_id + "&type=2";
        }
        if (event == 'interview') {// 绩效面谈

            if(issuper == 1 ){ // 直属上级填写面谈
                window.location.href  = "/jxp/toJxInterviewAddPage?entryId=" + data.entry_id ;
            }else if((!issuper || issuper != 1) && !interviewtime){ //非直属上级
                layer.msg('还没有面谈记录!',{time: 1000});
            }else{ //查看面谈
                window.location.href  = "/jxp/toJxInterviewPage?entryId=" + data.entry_id ;
            }
        }
        
    });

    var $ = layui.$, active = {
        reload: function () {
            var khcycle = $('#khcycle');
            var khOrg = $('#khOrg');
            var personName = $('#personName');
                //执行重载
                table.reload('halfListTb', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , limit: 10
                    , where: {
                        cycle: khcycle.val()
                        , khOrg: khOrg.val()
                        ,personName:personName.val()
                    }
                });



        }
    };

    $('#searchForm .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


});

/**
 * 考核类型 下拉框值改变，重新渲染考核月份
 * @param type
 */
function typeChange(type) {
    var monthlist = [["请选择...", ""], ["一月", "1"], ["二月", "2"], ["三月", "3"], ["四月", "4"], ["五月", "5"], ["六月", "6"], ["七月", "7"], ["八月", "8"], ["九月", "9"], ["十月", "10"], ["十一月", "11"], ["十二月", "12"]];
    var list = [];
    var khCycle = document.getElementById("khCycle");
    var khtype = document.getElementById("khtype");
    khCycle.length = 0; //下拉框清空
    // 根据考核类型初始化下拉框列表
    if (!(type == null || type == "")) {
        if (type == 1) {
            khtype.innerHTML = "考核月份";
            list = monthlist;
            $("#khCycle").attr("name","cycle_month");
        } else if (type == 3) {
            khtype.innerHTML = "年度类型";
            list = [["请选择...", ""], ["半年度", 1], ["年度", 2]];
            $("#khCycle").attr("name","cycle_yeartype");
        } else if (type == 2) {
            khtype.innerHTML = "考核季度";
            list = [["请选择...", ""], ["一季度", 1], ["二季度", 2], ["三季度", 3], ["四季度", 4]];
            $("#khCycle").attr("name","cycle_quarter");
        }
    }

    // 追加下拉框选项
    for (var i = 0; i < list.length; i++) {
        var option = document.createElement("Option");
        option.text = list[i][0];
        option.value = list[i][1];
        khCycle.options[khCycle.length] = option;
    }
}



/**
 * 点击年份选择器，进行渲染
 */
function yearClick() {
    layui.use('laydate', function () {
        var laydate = layui.laydate;

        // 考核年份选择器渲染
        laydate.render({
            elem: '#year' //指定元素
            , type: 'year' //年选择器（year，month，date，time，datetime）
            , format: 'yyyy' //自定义格式
            , min: '1900-1-1' //最小值
            , max: '9999-12-31' //最大值
        });
    });
}

/**
 * 点击月份选择器，进行渲染
 */
function monthClick() {
    var type = $('#type').val();
    typeChange(type);
    layui.use('form', function () {
        var form = layui.form;
        form.render('select', 'month');
    });

}

