'use strict';

/**
 * Created by Administrator on 2016/7/27.
 */
(function () {
    var $form = $('#container .introducer');
    var $submit = $('input[type="submit"]', $form);
    var $introducerMobile = $('#introducerMobile', $form);
    var $projectDescription = $('#projectDescription', $form);
    var $errorPhone = $('.error-phone', $form);
    var $errorDescription = $('.error-description', $form);
    var $successDialog = $('.modal-box-success');

    var filterMobileInput = function filterMobileInput($mobile) {
        $mobile.val(($mobile.val() + '').replace(/[^\d]/g, ''));
    };

    var validateMobile = function validateMobile() {
        var mobile = $.trim($introducerMobile.val() + '');
        if (/^[\d]{11}$/g.test(mobile)) {
            $errorPhone.empty();
            return mobile;
        } else {
            $errorPhone.html('手机号输入错误!');
            return false;
        }
    };

    var validateDescription = function validateDescription() {
        var description = $.trim($projectDescription.val() + '');
        if (description.length) {
            $errorDescription.empty();
            return description;
        } else {
            $errorDescription.html('请输入项目描述内容!');
            return false;
        }
    };

    $introducerMobile.on('keyup', function (e) {
        filterMobileInput($introducerMobile);
    }).on('change blur', function (e) {
        validateMobile();
    }).on('focus', function (e) {
        $errorPhone.empty();
    });

    $projectDescription.on('change blur', function (e) {
        validateDescription();
    }).on('focus', function (e) {
        $errorDescription.empty();
    });

    $successDialog.on('click', '.modal-dialog-close', function (e) {
        $successDialog.fadeOut(300);
    });

    $form.on('submit', function (e) {
        e.preventDefault();
        var mobile = validateMobile();
        var description = validateDescription();
        if (mobile && description) {
            request('/api/1.0/proj/add.json', {
                mobile: mobile,
                description: description
            }, 'POST').then(function () {
                $successDialog.fadeIn(300);
            }).catch(function () {
                alert('提交失败, 请稍后再试!');
            });
        }
    });
})();

(function () {
    var $modalBox = $('.modal-box');

    $('.hot-project-address').on('click', 'a', function (e) {
        e.preventDefault();
        $modalBox.fadeIn(300);
    });

    $modalBox.on('click', '.modal-dialog-close', function (e) {
        e.preventDefault();
        $modalBox.fadeOut(300);
    });
})();

// (function () {
//
//
//
//
//
//     var $modalBoxProject = $('.modal-box-project');
//     var $modalBoxSuccess = $('.modal-box-success');
//
//
//
//
//     /**
//      * 地图项目地址弹框
//      */
//     $('.hot-project-address a').click(function (e) {
//         e.preventDefault();
//         $modalBox.fadeIn(300);
//     });
//     $('#modalBoxHidden').click(function (e) {
//         e.preventDefault();
//         $modalBox.fadeOut(300);
//     });
//
//     /**
//      * 项目提交弹框
//      */
//     $submit.on('click',function(e){
//         e.preventDefault();
//         $modalBoxSuccess.fadeIn(300);
//     });
//     $('.modal-box-success a').on('click',function(e){
//         e.preventDefault();
//         $modalBoxSuccess.fadeOut(300);
//     });
//
//
// })();

//# sourceMappingURL=project.js.map