var showAddCustomerForm ;
$(function () {
    //表单
    // layui.use('form', function () {
    //     var form = layui.form;
    // });
    //日历
    layui.use(['laydate','form','layer'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var layer = layui.layer;
        //单控件
        // 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()
            ,type: 'datetime'
            , 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)
                }
            });
        });


        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_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;
                        // 登记人信息
                        //2021-06-24 设置了隐藏登记人 隐藏 登记店面，登记部门，登记人 ，取值当前登录人信息
                        if ($("#lr_Hidden_dj_staff_id").val() == 1) {
                            data.add_shop_code = $("#lr_h_add_shop_code").val();
                            data.add_shop_name = $("#lr_h_add_shop_name").val();
                            data.dj_department_id = $("#lr_h_dj_department_id").val();
                            data.dj_department_name = $("#lr_h_dj_department_name").val();
                            data.dj_staff_id = $("#lr_h_dj_staff_id").val();
                            data.dj_staff = $("#lr_h_dj_staff_name").val();
                        }else{
                            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;
                            }
                        }
                        data.customer_sex = $("#form_new #customer_sex").val();
                        if(isInArray('customer_sex',jslrMust)){
                            if (!data.customer_sex ) {
                                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_create_time').val() ) { data.create_time = $('#lr_create_time').val() };
                        // 结婚日期 预计婚期
                        if ( $('#lr_marry_day').val() ) { data.marry_day = $('#lr_marry_day').val() };
                        if ( $('#lr_ready_marry_day').val() ) { data.ready_marry_day = $('#lr_ready_marry_day').val() };

                        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 (data.customer_type ==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',jslrMust)){
                            if (!data.consultation_type) {
                                layer.alert("消费类型不能为空");
                                layer.close(shade);
                                return false;
                            }
                        }
                        // 客户来源
                        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_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_customer_key').val() ) { data.customer_key = $('#lr_customer_key').val() };
                        // 预算
                        if ( $('#lr_budget').val() ) { data.budget = $('#lr_budget').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() };
                        // 
                        // 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_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('');

            $('#lr_channel_plan_number').val('');
            $('#lr_ad_page').val('');
            $('#lr_customer_key').val('');
            $('#lr_budget').val('');
            $('#lr_customer_address').val('');
            $('#mTel_belong').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('');*/
            
            layui.form.render();
        }

        $("#add_new_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');
            }
        })

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

        // $('.cus-btn').on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        //     $(this).addClass("active").siblings(".cus-model-btn").removeClass("active");
        //     if (type == "allot") {
        //         $("#allot-box").parent().css("overflow", "inherit");
        //     }
        // });


            // $.ajax({
            //     type: "POST",
            //     url: 'getLrSystemStaffs',
            //     data: {},
            //     success: function(msg){
            //         // console.log('msg==>'+msg);
            //         if (msg.code==200) {
            //             var lrSystemStaffs = msg.data;
            //             // 登记店面
            //             if (lrSystemStaffs.djShops) {
            //                 var djShops = lrSystemStaffs.djShops;
            //                 var djShopsStr = "";
            //                 djShopsStr+="<option value=''>请选择登记店面</option>";
            //                 for (var i = 0; i < djShops.length; i++) {
            //                     if (djShops[i].selected==1) {
            //                         djShopsStr+= "<option value='"+djShops[i].shop_code+"' selected >"+djShops[i].shop_name+"</option>";
            //                     }else{
            //                         djShopsStr+= "<option value='"+djShops[i].shop_code+"' >"+djShops[i].shop_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_add_shop_code").html(djShopsStr);
            //             };
            //             // 登记部门
            //             if (lrSystemStaffs.djDepartments) {
            //                 var djDepartments = lrSystemStaffs.djDepartments;
            //                 var djDepartmentsStr = "";
            //                 djDepartmentsStr+="<option value=''>请选择登记部门</option>";
            //                 for (var i = 0; i < djDepartments.length; i++) {
            //                     if (djDepartments[i].selected==1) {
            //                         djDepartmentsStr+= "<option value='"+djDepartments[i].department_id+"' selected >"+djDepartments[i].department_name+"</option>";
            //                     }else{
            //                         djDepartmentsStr+= "<option value='"+djDepartments[i].department_id+"' >"+djDepartments[i].department_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_dj_department_id").html(djDepartmentsStr);
            //             };
            //             // 登记人
            //             if (lrSystemStaffs.djStaffs) {
            //                 var djStaffs = lrSystemStaffs.djStaffs;
            //                 var djStaffsStr = "";
            //                 djStaffsStr+="<option value=''>请选择登记人</option>";
            //                 for (var i = 0; i < djStaffs.length; i++) {
            //                     if (djStaffs[i].selected==1) {
            //                         djStaffsStr+= "<option value='"+djStaffs[i].staff_id+"' selected >"+djStaffs[i].staff_name+"</option>";
            //                     }else{
            //                         djStaffsStr+= "<option value='"+djStaffs[i].staff_id+"' >"+djStaffs[i].staff_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_dj_staff_id").html(djStaffsStr);
            //             };
            //             // 追踪店面
            //             if (lrSystemStaffs.shopCode) {
            //                 var shopCode = lrSystemStaffs.shopCode;
            //                 var shopCodeStr = "";
            //                 shopCodeStr+="<option value=''>请选择追踪店面</option>";
            //                 for (var i = 0; i < shopCode.length; i++) {
            //                     if (shopCode[i].selected==1) {
            //                         shopCodeStr+= "<option value='"+shopCode[i].shop_code+"' selected >"+shopCode[i].shop_name+"</option>";
            //                     }else{
            //                         shopCodeStr+= "<option value='"+shopCode[i].shop_code+"' >"+shopCode[i].shop_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_shop_code").html(shopCodeStr);
            //             };
            //             // 追踪部门
            //             if (lrSystemStaffs.salesDepartment) {
            //                 var salesDepartment = lrSystemStaffs.salesDepartment;
            //                 var salesDepartmentStr = "";
            //                 salesDepartmentStr+="<option value=''>请选择追踪部门</option>";
            //                 for (var i = 0; i < salesDepartment.length; i++) {
            //                     if (salesDepartment[i].selected==1) {
            //                         salesDepartmentStr+= "<option value='"+salesDepartment[i].department_id+"' selected >"+salesDepartment[i].department_name+"</option>";
            //                     }else{
            //                         salesDepartmentStr+= "<option value='"+salesDepartment[i].department_id+"' >"+salesDepartment[i].department_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_sales_department_id").html(salesDepartmentStr);
            //             };
            //             // 追踪人
            //             if (lrSystemStaffs.salesStaffs) {
            //                 var salesStaffs = lrSystemStaffs.salesStaffs;
            //                 var salesStaffsStr = "";
            //                 salesStaffsStr+="<option value=''>请选择追踪人</option>";
            //                 for (var i = 0; i < salesStaffs.length; i++) {
            //                     if (salesStaffs[i].selected==1) {
            //                         salesStaffsStr+= "<option value='"+salesStaffs[i].staff_id+"' selected >"+salesStaffs[i].staff_name+"</option>";
            //                     }else{
            //                         salesStaffsStr+= "<option value='"+salesStaffs[i].staff_id+"' >"+salesStaffs[i].staff_name+"</option>";
            //                     }
            //                 }
            //                 $("#lr_sales_staff_id").html(salesStaffsStr);
            //             };
            //             // 意向套系
            //             if (lrSystemStaffs.package) {
            //                 var package = lrSystemStaffs.package;
            //                 var packageStr = "";
            //                 packageStr+="<option value=''>请选择套系</option>";
            //                 for (var i = 0; i < package.length; i++) {
            //                     if (package[i].selected==1) {
            //                         packageStr+= "<option value='"+package[i].package_name+"' selected >"+package[i].package_name+"</option>";
            //                     }else{
            //                         packageStr+= "<option value='"+package[i].package_name+"' >"+package[i].package_name+"</option>";
            //                     }
            //                 }
            //                 $("#package").html(packageStr);
            //             }
            //         }
            //         layui.form.render();
            //     }
            // });
            // // selectType 是在打开列表页的时候，后台控制器返回的，在列表页头部赋值
            // // console.log(selectType);
            // $.ajax({
            //     type: "POST",
            //     // url: 'getSelectData',
            //     url: 'getcustomeraddselect',
            //     data: {selectType:selectType},
            //     success: function(msg){
            //         if (msg.code==200) {
            //             var selectData = msg.data;
            //             var channelStr ="<option value=''>请选择渠道名称</option>";
            //             // 三级渠道--通过二级渠道下拉获取
            //             if (selectData.channel) {
            //                 var channel = selectData.channel;
            //                 var channelStr = "";
            //                 channelStr+="<option value=''>请选择渠道名称</option>";
            //                 for (var i = 0; i < channel.length; i++) {
            //                     channelStr+= "<option value='"+channel[i].id+"' sign='"+channel[i].sign_id+"'>"+channel[i].channel_name+"</option>";
            //                 }
            //                 $("#lr_customer_channel_id").html(channelStr);
            //             };
            //             // 获取方式
            //             if (selectData.accessModel) {
            //                 var accessModel = selectData.accessModel;
            //                 var accessModelStr = "";
            //                 accessModelStr+="<option value=''>请选择获取方式</option>";
            //                 for (var i=0; i< accessModel.length; i++){
            //                     accessModelStr += "<option value="+accessModel[i].id+">"+accessModel[i].accessmodel+"</option>";
            //                 }
            //                 $('#lr_access_model_id').html(accessModelStr);
            //             };
            //             // 消费类型
            //             if (selectData.consumptionType) {
            //                 var consumptionType = selectData.consumptionType;
            //                 var consumptionTypeStr = "";
            //                 consumptionTypeStr+="<option value=''>请选择消费类型</option>";
            //                 for (var i=0; i< consumptionType.length; i++){
            //                     consumptionTypeStr += "<option value="+consumptionType[i].id+">"+consumptionType[i].consumption_name+"</option>";
            //                 }
            //                 $('#lr_consultation_type_id').html(consumptionTypeStr);
            //             };
            //             // 客户意向
            //             if (selectData.intention) {
            //                 var intention = selectData.intention;
            //                 var intentionStr = "";
            //                 intentionStr+="<option value=''>请选择客户意向</option>";
            //                 for (var i=0; i< intention.length; i++){
            //                     intentionStr += "<option value="+intention[i].id+">"+intention[i].intention_name+"</option>";
            //                 }
            //                 $('#lr_customer_intention_id').html(intentionStr);
            //             };
            //             // 客户来源
            //             if (selectData.source) {
            //                 var source = selectData.source;
            //                 var sourceStr = "";
            //                 sourceStr+="<option value=''>请选择客户来源</option>";
            //                 for (var i=0; i< source.length; i++){
            //                     sourceStr += "<option value="+source[i].id+">"+source[i].cus_name+"</option>";
            //                 }
            //                 $('#lr_customer_cource_id').html(sourceStr);
            //             };
            //             // 客资标记
            //             if (selectData.tracksign) {
            //                 var tracksign = selectData.tracksign;
            //                 var tracksignStr = "";
            //                 tracksignStr+="<option value=''>请选择客资标记</option>";
            //                 for (var i=0; i< tracksign.length; i++){
            //                     tracksignStr += "<option value="+tracksign[i].id+">"+tracksign[i].tracksign+"</option>";
            //                 }
            //                 $('#lr_sign_id').html(tracksignStr);
            //             };
            //             // 意向店面
            //             if (selectData.shops) {
            //                 var shops = selectData.shops;
            //                 var shopStr = "";
            //                 shopStr+="<option value=''>请选择意向店面</option>";
            //                 for (var i=0; i< shops.length; i++){
            //                     shopStr += "<option value="+shops[i].shop_code+">"+shops[i].shop_name+"</option>";
            //                 }
            //                 $('#lr_consulation_shop_code').html(shopStr);
            //             };
            //         }
            //         layui.form.render();
            //     }
            // });
        

    });
    function trim(str){
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }

    //日历
    layui.use('laydate', function () {
        var now = new Date(); //当前日期
        var date1 = new Date(),
            time1=date1.getFullYear()+"-"+(date1.getMonth()+1)+"-"+date1.getDate();//time1表示当前时间
        var date2 = new Date(date1);
        date2.setDate(date1.getDate()-7);
        var time2 = date2.getFullYear()+"-"+(date2.getMonth()+1)+"-"+date2.getDate();
        var nowM = date1.getMonth()+1;
        if (nowM < 10) {
            nowM = "0" + nowM;
        }
        var startMonth = date2.getFullYear()+"-"+nowM+"-01";
        //var thisMonth = date2.getFullYear()+"-"+
        var laydate = layui.laydate;
        //单控件
        // 开始时间
        laydate.render({
            elem: '#start_time'
            , value: $("#start_time").val()
            , trigger: 'click'
            , type:'datetime'
            , format:'yyyy-MM-dd HH:mm'
            // , btns: ['clear', 'now', 'confirm']
        });
        // 结束时间
        laydate.render({
            elem: '#end_time'
            , value: $("#end_time").val()
            , trigger: 'click'
            , type:'datetime'
            , format:'yyyy-MM-dd HH:mm'
            // , btns: ['clear', 'now', 'confirm']
        });
        // 获取日期
        laydate.render({
            elem: '#lr_create_time'
            , format: 'yyyy-MM-dd HH:mm:ss'
            , value: $("#lr_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: '#lr_customer_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#lr_customer_birthday").val()
            , trigger: 'click'
        });
        // 配偶生日
        laydate.render({
            elem: '#mate_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#mate_birthday").val()
            , trigger: 'click'
        });
        // 配偶生日
        laydate.render({
            elem: '#lr_mate_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#lr_mate_birthday").val()
            , trigger: 'click'
        });
        // 标记日期
        laydate.render({
            elem: '#op_signday'
            , format: 'yyyy-MM-dd H:m:s'
            ,type:'datetime'
            , value: $("#op_signday").val()
        });
        // 标记日期
        laydate.render({
            elem: '#op_signday_aaa'
            , format: 'yyyy-MM-dd H:m:s'
            ,min: -7
            ,max:7
            ,type:'datetime'
            , value: $("#op_signday").val()
        });
        // 登记日期
        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()
            , range: true
            , trigger: 'click'
        });
        // 邀约日期
        laydate.render({
            elem: '#zz_yjd_day'
            , format: 'yyyy-MM-dd H:m:s'
            ,type:'datetime'
            , trigger: 'click'
        });
        // 订单日期
        laydate.render({
            elem: '#order_day'
            , format: 'yyyy-MM-dd'
            , 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: '#lr_marry_day'
            , format: 'yyyy-MM-dd'
            , value: $("#lr_marry_day").val()
            , trigger: 'click'
        });
        // 预产日期
        laydate.render({
            elem: '#ready_baby_day'
            , format: 'yyyy-MM-dd'
            ,type:'datetime'
            , value: $("#ready_baby_day").val()
            , trigger: 'click'
        });
        // 宝宝生日
        laydate.render({
            elem: '#baby_birthday'
            , format: 'yyyy-MM-dd'
            , value: $("#baby_birthday").val()
            , trigger: 'click'
        });
        // 开始时间
        if(yearCus == 1){
            laydate.render({
                elem: '#start_time'
                , value: $("#start_time").val()
                , trigger: 'click'
                , type:'datetime'
                , format:'yyyy-MM-dd HH:mm'
                , min:-365
                // , btns: ['clear', 'now', 'confirm']
            });
            // 结束时间
            laydate.render({
                elem: '#end_time'
                , value: $("#end_time").val()
                , trigger: 'click'
                , type:'datetime'
                , format:'yyyy-MM-dd HH:mm'
                , min:-365
                // , btns: ['clear', 'now', 'confirm']
            });
        }else{
            laydate.render({
                elem: '#start_time'
                , value: $("#start_time").val()
                , trigger: 'click'
                , type:'datetime'
                , format:'yyyy-MM-dd HH:mm'
                // , btns: ['clear', 'now', 'confirm']
            });
            // 结束时间
            laydate.render({
                elem: '#end_time'
                , value: $("#end_time").val()
                , trigger: 'click'
                , type:'datetime'
                , format:'yyyy-MM-dd HH:mm'
                // , btns: ['clear', 'now', 'confirm']
            });
        }
        // 加好友时间
        laydate.render({
            elem: '#df_add_friend_time'
            ,format: 'yyyy-MM-dd H:m:s'
            ,type:'datetime'
            ,trigger: 'click'
        });
        // 婚期时间
        laydate.render({
            elem: '#df_marry_day'
            ,format: 'yyyy-MM-dd'
            ,type:'date'
            ,trigger: 'click'
        });
        // 再约
        laydate.render({
            elem: '#df_next_time'
            ,format: 'yyyy-MM-dd HH:mm:ss'
            ,type:'datetime'
            ,trigger: 'click'
            ,min: "{$now_time}"
        });

        // 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)
        //         }
        //     });
        // });
    });

    // 介绍人搜索
    $('.search_introducer').on('click',function () {

    // function jieshao_search() {
        $.ajax({
            type: "POST",
            url: '/crm/Customer/searchCustomers',
            data: {customer_name:$('#search_content').val()},
            success: function (msg) {
                if (msg.Code == 200) {

                    var jsrPop=layer.open({
                        type: 1,
                        title: '介绍人搜索',
                        skin: 'layui-layer-rim',
                        area: ['420px', '90%'],
                        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);
                            })
                        },
                        //yes:function(index) {
                        //    layer.close(index);
                        //    //$(".layui-layer-content iframe").contents().children().find("#jieshao_intro").html(crmItem);
                        //},
                        //cancel:function(index) {
                        //    layer.close(index);
                        //},
                    });
                }else{
                    layer.alert('未搜索到对应数据！');
                    return false;
                }
            }
        })
    })

    //省市县
    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();
    })
});
