var showAddCustomerForm ;
$(function () {
    //日历
    layui.use(['laydate','form'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        // 预产日期
        laydate.render({
            elem: '#lr_ready_baby_day'
            , format: 'yyyy-MM-dd'
            , value: $("#ready_baby_day").val()
            , trigger: 'click'
        });
        // 宝宝生日
        laydate.render({
            elem: '#lr_baby_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#baby_birthday").val()
            , trigger: 'click'
            ,done: function(value, date, endDate){//控件选择完毕后的回调---点击日期、清空、现在、确定均会触发。
                //录入宝宝生日的时候，如果预产期没有填，把时间同步到预产期 2021-05-20
                if (value.length > 0) {
                    var lr_ready_baby_day = $("#lr_ready_baby_day").val();
                    if (lr_ready_baby_day.length == 0) {
                        $("#lr_ready_baby_day").val(value);
                    };
                };
            }
        });
        // 妈妈生日
        laydate.render({
            elem: '#lr_customer_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#lr_customer_birthday").val()
            , trigger: 'click'
        });
        // 爸爸生日
        laydate.render({
            elem: '#lr_mate_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#lr_mate_birthday").val()
            , trigger: 'click'
        });
        // 填写日期
        laydate.render({
            elem: '#lr_record_day'
            , format: 'yyyy-MM-dd'
            , value: $("#record_day").val()
            , trigger: 'click'
        });


        showAddCustomerForm = function(closewindow=1){
            var height = 650;
            var width = 740;
            // 网页可用高度
            var sHeihgt = $(window).height();
            if (sHeihgt>900) {
                height = 750;
            }else if (sHeihgt>700 && sHeihgt<800) {
                height = 650;
            }else if (sHeihgt>600 && sHeihgt<700) {
                height = 550;
            }else if (sHeihgt<600) {
                height = 450;
            }
            // 打开录入客资页面
            setTimeout(function(){
                layer.open({
                    title: '客资登记',
                    type: 1,
                    skin: 'layui-layer-rim',
                    area: [width+'px', height+'px'],
                    content: $('#add_new_child_customer'),
                    btn: ['提交','取消'],
                    yes:function(index){
                        var shade = layer.load(1, {shade: 0.3});
                        var data = {};
                        // var customer = $("#form_new").serialize();
                        // console.log(customer);

                        data.customer_type = $("#customer_type").val() ? $("#customer_type").val() : 1;
                        // 如果转介绍
                        if (data.customer_type==2) {
                            data.zjs_from = $('#lr_zjs_from').val();
                            if ( !data.zjs_from ) {
                                layer.alert("请选择转介绍类型");
                                layer.close(shade);
                                return false;
                            };
                            // 老顾客转介绍
                            if (data.zjs_from == 1) {
                                data.introducer_name = $('#lr_introducer_name').val();
                                data.introducer_ordernum = $('#lr_introducer_ordernum').val();
                                data.introducer_tel = $('#lr_introducer_tel').val();
                                if (!data.introducer_name && !data.introducer_ordernum && !data.introducer_tel) {
                                    layer.alert('老顾客转介绍时必须选择介绍人');
                                    layer.close(shade);
                                    return false;
                                };
                            };
                        };
                        // 介绍关系 1:亲属 2:朋友
                        data.zjs_rel = $('#zjs_rel').val() ? $('#zjs_rel').val() : 0;
                        // 登记人信息
                        if ($("#lr_add_shop_code").val()) {
                            data.add_shop_code = $("#lr_add_shop_code").val();
                            data.add_shop_name = $("#lr_add_shop_code option:selected").html();
                        };
                        if ($("#lr_dj_department_id").val()) {
                            data.dj_department_id  = $("#lr_dj_department_id").val();
                            data.dj_department_name= $("#lr_dj_department_id option:selected").html();
                        };
                        if ($("#lr_dj_staff_id").val()) {
                            data.dj_staff_id= $("#lr_dj_staff_id").val();
                            data.dj_staff   = $("#lr_dj_staff_id option:selected").html();
                        };
                        
                        if(!data.dj_staff_id){
                            layer.alert("请选择客资登记人");
                            layer.close(shade);
                            return false;
                        }
                        // 追踪人信息
                        if ($("#lr_shop_code").val()) {
                            data.shop_code = $("#lr_shop_code").val();
                            data.shop_name = $("#lr_shop_code option:selected").html();
                        };
                        if ($("#lr_sales_department_id").val()) {
                            data.sales_department_id = $("#lr_sales_department_id").val();
                            data.sales_department_name= $("#lr_sales_department_id option:selected").html();
                        };
                        if ($("#lr_sales_staff_id").val()) {
                            data.sales_staff_id= $("#lr_sales_staff_id").val();
                            data.sales_staff   = $("#lr_sales_staff_id option:selected").html();
                        };
                        if(isInArray('sales_staff_id',jslrMust)){
                            if (!data.sales_staff_id) {
                                layer.alert("追踪人不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 宝宝信息
                        if ( $('#lr_baby_name').val() ) { data.baby_name = $('#lr_baby_name').val() };
                        if(isInArray('baby_name',jslrMust)){
                            if (!data.baby_name) {
                                layer.alert("宝宝姓名不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_baby_sex').val() ) { data.baby_sex = $('#lr_baby_sex').val() };
                        if(isInArray('baby_sex',jslrMust)){
                            if (!data.baby_sex) {
                                layer.alert("宝宝性别不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_baby_stature').val() ) { data.baby_stature = $('#lr_baby_stature').val() };
                        if ( $('#lr_baby_weight').val() ) { data.baby_weight = $('#lr_baby_weight').val() };
                        if ( $('#lr_baby_time').val() ) {data.baby_time = $('#lr_baby_time').val()};
                        if ( $('#lr_baby_birthday').val() ) {data.baby_birthday = $('#lr_baby_birthday').val()};
                        if ( $('#lr_record_day').val() ) {data.record_day = $('#lr_record_day').val()};
                        if ( $('#lr_ready_baby_day').val() ) {data.ready_baby_day = $('#lr_ready_baby_day').val()};
                        if(!data.baby_birthday && !data.record_day && !data.ready_baby_day){
                            layer.alert("宝宝生日、预产期、填写日期必须填写一项");
                            layer.close(shade);
                            return false;
                        }
                        // 妈妈信息
                        if ( $('#lr_customer_name').val() ) { data.customer_name = $('#lr_customer_name').val() };
                        if(isInArray('customer_name',jslrMust)){
                            if (!data.customer_name) {
                                layer.alert("妈妈姓名不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_customer_tel').val() ) { data.customer_tel = $('#lr_customer_tel').val() };
                        if ( $('#lr_customer_wechat').val() ) { data.customer_wechat = $('#lr_customer_wechat').val() };
                        if(isInArray('customer_wechat',jslrMust)){
                            if (!data.customer_wechat) {
                                layer.alert("妈妈微信不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_customer_qq').val() ) { data.customer_qq = $('#lr_customer_qq').val() };
                        var regeQq=/[.*]/;
                        if(data.customer_qq&&regeQq.test(data.customer_qq)){
                            layer.alert('QQ号输入错误');
                            layer.close(shade);
                            return false;
                        }
                        if(isInArray('customer_qq',jslrMust)){
                            if (!data.customer_qq) {
                                layer.alert("妈妈QQ不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if(!data.customer_tel && !data.customer_wechat && !data.customer_qq){
                            layer.alert("请填写手机号、微信、QQ任一项联系方式");
                            layer.close(shade);
                            return false;
                        }
                        var patrn = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
                        if (data.customer_tel && !patrn.exec(data.customer_tel)) {
                            layer.alert("手机号输入错误");
                            layer.close(shade);
                            return false;
                        }
                        if ( $('#lr_customer_weibo').val() ) { data.customer_weibo = $('#lr_customer_weibo').val() };
                        if(isInArray('customer_weibo',jslrMust)){
                            if (!data.customer_weibo) {
                                layer.alert("妈妈微博不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_customer_birthday').val() ) { data.customer_birthday = $('#lr_customer_birthday').val() };
                        if(isInArray('customer_birthday',jslrMust)){
                            if (!data.customer_birthday) {
                                layer.alert("妈妈生日不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_customer_idtype').val() ) { data.customer_idtype = $('#lr_customer_idtype').val() };
                        if ( $('#lr_customer_idnumber').val() ) { data.customer_idnumber = $('#lr_customer_idnumber').val() };
                        // 座机 邮箱
                        if ( $('#lr_home_phone').val() ) { data.home_phone = $('#lr_home_phone').val() };
                        if(isInArray('home_phone',jslrMust)){
                            if (!data.home_phone) {
                                layer.alert("座机号码不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        if ( $('#lr_email').val() ) { data.email = $('#lr_email').val() };
                        if(isInArray('email',jslrMust)){
                            if (!data.email) {
                                layer.alert("邮箱地址不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 渠道名称 新客资 或者 babypastel 
                        if (data.customer_type ==1 || babypastel == 1) {
                            data.customer_channel_id = $('#lr_customer_channel_id').val();
                            if (!data.customer_channel_id) {
                                layer.alert("渠道名称不能为空");
                                layer.close(shade);
                                return false;
                            }else{
                                data.customer_channel = $('#lr_customer_channel_id option:selected').html();
                            }
                        }
                        // 推广编号
                        if ( $('#lr_channel_plan_number').val() ) { data.channel_plan_number = $('#lr_channel_plan_number').val() };
                        if(isInArray('channel_plan_number',jslrMust)){
                            if (!data.channel_plan_number) {
                                layer.alert("推广编号不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 广告落地页
                        if ( $('#lr_ad_page').val() ) { data.ad_page = $('#lr_ad_page').val() };
                        // 获取方式
                        if ( $('#lr_access_model_id').val() ) {
                            data.access_model_id= $('#lr_access_model_id').val();
                            data.access_model = $('#lr_access_model_id option:selected').html();
                        };
                        if(isInArray('access_model_id',jslrMust)){
                            if (!data.lr_access_model_id) {
                                layer.alert("获取方式不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 消费类型
                        if ( $('#lr_consultation_type_id').val() ) {
                            data.consultation_type_id= $('#lr_consultation_type_id').val();
                            data.consultation_type   = $('#lr_consultation_type_id option:selected').html();
                        };
                        if(isInArray('consultation_type_id',jslrMust)){
                            if (!data.lr_consultation_type_id) {
                                layer.alert("消费类型不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 意向套系
                        if ( $('#package').val() ) {
                            data.package_name = $('#package').val();
                        };
                        // 客户来源
                        if ( $('#lr_customer_cource_id').val() ) {
                            data.customer_cource_id= $('#lr_customer_cource_id').val();
                            data.customer_cource   = $('#lr_customer_cource_id option:selected').html();
                        };
                        if(isInArray('customer_cource_id',jslrMust)){
                            if (!data.lr_customer_cource_id) {
                                layer.alert("客户来源不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 客资标记
                        if ( $('#lr_sign_id').val() ) {
                            data.sign_id  = $('#lr_sign_id').val();
                            data.sign_name= $('#lr_sign_id option:selected').html();
                        };
                        if(isInArray('sign_id',jslrMust)){
                            if (!data.sign_id) {
                                layer.alert("客资标记不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 推广人
                        if ( $('#lr_recommend_staff_id').val() ) {
                            data.recommend_staff_id= $('#lr_recommend_staff_id').val();
                            data.recommend_staff   = $('#lr_recommend_staff_id option:selected').html();
                        };
                        // 意向店面
                        if ( $('#lr_consulation_shop_code').val() ) {
                            data.consulation_shop_code= $('#lr_consulation_shop_code').val();
                            data.consulation_shop     = $('#lr_consulation_shop_code option:selected').html();
                        };
                        if(isInArray('consulation_shop',jslrMust)){
                            if (!data.consulation_shop_code) {
                                layer.alert("客资标记不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 客资状态
                        if ( $('#lr_is_valid').val() ) { data.is_valid = $('#lr_is_valid').val() };
                        if(isInArray('is_valid',jslrMust)){
                            if (!data.is_valid) {
                                layer.alert("客资状态不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 出生医院
                        if ( $('#lr_baby_address').val() ) { data.baby_address = $('#lr_baby_address').val() };
                        if(isInArray('baby_address',jslrMust)){
                            if (!data.baby_address) {
                                layer.alert("出生医院不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 医拍时间
                        if ( $('#lr_hospital_photo_time').val() ) { data.hospital_photo_time = $('#lr_hospital_photo_time').val() };
                        // 医拍摄影师
                        if ( $('#lr_hospital_photo_cameraman_id').val() ) {
                            data.hospital_photo_cameraman_id= $('#lr_hospital_photo_cameraman_id').val();
                            data.hospital_photo_cameraman   = $('#lr_hospital_photo_cameraman_id option:selected').html();
                        };
                        // 医拍助理
                        if ( $('#lr_hospital_photo_assistant_id').val() ) {
                            data.hospital_photo_assistant_id= $('#lr_hospital_photo_assistant_id').val();
                            data.hospital_photo_assistant   = $('#lr_hospital_photo_assistant_id option:selected').html();
                        };
                        // 入住会所
                        if ( $('#lr_club_id').val() ) {
                            data.club_id  = $('#lr_club_id').val();
                            data.club_name= $('#lr_club_id option:selected').html();
                        };
                        if(isInArray('club_id',jslrMust)){
                            if (!data.club_id) {
                                layer.alert("入住会所不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 关键字
                        if ( $('#lr_customer_key').val() ) { data.customer_key = $('#lr_customer_key').val() };
                        // 预算
                        if ( $('#lr_budget').val() ) { data.budget = $('#lr_budget').val() }; 
                        // 小阿福定制 网络部 品类 
                        if ( $('#pinlei').val() ) { data.pinlei = $('#pinlei').val() }; 
                        // 客户地址
                        if ( $('#lr_province_id').val() ) {
                            data.province_id= $('#lr_province_id').val();
                            data.province   = $('#lr_province_id option:selected').html();
                        };
                        if ( $('#lr_city_id').val() ) {
                            data.city_id= $('#lr_city_id').val();
                            data.city   = $('#lr_city_id option:selected').html();
                        };
                        if ( $('#lr_county_id').val() ) {
                            data.county_id= $('#lr_county_id').val();
                            data.county   = $('#lr_county_id option:selected').html();
                        };
                        if ( $('#lr_customer_address').val() ) { data.customer_address = $('#lr_customer_address').val() };
                        if(isInArray('customer_address',jslrMust)){
                            if (!data.province_id || !data.city_id || !data.county_id || !data.customer_address) {
                                layer.alert("客户地址(省市区及详细地址)不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 沟通记录
                        if ( $("#lr_img_record iframe").contents().children().find('.view').html() ) { 
                            data.img_record = $("#lr_img_record iframe").contents().children().find('.view').html() 
                        };
                        // 爸爸信息
                        if ( $('#lr_mate_name').val() ) { data.mate_name = $('#lr_mate_name').val() };
                        if ( $('#lr_mate_tel').val() ) { data.mate_tel = $('#lr_mate_tel').val() };
                        if ( $('#lr_mate_wechat').val() ) { data.mate_wechat = $('#lr_mate_wechat').val() };
                        if ( $('#lr_mate_birthday').val() ) { data.mate_birthday = $('#lr_mate_birthday').val() };
                        if ( $('#lr_mate_idtype').val() ) { data.mate_idtype = $('#lr_mate_idtype').val() };
                        if ( $('#lr_mate_idnumber').val() ) { data.mate_idnumber = $('#lr_mate_idnumber').val() };
                        if($('#lr_b_id').val()){
                            data.b_id = $('#lr_b_id').val();
                            data.b_name     = $('#lr_b_id option:selected').html();
                        }
                        // 
                        // console.log(data);return false;
                        // 提交客资
                        $.ajax({
                            type: "POST",
                            // url: 'addCustomerNew',
                            url: '/crm/customer/addCustomer',
                            data: data,
                            success: function (msg) {
                                layer.close(shade);
                                //alert(msg.data.all_crm);console.log(msg.data.all_crm);
                                if (msg.code == 1) {
                                    if(closewindow){
                                        layer.closeAll();
                                    }else{
                                        initAddFormInput();
                                        layer.msg('添加成功，请继续添加。');
                                    }
                                    
                                    if(msg.is_repeat == 1 && msg.save == 1){
                                        layer.alert('此客资有重复，已更新旧客资。');
                                    }
                                    setTimeout(
                                        function(){
                                            if(typeof customer_list_active == 'object'){
                                                customer_list_active.refresh();
                                            }
                                        }
                                        ,1000
                                    )
                                } else {
                                    layer.alert(msg.msg);
                                    layer.close(shade);
                                    return false;
                                }
                            },
                            error:function(){
                                layer.alert('客资录入失败');
                                layer.close(shade);
                                return false;
                            }
                        });
                    },
                    success: function(layero, index){
                        initAddFormInput();
                    }
                });
            },10);            
        }
        var initAddFormInput = function(){
            // 置空客基本信息
            $('#lr_baby_name').val('');
            $('#lr_baby_sex').val('');
            $('#lr_baby_stature').val('');
            $('#lr_baby_weight').val('');
            $('#lr_baby_birthday').val('');
            $('#lr_ready_baby_day').val('');
            $('#lr_customer_name').val('');
            $('#lr_customer_tel').val('');
            $('#lr_customer_wechat').val('');
            $('#lr_customer_qq').val('');
            $('#lr_customer_weibo').val('');
            $('#lr_customer_birthday').val('');
            $('#lr_customer_idtype').val('');
            $('#lr_customer_idnumber').val('');
            $('#autoWrite').val('');
            // console.log(recordDayToday);
            if (!recordDayToday) {
                $('#lr_record_day').val('');
            };

            $('#lr_home_phone').val('');
            $('#lr_email').val('');
            $('#lr_channel_plan_number').val('');
            $('#lr_ad_page').val('');
            $('#lr_customer_key').val('');
            $('#lr_budget').val('');
            $('#lr_customer_address').val('');
            $('#mTel_belong').val('');
            $('#lr_hospital_photo_time').val('');

            $('#lr_mate_name').val('');
            $('#lr_mate_tel').val('');
            $('#lr_mate_wechat').val('');
            $('#lr_mate_birthday').val('');
            $('#lr_mate_idtype').val('');
            $('#lr_mate_idnumber').val('');
            // 转介绍搜索框
            $('#search_content').val('');
            $('#lr_introducer_name').val('');
            $('#lr_introducer_ordernum').val('');
            $('#lr_introducer_tel').val('');
            // ajax 获取 组织架构相关数据 登记店面、部门、小组、人 追踪店面、部门、小组、人员
            /*
            $("#lr_add_shop_code").html('');
            $("#lr_dj_department_id").html('');
            $("#lr_dj_staff_id").html('');
            $("#lr_shop_code").html('');
            $("#lr_sales_department_id").html('');
            $("#lr_sales_staff_id").html('');
            $("#package_name").html('');
            // 获取次数 一胎、二胎
            /**
            var baby_time_op = document.getElementById("lr_baby_time");
            // console.log(baby_time_op);
            if (baby_time_op) {
                for(var i=0; i<baby_time_op.options.length; i++){
                    if(baby_time_op.options[i].value == '一胎'){
                        baby_time_op.options[i].selected = true;
                        break; 
                    }
                }
            };*/

            $("#lr_baby_time").val('一胎');


            //2021-06-11 定下 每次录入客资时 清除上一次数据项，剩下的不清除
            //意向套系
            $("#package").val('');

            layui.form.render();
        }
        $("#add_new_child_customer .layui-tab-title li").click(function(){
            var aty = $(this).text();
            console.log(aty);
            if (aty == "转介绍") {
                // 转介绍tab 显示 转介绍信息
                $(".zjs_use").show();
                // 转介绍tab 隐藏 渠道信息 除了 babypastel
                if (babypastel != 1) {
                    $(".qudao_use").hide();
                };
                $('#customer_type').val('2');
            }else if(aty == "新客资"){
                // 新客资tab 隐藏转介绍信息
                $(".zjs_use").hide();
                // 新客资tab 显示渠道信息
                $(".qudao_use").show();
                $('#customer_type').val('1');
            };
        })

        //转介绍类型监听
        form.on('select(introducer)', function (data) {
            var zjs_from = data.value;
            if(zjs_from == 1){
                $('.introducer').css('display','inline-block');
            }else{
                $('.introducer').css('display','none');
            }
        })

        //单控件
        // lay('.test-item').each(function () {
        //     laydate.render({
        //         elem: this
        //         , format: 'yyyy-MM-dd'
        //         , value: ''
        //         , trigger: 'click'
        //     });
        // });
        // 获取日期
        laydate.render({
            elem: '#create_time'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#create_time").val()
            ,type: 'datetime'
            , trigger: 'click'
        });
        // 预拍日期
        laydate.render({
            elem: '#yp_day'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#yp_day").val()
            , trigger: 'click'
        });
        // 客户生日
        laydate.render({
            elem: '#customer_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#customer_birthday").val()
            , trigger: 'click'

        });
        // 配偶生日
        laydate.render({
            elem: '#mate_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#mate_birthday").val()
            , trigger: 'click'
        });
        // 标记日期
        laydate.render({
            elem: '#op_signday'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#op_signday").val()
            , trigger: 'click'
        });
        // 登记日期
        laydate.render({
            elem: '#dj_day'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#dj_day").val()
            , trigger: 'click'
        });
        // 邀约日期
        laydate.render({
            elem: '#yjd_day'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#yjd_day").val()
            , trigger: 'click'
        });
        // 订单日期
        laydate.render({
            elem: '#order_day'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#order_day").val()
            , trigger: 'click'
        });
        // 进店日期
        laydate.render({
            elem: '#syjd_day'
            , format: 'yyyy-MM-dd H:m:s'
            , value: $("#syjd_day").val()
            , trigger: 'click'
        });
        // 结婚日期
        laydate.render({
            elem: '#marry_day'
            , format: 'yyyy-MM-dd'
            , value: $("#marry_day").val()
            , trigger: 'click'
        });
        // 预产日期
        // laydate.render({
        //     elem: '#ready_baby_day'
        //     , format: 'yyyy-MM-dd'
        //     , value: $("#ready_baby_day").val()
        //     , trigger: 'click'
        // });
        // 宝宝生日
        laydate.render({
            elem: '#baby_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#baby_birthday").val()
            , trigger: 'click'
        });

        lay('.hour-item').each(function () {
            laydate.render({
                elem: this
                , type: 'time'
                , done: function (value, date, endDate) {
                    console.log(value, date, endDate);
                }
                , change: function (value, date, endDate) {
                    //this.elem.val(value)
                }
            });
        });
    });
    function trim(str){
        if (str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }else{
            return '';
        }
    }
    //弹窗
    layui.use(['layer','layedit'], function () {
        var $ = layui.jquery, layer = layui.layer,layedit = layui.layedit;//独立版的layer无需执行这一句

        // 介绍人搜索
        $(".tk_jsr_search").click (function function_name () {
            $.ajax({
                type: "POST",
                url: '/crm/Customer/searchCustomers',
                data: {customer_name:$('#search_content').val()},
                success: function (msg) {
                    if (msg.Code == 200) {
                        if (msg.Data.length > 0) {
                            var jsrPop=layer.open({
                                type: 1,
                                title: '介绍人搜索',
                                skin: 'layui-layer-rim',
                                area: ['420px', '480px'],
                                content: $('#jieshao_intro'),
                                btn:'关闭',
                                success:function(index){
                                    var a = msg.Data;
                                    var crmItem = '';
                                    for(var i=0;i< a.length;i++){
                                        crmItem +='<div class="jieshao" onclick="testlick(this);"><p class="old_order_item" name="s" cus_name="'+a[i]['customer_name']+'">姓名：'+a[i]['customer_name']+'</p><p cus_tel="'+a[i]['customer_tel']+'">手机号：'+a[i]['customer_tel']+'</p><p cus_order="'+a[i]['order_number']+'">订单编号：'+a[i]['order_number']+'</p></div>';
                                    }
                                    $("#jieshao_intro").html(crmItem);
                                    $("body").on("click",".jieshao",function(){
                                        layer.close(jsrPop);
                                    })
                                },
                            });
                        }else{
                            layer.alert('未搜索到对应数据！');
                            return false;
                        }
                    }else{
                        layer.alert('未搜索到对应数据！');
                        return false;
                    }
                }
            })
        });

        $('.menu_addb').on('click', function () {
            showAddCustomerForm(0);
        });
    });

    //省市县
    layui.use('form', function(){
        var form = layui.form;
        form.on('select(province)', function(data){
            var province = data.value;
            if(province){
                $.ajax({
                    type: "POST",
                    url: 'searchCity',
                    data: "pid="+province,
                    success: function (msg) {
                        var replace = '<select name="city_id" id="city_id" lay-filter="city" class="cus-input"><option value="">请选择</option>';
                        for(var i=0;i<msg.length;i++){
                            replace += '<option value="'+msg[i]['id']+'">'+msg[i]['name']+'</option>';
                        }
                        replace +='</select>';
                        $(".city").html(replace);
                        var county='<select name="county_id" id="county_id" lay-filter="county" class="cus-input"><option value="">请选择</option></select>';
                        $(".county").html(county);
                        form.render();
                    }
                });
            }else{
                var replace = '<select name="city_id" id="city_id" lay-filter="city" class="cus-input"><option value="">请选择</option></select>';
                $(".city").html(replace);
                var county = '<select name="county_id" id="county_id" lay-filter="county" class="cus-input"><option value="">请选择</option></select>';
                $(".county").html(county);
                form.render();
            }

        });

        form.on('select(city)', function(data){
            var city = data.value;
            if(city){
                $.ajax({
                    type: "POST",
                    url: 'searchCity',
                    data: "pid="+city,
                    success: function (msg) {

                        var replace = '<select name="county_id" id="county_id" lay-filter="county" class="cus-input"><option value="">请选择</option>';
                        for(var i=0;i<msg.length;i++){
                            replace += '<option value="'+msg[i]['id']+'">'+msg[i]['name']+'</option>';
                        }
                        replace +='</select>';
                        $(".county").html(replace);
                        form.render();

                    }
                });
            }else{
                var replace = '<select name="county_id" id="county_id" lay-filter="county" class="cus-input"><option value="">请选择</option></select>';
                $(".county").html(replace);
                form.render();
            }

        });

        // 客资分配，部门--->员工选择，联动
        form.on('select(department)', function(data){
            var department_name = data.value;
            $.ajax({
                type: "POST",
                url: 'getDepartmentStaffs',
                data: "department_name="+department_name,
                success: function (msg) {
                    var replace = '<select name="staff_id" id="staff_id' +
                        '" lay-filter="staff" class="cus-input" ><option value="">请选择员工</option>';
                    for(var i=0;i<msg.length;i++){
                        replace += '<option value="'+msg[i]['id']+'">'+msg[i]['staffname']+'</option>';
                    }
                    replace +='</select>';
                    $(".staff").html(replace);
                    form.render();
                }
            });
        });
        // 客资详情、编辑 地区三级联动
        form.on('select(provinceI)', function(data){
            var province = data.value;
            $.ajax({
                type: "POST",
                url: '/crm/Customer/searchCity',
                data: "pid="+province,
                success: function (msg) {

                    var replace = '<select name="city_id" id="city_id" lay-filter="cityI" class="cus-input"><option value="">请选择</option>';
                    for(var i=0;i<msg.length;i++){
                        replace += '<option value="'+msg[i]['id']+'">'+msg[i]['name']+'</option>';
                    }
                    replace +='</select>';
                    // alert(replace);
                    $(".cityI").html(replace);
                    var county='<select name="county_id" id="county_id" lay-filter="countyI" class="cus-input"><option value="">请选择</option></select>';
                    $(".countyI").html(county);
                    form.render();

                }
            });
        });

        form.on('select(cityI)', function(data){
            var city = data.value;
            $.ajax({
                type: "POST",
                url: '/crm/Customer/searchCity',
                data: "pid="+city,
                success: function (msg) {

                    var replace = '<select name="county_id" id="county_id" lay-filter="countyI" class="cus-input"><option value="">请选择</option>';
                    for(var i=0;i<msg.length;i++){
                        replace += '<option value="'+msg[i]['id']+'">'+msg[i]['name']+'</option>';
                    }
                    replace +='</select>';
                    $(".countyI").html(replace);
                    form.render();

                }
            });
        });


    });

    //选项卡
    layui.use('element', function () {
        var element = layui.element;
    });


    //    滚动条问题
    $(".layui-layer-content").scroll(function(){
        $("body").preventDefault();
    })
});


/* 
**手机号 判断 
**需加载layer ，弹框用的layer弹框
**参数：tel_id 手机tel所在的id值 ;
*/
function check_tel_id_11(tel_id){
    var tel = $('#'+tel_id).val();
    if (tel.length > 0) {
        if (tel.length != 11) {
            layer.alert("手机号长度不对");
            return false;
        };
        var patrn = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
        if(!patrn.exec(tel)){
            layer.alert("手机号输入错误");
            return false;
        }
    };
}
