/**
 * Created by likuan on 9/13 0013.
 */
var school={};
//#######################
//默认加载并执行的方法
//#######################
school.init = function(){

    if($('.xuexiao-table').size()){
        school.func.template($('.xuexiao-table'),$('.xuxiao-tpl-table'),school.func.data);
        school.func.table($(".table-cn"));
    }
    //批量上传按钮点击事件
    if($('.js-pl-add').size()){
        $('.js-pl-add').click(function(){
            school.func.update();
        });
    }
    //添加或编辑页面上的图片剪切
    if($('#crop-avatar').size()){
        school.func.img_cropper.func($('#crop-avatar'));
    }
    //删除功能
    if($('.js-del').size()){
        $('.js-del').click(function(){
         deldata('您确认要删除所选的信息吗？');
        })
    }
    //审核
    if($('.kk-btn').size()){
        $('.kk-btn').on('click', function(){
            var _t= $(this);
            _t.next('.shenghe-cn').show();
            $(this).parent().find('input[type="radio"]').click(function(){
                _t.next('.shenghe-cn').hide();
            });
            _t.next('.shenghe-cn').mouseleave(function(){
                _t.next('.shenghe-cn').hide();
            })
        });
    }

};$(school.init);

//----------华丽的分割线-----------------

//##############################
//学校模块,方法集合
//##############################
school.func = {
    update:function(){
        layer.open({
            type: 1,
            title: "上传文件",
            closeBtn: true,
            shadeClose: true,
            skin: 'my-doug',
            content: '<div class="shang-liaot-c js-shangc">' +
            '<div class="tit-le">' +
            '<input type="text" class="wenjin-shangc" placeholder="请选择上传的文件">' +
            '<button class="button-cc"  value="">选择文件</button>' +
            '<input type="file" class="file-cs-js" onchange="$(this).prev().prev().val($(this).val());"></div>' +
            '<div class="xia-moban">请选择EXL文件，您也可以先<a href="">下载模板</a>再上传！</div>' +
            '<div class="shangchuan-an"><button>确认</button> <button class="quxiaob-x">取消</button></div>'
        });
    },
    // 表格渲染
    table:function(tag){
        tag.find('.table-c').DataTable({
            "language": {
                "url": "/public/new_site/scripts/datatable/Chinese.json"
            },
            "aaSorting": [
                [1, "desc"]
            ], //默认某列排序
            "aoColumnDefs": [{
                "bSortable": false,
                "aTargets": [0]
            }],
            "fnInitComplete": function() { //完成后事件
                if(tag.find('.table-btn').size()){
                    tag.find('.dataTables_filter').before(tag.find('.table-btn'));
                }
                if(tag.find('.table-btn').size()){
                    tag.find('.dataTables_filter').after(tag.find('.table-right'));
                }
            }
        });
        if(tag.find('th input[type="checkbox"]').size()){
            tag.find('th input[type="checkbox"]').click(function(){
                if($(this).prop('checked')){
                    tag.find('td input[type="checkbox"]').prop('checked','checked');
                } else{
                    tag.find('td input[type="checkbox"]').prop('checked',false);
                }
            });
        }
    },
    //模板渲染
    template:function(tag,tpl,data){
        tag.html(Handlebars.compile(tpl.html())(data));
    },
    //弹出裁剪框
    img_cropper:{
        func:function(tag){

            var options ={imageBox: '.imageBox',thumbBox: '.thumbBox',spinner: '.spinner',imgSrc: 'avatar.png'};
            var cropper;
            tag.click(function(){
                log($('.avatar-modal').size());
                var _m = $('.avatar-modal');
                var is_up= 0;
                var img;
                _m.modal('show').find('.modal-dialog').css('margin-top','120px');
                _m.find('.img-file').on('change', function(){
                    is_up=1;
                    _m.find('.action').show();
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        options.imgSrc = e.target.result;
                        cropper = new cropbox(options);
                    };
                    reader.readAsDataURL(this.files[0]);
                    //this.files = [];
                });
                _m.find('.btnCrop-bn').on('click', function(){
                    if(is_up){
                        img = cropper.getDataURL();
                        $(this).next().show();
                        $('.cropped1 img,.cropped2 img,.cropped3 img').attr('src',img);
                    }else{
                        layer.msg('请选上传文件..');
                    }
                }).end().find('.btnZoomIn-bn').on('click', function(){cropper.zoomIn()}).end().find('.btnZoomOut-bn').on('click', function(){
                    cropper.zoomOut();
                });
                $('.js-save-img').click(function(){
                    _m.modal('hide');
                    $('.js-touxiang-c').attr('src',img);
                })
            });
        }
    },
    //模拟数据
    data:[
        {
            "id":"1",
            "name":"北京大学",
            "add":"北京市崇文区张安大街34号",
            "xz":"张浩",
            "by":"1987-08-01",
            "zt":"已付款"
        },
        {
            "id":"2",
            "name":"山东大学",
            "add":"山东省济南市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },{
            "id":"3",
            "name":"复旦大学",
            "add":"上海市杨浦区四品路124号",
            "xz":"张浩",
            "by":"1987-08-01",
            "zt":"已付款"
        },
        {
            "id":"4",
            "name":"清华大学",
            "add":"北京市宣武区青春路2391号",
            "xz":"张含韵",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"5",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"6",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"7",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"8",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"9",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"10",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        },
        {
            "id":"11",
            "name":"上海交通大学",
            "add":"上海市张安大街34号",
            "xz":"李如",
            "by":"1987-08-01",
            "zt":"未付款"
        }
    ]
};
