/**
 * 考核列表
 */
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: '/jx/getJxManagerList' //异步接口
        , even: true // 开启隔行背景
        , size: 'sm' // 小尺寸
        ,jump:function(obj, first){
            if(!first){
                alert(1);
            }
        }
        , cols: [[{type: 'numbers'},{type: 'checkbox', align: 'center'}
            , {field: 'cycle_name', align: 'center', title: "考核周期"}
            , {field: 'entryno_orgname', align: 'center', title: "考核组织"}
            , {field: 'entryno_creatorname', align: 'center', title: "发起人"}
            , {field: 'entryno_createtime', align: 'center', title: "发起时间"}
            , {field: 'entryno_appnode', align: 'center', title: "状态",templet: function(d){
                    if(d.entryno_appnode=='0')return '编辑中';
                    if(d.entryno_appnode=='5')return '指标审核中';
                    if(d.entryno_appnode=='7')return '待启动评分';
                    if(d.entryno_appnode=='1')return '评分中';
                    if(d.entryno_appnode=='2')return '待确认';
                    if(d.entryno_appnode=='9')return '完成';
                   return '';
                }}
            , {align: 'center', title: "操作",templet: function(d){
                    if(d.entryno_appnode=='0'){
                        return '<a class="layui-btn  layui-btn-xs" lay-event="edit">编辑</a>' +
                            //'<a class="layui-btn layui-btn-xs" lay-event="copy">复制</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="export">导出</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="delete">删除</a>';
                    }else if(d.entryno_online=='2' ){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">考核</a>' +
                            '<a class="layui-btn  layui-btn-xs" lay-event="delete" style="background-color: red">删除</a>';
                    }else if(d.entryno_appnode=='5' || d.entryno_appnode=='7' ){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">考核</a>' +
                            //'<a class="layui-btn  layui-btn-xs" lay-event="copy">复制</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="export">导出</a>'+
                            '<a href="/jx/toTrackWf?back=toJxManagerPage&entryNo='+d.entryno_id+'" class="layui-btn layui-btn-xs">跟踪</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="delete" style="background-color: red">删除</a>';
                    }else if(d.entryno_appnode=='1' || d.entryno_appnode=='2' ){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">考核</a>' +
                            // '<a class="layui-btn  layui-btn-xs" lay-event="rating">评级</a>'+

                           '<a class="layui-btn  layui-btn-xs" lay-event="export">导出</a>'+
                            '<a href="/jx/toTrackWf?back=toJxManagerPage&entryNo='+d.entryno_id+'" class="layui-btn layui-btn-xs">跟踪</a>'+
                            '<a href="/jx/toTrackWf2?back=toJxManagerPage&entryNo='+d.entryno_id+'" class="layui-btn layui-btn-xs">确认跟踪</a>';
                    }else if(d.entryno_appnode=='9' ){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">考核</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="export">导出</a>'+
                            '<a href="/jx/toTrackWf2?back=toJxManagerPage&entryNo='+d.entryno_id+'" class="layui-btn layui-btn-xs">确认跟踪</a>';
                   }else{

                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">考核</a>' +
                            //'<a class="layui-btn  layui-btn-xs" lay-event="copy">复制</a>'+
                            '<a class="layui-btn  layui-btn-xs" lay-event="export">导出</a>'+
                            '<a href="/jx/toTrackWf?back=toJxManagerPage&entryNo='+d.entryno_id+'" class="layui-btn layui-btn-xs">跟踪</a>';
                   }



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

    // 监听表格工具栏
    table.on('tool(halfListFilter)', function (obj) {
        var data = obj.data //当前行数据
            , event = obj.event //事件

            , jxcycle_id = data.cycle_id
            , type = data.cycle_type
            , year = data.cycle_year
            ,entryno = data.entryno_id;

        if (event == 'copy') {// 查看
            layer.open({
                type: 2,
                title: '复制考核数据',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area: ['800px', '520px'],
                content: '/jx/toCopyJxEntrysPage?entryno=' + entryno
            });
        }else if(event == 'delete'){
            var d = {};
            d.entryno = entryno;
            if(d.entryno_appnode=='0'){
                layer.confirm('确定要删除吗?', function(){
                    AjaxTool.XPost("/jx/delJxEntrysByEntryNo", d, function(data) {
                        if(data.code == 'success'){
                            layer.alert('删除成功',function (data) {
                                window.location.href  = "/jx/toJxManagerPage";
                            });
                        }else{
                            layer.alert("删除失败!"+(data.msg?data.msg:''),{icon:5})
                        }

                    }, function(data) {
                        layer.alert("删除失败，您可能已掉线，请刷新页面或关闭后重试！!"+(data.msg?data.msg:''),{icon:5})
                    });

                });
            }else{
                layer.confirm('  警告！('+data.cycle_name+'-'+data.entryno_orgname+')考核流程已启动，删除操作将清空考核数据和流程，操作不可恢复！确定要删除吗?', function(){
                    AjaxTool.XPost("/jx/delJxEntrysByEntryNo", d, function(data) {
                        if(data.code == 'success'){
                            layer.alert('删除成功',function (data) {
                                window.location.href  = "/jx/toJxManagerPage";
                            });
                        }else{
                            layer.alert("删除失败!"+(data.msg?data.msg:''),{icon:5})
                        }

                    }, function(data) {
                        layer.alert("删除失败，您可能已掉线，请刷新页面或关闭后重试！!"+(data.msg?data.msg:''),{icon:5})
                    });

                });
            }

        }else if(event == 'edit'){
        	window.location.href  = "/jx/toJxEntryPage?back=toJxManagerPage&entryno="+entryno;
        }else if(event == 'rating'){
                layer.open({
                    type : 2,
                    title : '导入数据',
                    maxmin : true,
                    shadeClose : true, //点击遮罩关闭层
                    area : [ '600px', '200px' ],
                    content : '/excel/importUi?type=2&entryno='+entryno
                });
        }else if(event == 'export'){
            layer.open({
                type : 2,
                title : '导出数据',
                maxmin : true,
                shadeClose : true, //点击遮罩关闭层
                area : [ '350px', '300px' ],
                content : '/excel/exportUi?type=1&entryno='+entryno
            });

        }
        
    });

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

                    }
                });
        },
        reload1: function () {
            var khcycle = $('#khcycle');
            var khOrg = $('#khOrg');

            //执行重载
            table.reload('halfListTb', {
                page: {
                    curr: $('#reloadpage').val() //重新从第 1 页开始
                }
                , limit: $('#reloadlimit').val()
                , where: {
                    cycle_name: khcycle.val()
                    , khOrg: khOrg.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');
    });

}

