$(function () {
    'use strict';
    //主页面
    $(document).on("pageInit", "#login-page", function(e, id, page) {
        $(page).on("click", "#login", function(e) {
            var username = $.trim($("#username").val());
            var password = $.trim($("#password").val());
            if(false == checkNull()) return false;//检查空输入
            $.ajax({
                url: 'index.php?d=wap_teacher&c=login&m=check_login',   //后台处理程序
                type: "post",         //数据发送方式
                dataType:"json",    //接受数据格式
                beforeSend: function () {
                    //加载提示符
                    $.showIndicator();
                },
                complete: function () {
                    $.hideIndicator();
                },
                data:{username:username,password:password},  //要传递的数据
                success:function(data) {
                    if (data.err_code!=0) {
                        $.alert(data.err_msg);

                    }else
                    {
                        $.toast(data.err_msg, 2000, 'success top');
                        window.location.replace('index.php?d=wap_teacher&c=home&m=index');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $.alert(errorThrown);
                }
            });
        });
        function checkNull(){
            $("#username").val($.trim($("#username").val()));
            if(!$("#username").val() || !$("#password").val() || $("#username").val() == $("#username").attr('prompt')) {
                if(!$("#username").val() || $("#username").val() == $("#username").attr('prompt')){
                    $.alert('请输入用户名!');
                    return false;
                }
                if(!$("#password").val()){

                    $.alert('请输入密码!');
                    return false;
                }
            }
            return true;
        }
    });
    //密码修改页面
    $(document).on("pageInit", "#change-password-page", function(e, id, page) {
        $(page).on("click", "#submit", function(e) {
            var old_password = $.trim($("#old_password").val());
            var new_password = $.trim($("#new_password").val());
            var re_new_password = $.trim($("#re_new_password").val());
            if(false == checkNull()) return false;//检查空输入
            if(new_password!=re_new_password)
            {
                $.alert('两次输入密码不一致');
                return false;
            }
            $.ajax({

                url: 'index.php?d=wap_teacher&c=home&m=password_save',   //后台处理程序
                type: "post",         //数据发送方式
                dataType:"json",    //接受数据格式
                data:{old_password:old_password,new_password:new_password},  //要传递的数据
                success:function(data) {
                    if (data.err_code) {
                        $.alert(data.err_msg);
                    }
                    else
                    {
                        $.toast('操作成功，正在跳转...', 2000, 'success top');
                        window.location.replace('index.php?d=wap_teacher&c=login&m=index');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $.alert(errorThrown);
                }
            });
        });
        function checkNull(){
            $("#old_password").val($.trim($("#old_password").val()));
            if(!$("#old_password").val() || !$("#new_password").val() || !$("#re_new_password").val() || $("#old_password").val() == $("#old_password").attr('prompt')) {
                if(!$("#old_password").val() || $("#old_password").val() == $("#old_password").attr('prompt')){
                    $.alert('请输入旧密码');
                    return false;
                }
                if(!$("#new_password").val()){
                    $.alert('请输入新密码');
                    return false;
                }
                if(!$("#re_new_password").val()){
                    $.alert('请输入确认密码');
                    return false;
                }
            }
            return true;
        }
    });
    //头像修改
    $(document).on("pageInit", "#change-avatar-page", function(e, id, page) {
        var f = document.querySelector('#image-uploader');
        f.onchange = function () {
            lrz(this.files[0],{width:640,fieldName:"upfile"}).then(function (rst) {
                var xhr = new XMLHttpRequest();
                xhr.open('POST', 'index.php?d=wap_teacher&c=file&m=image_upload');

                xhr.onload = function () {
                    if (xhr.status === 200) {
                        var obj = eval('(' + xhr.responseText + ')');
                        $('#thumb').attr('src',obj.full_url );
                        $("#headimgurl").val(obj.url);
                    } else {
                        // 处理其他情况
                    }
                };

                xhr.onerror = function () {
                    // 处理错误
                };

                xhr.upload.onprogress = function (e) {
                    // 上传进度
                    var percentComplete = ((e.loaded / e.total) || 0) * 100;
                }

                // 添加参数
                rst.formData.append('size', rst.fileLen);
                rst.formData.append('base64', rst.base64);
                // 触发上传
                xhr.send(rst.formData);

                return rst;
            })

                .catch(function (err) {
                    alert(err);
                })

                .always(function () {// 不管是成功失败，这里都会执行
                });
        }
        $(page).on("click", "#submit", function(e) {
            var headimgurl = $.trim($("#headimgurl").val());

            if(headimgurl=='')
            {
                $.alert('照片不能为空');
                return false;
            }
            $.ajax({
                url: 'index.php?d=wap_teacher&c=home&m=avatar_save',   //后台处理程序
                type: "post",         //数据发送方式
                dataType:"json",    //接受数据格式
                data:{thumb:headimgurl},  //要传递的数据
                success:function(data) {
                    if (data.err_code!=0) {
                        $.alert(data.err_msg);
                    }
                    else
                    {
                        $.toast('操作成功，正在跳转...', 2000, 'success top');
                        window.location.replace('index.php?d=wap_teacher&c=home&m=teacher_info');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $.alert(errorThrown);
                }
            });
        });
    });
    /*约车*/
    $(document).on("pageInit", "#train-booking-detail-page", function (e, id, page) {

        $(page).on('click', '.change-status', function () {
            var list=statuss;
            var html = '<div class="list-block media-list"><ul>';
            for (var i in list) {

                html += '<li> ' +
                '<label class="label-checkbox item-content"> ' +
                '<input type="radio" name="my-radio" data-key=' + i + ' value="' + list[i] + '"> ' +
                '<div class="item-media"><i class="icon icon-form-checkbox"></i></div> ' +
                '<div class="item-inner"> ' +
                '<div class="item-title-row"> ' +
                '<div class="item-title">' + list[i] + '</div> ' +
                '</div> ' +
                '</div> ' +
                '</label> ' +
                '</li>';
            }
            html += '</<ul></div>';
            $.modal({
                title: '约考审核：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var checked_value = $("input[name='my-radio']:checked").attr('value');
                            var key = $("input[name='my-radio']:checked").attr('data-key');
                            if (key != undefined) {
                                $('.change-status').html('状态：'+checked_value);
                                if(key==2)
                                {
                                    $('.change-status').removeClass('state1').addClass('state');
                                }
                                else
                                {
                                    $('.change-status').removeClass('state').addClass('state1');
                                }
                                var value={active:key};
                                $.post('index.php?d=wap_teacher&c=train_booking&&m=save', {id:train_id,value: value}, function (text, status) {
                                });
                            }
                        }
                    }
                ]
            });
        });
        $(page).on('click', '.change-reply', function () {
            var reply=$(this).find('p').html();
            var html = '<div class="list-block inset"> <ul> ' +
                '<li class="align-top"><div class="item-content"> ' +
                '<div class="item-inner"><div class="item-input"> ' +
                '<textarea placeholder="请输入留言内容" id="content">'+reply+'</textarea> ' +
                '</div></div></div></li></ul></div>';
            $.modal({
                title: '教练留言：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var content = $("#content").val();
                            var value={reply:content};
                            $.post('index.php?d=wap_teacher&c=train_booking&&m=save', {id:train_id,value: value}, function (text, status) {
                                $('.change-reply').find('p').html(content);
                            });
                        }
                    }
                ]
            });
        });
    });
    /*约车*/
    $(document).on("pageInit", "#train-page", function (e, id, page) {

        $(page).on('click', '.change-active', function () {
            var list=statuss;
            var train_id=$(this).attr('data-id');
            var html = '<div class="list-block media-list"><ul>';
            for (var i in list) {

                html += '<li> ' +
                '<label class="label-checkbox item-content"> ' +
                '<input type="radio" name="my-radio" data-key=' + i + ' value="' + list[i] + '"> ' +
                '<div class="item-media"><i class="icon icon-form-checkbox"></i></div> ' +
                '<div class="item-inner"> ' +
                '<div class="item-title-row"> ' +
                '<div class="item-title">' + list[i] + '</div> ' +
                '</div> ' +
                '</div> ' +
                '</label> ' +
                '</li>';
            }
            html += '</<ul></div>';
            $.modal({
                title: '练车审核：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var checked_value = $("input[name='my-radio']:checked").attr('value');
                            var key = $("input[name='my-radio']:checked").attr('data-key');
                            if (key != undefined) {
                                $('.change-status').html('状态：'+checked_value);
                                if(key==0)
                                {
                                    $('.change-active').html('未确认');
                                }
                                else
                                {
                                    $('.change-active').html('已确认');
                                }
                                var value={active:key};
                                $.post('index.php?d=wap_teacher&c=train&m=status_save', {id:train_id,value: value}, function (text, status) {
                                });
                            }
                        }
                    }
                ]
            });
        });
    });
    //练车
    $(document).on("pageInit", "#train-add-page", function (e, id, page) {
        $(page).on("click", "#submit", function(e) {
            var studentid = $.trim($("#studentid").val());
            var begintime = $.trim($("#begintime").val());
            var endtime = $.trim($("#endtime").val());
            var vehicleid = $.trim($("#vehicleid").val());
            var content = $.trim($("#content").val());
            if(studentid=='')
            {
                $.alert('请选择学员');
                return false;
            }
            if(begintime=='')
            {
                $.alert('请选择开始时间');
                return false;
            }
            if(endtime=='')
            {
                $.alert('请选择结束时间');
                return false;
            }
            if(vehicleid=='')
            {
                $.alert('请选择车辆信息');
                return false;
            }
            var value={
                studentid:studentid,
                begintime:begintime,
                endtime:endtime,
                vehicleid:vehicleid,
                content:content
            };
            $.ajax({
                url: 'index.php?d=wap_teacher&c=train&m=save',   //后台处理程序
                type: "post",         //数据发送方式
                dataType:"json",    //接受数据格式
                data:{value:value},  //要传递的数据
                success:function(data) {
                    if (data.err_code) {
                        $.alert(data.err_msg);
                    }
                    else
                    {
                        $.toast('操作成功，正在跳转...', 2000, 'success top');
                        window.location.replace('index.php?d=wap_teacher&c=train&m=index');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $.alert(errorThrown);
                }
            });
        });
        $(page).on('click', '#student', function () {
            var list=student;
            var html = '<div class="list-block media-list"><ul>';
            for (var i in list) {

                html += '<li> ' +
                '<label class="label-checkbox item-content"> ' +
                '<input type="radio" name="my-radio" data-key='+list[i]['student_id']+' value="' +list[i]['real_name']+ '"> ' +
                '<div class="item-media"><i class="icon icon-form-checkbox"></i></div> ' +
                '<div class="item-inner"> ' +
                '<div class="item-title-row"> ' +
                '<div class="item-title">' + list[i]['real_name']+ '</div> ' +
                '</div> ' +
                '</div> ' +
                '</label> ' +
                '</li>';
            }
            html += '</<ul></div>';
            $.modal({
                title: '学员选择：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var key = $("input[name='my-radio']:checked").attr('data-key');
                            var checked_value = $("input[name='my-radio']:checked").attr('value');
                            if (key != undefined) {
                                $('#student').val(checked_value);
                                $('#studentid').val(key);
                            }
                        }
                    }
                ]
            });
        });
        $(page).on('click', '#vehicle', function () {
            var list=vehicle;
            var html = '<div class="list-block media-list"><ul>';
            for (var i in list) {

                html += '<li> ' +
                '<label class="label-checkbox item-content"> ' +
                '<input type="radio" name="my-radio" data-key='+list[i]['vehicle_id']+' value="' +list[i]['vehicle_num']+ '"> ' +
                '<div class="item-media"><i class="icon icon-form-checkbox"></i></div> ' +
                '<div class="item-inner"> ' +
                '<div class="item-title-row"> ' +
                '<div class="item-title">' + list[i]['vehicle_num']+ '</div> ' +
                '</div> ' +
                '</div> ' +
                '</label> ' +
                '</li>';
            }
            html += '</<ul></div>';
            $.modal({
                title: '车辆选择：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var key = $("input[name='my-radio']:checked").attr('data-key');
                            var checked_value = $("input[name='my-radio']:checked").attr('value');
                            if (key != undefined) {
                                $('#vehicle').val(checked_value);
                                $('#vehicleid').val(key);
                            }
                        }
                    }
                ]
            });
        });
    });
    //车辆维修
    $(document).on("pageInit", "#maintenance-add-page", function (e, id, page) {
        $(page).on("click", "#submit", function(e) {
            var projects = $.trim($("#projects").val());
            var publish_date = $.trim($("#publish_date").val());
            var address = $.trim($("#address").val());
            var vehicleid = $.trim($("#vehicleid").val());
            var charges = $.trim($("#charges").val());
            var brokerage = $.trim($("#brokerage").val());
            var content = $.trim($("#content").val());
            if(vehicleid=='')
            {
                $.alert('请选择车辆信息');
                return false;
            }
            if(projects=='')
            {
                $.alert('请选择保养项目');
                return false;
            }
            if(publish_date=='')
            {
                $.alert('请选择保养时间');
                return false;
            }
            if(content=='')
            {
                $.alert('请选填写描述信息');
                return false;
            }
            var value={
                projects:projects,
                publish_date:publish_date,
                address:address,
                vehicleid:vehicleid,
                charges:charges,
                brokerage:brokerage,
                content:content
            };
            $.ajax({
                url: 'index.php?d=wap_teacher&c=maintenance&m=save',   //后台处理程序
                type: "post",         //数据发送方式
                dataType:"json",    //接受数据格式
                data:{value:value},  //要传递的数据
                success:function(data) {
                    if (data.err_code) {
                        $.alert(data.err_msg);
                    }
                    else
                    {
                        $.toast('操作成功，正在跳转...', 20000, 'success top');
                        window.location.replace('index.php?d=wap_teacher&c=maintenance&m=index');
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $.alert(errorThrown);
                }
            });
        });
        $(page).on('click', '#vehicle', function () {
            var list=vehicle;
            var html = '<div class="list-block media-list"><ul>';
            for (var i in list) {

                html += '<li> ' +
                '<label class="label-checkbox item-content"> ' +
                '<input type="radio" name="my-radio" data-key='+list[i]['vehicle_id']+' value="' +list[i]['vehicle_num']+ '"> ' +
                '<div class="item-media"><i class="icon icon-form-checkbox"></i></div> ' +
                '<div class="item-inner"> ' +
                '<div class="item-title-row"> ' +
                '<div class="item-title">' + list[i]['vehicle_num']+ '</div> ' +
                '</div> ' +
                '</div> ' +
                '</label> ' +
                '</li>';
            }
            html += '</<ul></div>';
            $.modal({
                title: '车辆选择：',
                extraClass: 'home-modal',
                text: html,
                buttons: [
                    {
                        text: '确认',
                        bold: true,
                        onClick: function () {
                            var key = $("input[name='my-radio']:checked").attr('data-key');
                            var checked_value = $("input[name='my-radio']:checked").attr('value');
                            if (key != undefined) {
                                $('#vehicle').val(checked_value);
                                $('#vehicleid').val(key);
                            }
                        }
                    }
                ]
            });
        });
    });
    $.init();
});

