/**
 * 考核列表
 */
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' //最大值
        ,change: function(value){
            $('#year').val(value);
            $('#layui-laydate100001').remove();
        }
    });

    table.render({
        elem: '#halfList'
        , url: '/jx/getJxCycleList' //异步接口
        , even: true // 开启隔行背景
        , size: 'sm' // 小尺寸
        , cols: [[{type: 'numbers'},{type: 'checkbox', align: 'center'}
            , {field: 'cycle_name', align: 'center', title: "周期名称", sort: true}
            , {field: 'cycle_type', align: 'center', title: "考核类型",templet: function(d){
                if(d.cycle_type=='1')return '月度考核';
                if(d.cycle_type=='2')return '季度考核';
                if(d.cycle_type=='3')return '半年/年度考核';
                return ''
			}}
            , {field: 'cycle_year', align: 'center', title: "年份", sort: true}
            , {field: 'cycle_quarter', align: 'center', title: "季度/年度",templet: function(d){
                    if(d.cycle_type=='1')return d.cycle_month+'月';
                    if(d.cycle_type=='2'){
                        if(d.cycle_quarter=='1')return '一季度';
                        if(d.cycle_quarter=='2')return '二季度';
                        if(d.cycle_quarter=='3')return '三季度';
                        if(d.cycle_quarter=='4')return '四季度';
                    }
                    if(d.cycle_type=='3'){
                        if(d.cycle_yeartype=='1')return '上半年';
                        if(d.cycle_yeartype=='2')return '下半年';
                        if(d.cycle_yeartype=='3')return '年度';
                    }
                    return ''
                }}

            , {field: 'cycle_createtime', align: 'center', title: "创建时间"}
            , {align: 'center', title: "操作",templet: function(d){

                	return '<button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del">删除</button>';
			}} 
            ]
        ]
        , 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
        if(event == 'edit'){
        	//window.location.href  = "/jx/toJxEntryPage?cycleid="+jxcycle_id;
        }else if(event == 'del'){
        	 var d = {};
        	 d.cycleId = jxcycle_id;
        	 layer.confirm('确认要删除？', function(index){
        		 AjaxTool.XPost("/jx/deleteJxCycle", d, function(data) {
        			 if(data.code == '0'){
        					layer.msg('删除成功!',{time: 1000});
        					obj.del();
                	        layer.close(index);
                      }else  if(data.code == '1'){
                             layer.msg('删除失败,该周期已被考核引用!',{time: 1000});

        				}else{
        					layer.alert("删除失败"+(data.msg?data.msg:''),{icon:6})
        				}
     			 }, function(data) {
     				layer.alert("删除失败"+(data.msg?data.msg:''),{icon:6})
     			 });
        	         
        	 });
        }
        
    });

    var $ = layui.$, active = {
        reload: function () {
           // var name = $('#name');
            var type = $('#type');
            var year = $('#year');
           // var khCycle = $('#khCycle');


                //执行重载
                table.reload('halfListTb', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , limit: 10
                    , where: {
                        cycle_type: type.val()
                        , cycle_year: year.val()

                    }
                });



        }
    };

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


});





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

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

        });
    });
}

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

}

