{layout name="layout1" /}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
        <div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
            <div class="layui-colla-item">
                <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
                <div class="layui-colla-content layui-show">
                    <p>*会员列表，对会员进行编辑，账户调整，等级调整和资料查看等操作。</p>
                </div>
            </div>
        </div>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客户姓名:</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" placeholder="请输入客户姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户电话:</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" id="phone" placeholder="请输入用户电话" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">跟进人员：</label>
                    <div class="layui-input-inline">
                        <select id="area_id" name="admin_id"   style="height:80px;width: 80px" >
                            <option value="" selected>全部</option>
                                {foreach $admin as $item }
                                    <option value="{$item.id}">{$item.name}</option>
                                 {/foreach}
                            
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">来源渠道：</label>
                    <div class="layui-input-inline">
                        <select id="chann_id" name="chann_id"   style="height:80px;width: 80px" >
                            <option value="" selected>请选择客户的来源渠道</option>
                                {foreach $chann as $item }
                                    <option value="{$item.id}">{$item.name}</option>
                                 {/foreach}
                            
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">客户标签：</label>
                    <div class="layui-input-inline">
                        <select id="label_id" name="label_id"   style="height:80px;width: 80px" >
                            <option value="" selected>请选择客户标签</option>
                                {foreach $label_id as $item }
                                    <option value="{$item.id}">{$item.name}</option>
                                 {/foreach}
                            
                        </select>
                    </div>
                </div>


            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客户地址:</label>
                    <div class="layui-input-block">
                        <input type="text" name="adder" id="adder" placeholder="请输入客户的地址" autocomplete="off" class="layui-input">
                    </div>
                </div>                
                
                <div class="layui-inline">
                    <label class="layui-form-label">用户地区：</label>
                    <div class="layui-input-inline">
                        <select id="area_id" name="area_id"   style="height:80px;width: 80px" >
                            <option value="" selected>全部</option>
                            <option value="520102">南明区</option>
                            <option value="520103">云岩区</option>
                            <option value="520111">花溪区</option>
                            <option value="520112">乌当区</option>
                            <option value="520113">白云区</option>
                            <option value="520115">观山湖区</option>
                            <option value="520122">息烽县</option>
                            <option value="520123">修文县</option>
                            <option value="520181">清镇市</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">消费年份：</label>
                    <div class="layui-input-inline">
                        <select id="year" name="year"   style="height:80px;width: 80px" >
                            <option value="" selected>全部</option>
                            <option value="2022">2022</option>
                            <option value="2023">2023</option>
                            <option value="2024">2024</option>
                            <option value="2025">2025</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">建档日期:</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="start_time" name="start_time"  autocomplete="off">
                    </div>
                    <div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
                        <label class="layui-form-mid">-</label>
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="end_time" name="end_time"  autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" lay-submit lay-filter="user-search">查询</button>
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit lay-filter="user-clear-search">清空查询</button>
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit lay-filter="export-file">导出</button>
                </div>
            </div>

        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="add">增加档案</button>
                 <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="set_group">发送短信</button>
            </div>
            <table id="user-lists" lay-filter="user-lists"></table>


            <script type="text/html" id="staff-info">
                <div class="layui-input-inlines" style="text-align: left;">
                    <img src="{{d.avatar}}" style="height:80px;width: 80px;" class="image-show">
                    <p>会员ID：{{d.id}}</p>
                    <p style="text-overflow:ellipsis;overflow: hidden">客户姓名：{{d.contact}}</p>
                    <p>客户电话：{{d.telephone}}</p>
                    <p>绑定微信：{{d.nickname}}</p>
                </div>
            </script>
            <script type="text/html" id="qudao1">
                <div class="layui-input-inlines" style="text-align: left;">
                    <p>房屋地址：{{d.address}}</p>
                    <p style="text-overflow:ellipsis;overflow: hidden">房屋面积：{{d.areas}}平方</p>
                    <p>是否有宠物：
                            {{# if(d.pet==1){}}
                            有宠物
                            {{# }
                        else if(d.pet==2) { }}
                       未知
                    {{# }                            
                        else{ }}
                        无宠物
                    {{# } }}
                    </p>
                    <p>是否有厚玻璃：
                        {{# if(d.hbl==1){}}
                        有超厚玻璃
                        {{# }
                        else if(d.hbl==2) { }}
                        未知
                        {{# }
                        else { }}
                        无超厚玻璃
                        {{# } }}   
                    </p>
                </div>
            </script>
            <script type="text/html" id="reak">
                <div class="layui-input-inlines" style="text-align: left;">
                    <a  lay-event="user_remark" style="display: inline-block;">用户备注：
                        {{# if(d.user_remarks){}}
                        {{d.user_remarks}}
                        {{# }else{ }}
                        -
                        {{# } }}   
                    </a><br>
                    <a  lay-event="admin_remarks" style="display: inline-block;">管理员备注：
                        {{# if(d.admin_remarks){}}
                        {{d.admin_remarks}}
                        {{# }else{ }}
                        -
                        {{# } }}  
                      </a><br>
                    <a  lay-event="staff_remarks" style="display: inline-block;">保洁师叮嘱：
                        {{# if(d.staff_remarks){}}
                        {{d.staff_remarks}}
                        {{# }else{ }}
                        -
                        {{# } }}  
                      </a>
                    </a>
                </div>
            </script>
            <script type="text/html" id="qudao">
                <div class="layui-input-inlines" style="text-align: left;">
                <p>渠道：{{d.qudao}}</p>
                <a  lay-event="staff_remarks" style="display: inline-block; ">添加企微：
                    {{# if(d.wx==1){}}
                    添加
                    {{# }else{ }}
                    未添加
                    {{# } }}  
                  </a>
                </div>
                 <div style="display: inline-block; color:#ee0000">客户标签： {{d.user_label}}</div>
            </script>
            <script type="text/html" id="user-operation">
                <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="info">查看客户</a><p style="margin-bottom: 8px;"></p>
                <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="onedit">编辑档案</a><p style="margin-bottom: 8px;"></p>
                <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="custom">跟进记录</a><p style="margin-bottom: 8px;"></p>
                <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="del">删除客户</a>
            </script>
        </div>
    </div>
</div>
<style>
    .layui-table-cell {
        height: auto;
    }
</style>
<script>
    layui.config({
        version:"{$front_version}",
        base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','like','laydate'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table
            ,like = layui.like
            , laydate = layui.laydate;

        //监听搜索
        form.on('submit(user-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('user-lists', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //日期时间范围
        laydate.render({
            elem: '#start_time'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        //日期时间范围
        laydate.render({
            elem: '#end_time'
            ,type: 'datetime'
            ,trigger: 'click'
        });
        //清空查询
        form.on('submit(user-clear-search)', function(){
            $('#keyword_type').val('sn');
            $('#keyword').val('');  //清空输入框
            $('#level').val('');  //清空输入框
            $('#group_id').val('');  //清空输入框
            $('#start_time').val('');  //清空输入框
            $('#end_time').val('');  //清空输入框
            form.render('select');
            //刷新列表
            table.reload('user-lists', {
                where: [],
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 导出
        form.on('submit(export-file)', function(data){
            var field = data.field;
            $.ajax({
                url: '{:url("user/userexportFiles")}',
                type: 'get',
                data: field,
                dataType: 'json',
                error: function() {
                    layer.msg('导出超时，请稍后再试!');
                },
                success: function(res) {
                    table.exportFile(res.data.exportTitle,res.data.exportData, res.data.exportExt, res.data.exportName);
                },
                timeout: 15000
            });
            layer.msg('导出中请耐心等待~');
        });


        //事件
        var active = {
            
           set_group:function() { //设置分组
                var check_status = table.checkStatus('user-lists')
                    ,user_list = check_status.data; //得到选中的数据


                //是否已选数据
                if( user_list.length === 0){
                    return layer.msg('请选客户的档案');
                }
                //获取所选id
                ids = [];
                for (var i in user_list){
                    ids.push(user_list[i]['id']);
                }
                        
               $.ajax({
                    url:'{:url("message/messageadd")}',
                                data:{user_ids:ids},
                                type:"post",
                                success:function(res)
                                {
                                    if(res.code == 1)
                                    {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else if(res.code == 1){
                                        layui.layer.msg(res.msg)
                                    }
                                }
                            });
    
           
           
           
            }, 
            
            
            
            add:function(){ 
                layer.open({
                    type: 2
                    ,title: '增加用户'
                    ,content: '{:url("user/add")}'
                    ,area: ['60%', '80%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'user-submit-edit'
                            ,submit = layero.find('iframe').contents().find("#add-submit");
                        //监听提交
                        iframeWindow.layui.form.on('submit(add-submit)', function(data){
                            var data = data.field;
                            $.ajax({
                                url:'{:url("user/add")}',
                                data:data,
                                type:"post",
                                success:function(res)
                                {
                                    if(res.code == 1)
                                    {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    }
                                    else{
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                     
                     
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }

        }
        $('.layui-btn.layuiadmin-btn-user').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(document).on('click', '.image-show', function () {
            var src = $(this).attr('src');
            like.showImg(src,600);
        });
    });



    layui.define(['table', 'form'], function(exports){
        var $ = layui.$
            ,table = layui.table
            ,form = layui.form;

        //管理员管理
        table.render({
            id:'user-lists'
            ,elem: '#user-lists'
            ,url: '{:url("user/onelist")}' //模拟接口
            ,cols: [[
                {type: 'checkbox'}
                ,{field: 'contact', title: '客户的基础信息',align: 'center', toolbar: '#staff-info', }
                ,{field: 'qudao', title: '渠道来源',toolbar:'#qudao',}
                ,{field: 'address', title: '房屋信息及地址',toolbar: '#qudao1',width:260}
                ,{field: 'name', title: '跟进人员'}
                ,{field: 'create_time', title: '备注信息',toolbar: '#reak',width:240}
                ,{field: 'custom', title: '最近跟进记录'}
                ,{field: 'create_timess', title: '最近跟进时间',}
                ,{fixed: 'right', title: '操作', width: 100,toolbar: '#user-operation'}

            ]]
            ,page:true
            ,text: {none: '暂无数据！'}
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code":res.code,
                    "msg":res.msg,
                    "count": res.data.count, //解析数据长度
                    "data": res.data.lists, //解析数据列表
                };
            }
            , done: function fix() {
                $(".layui-table-main tr").each(function (index, val) {
                    $(".layui-table-fixed").each(function () {
                        $($(this).find(".layui-table-body tbody tr")[index]).height($(val).height());
                    });
                });
                $(".layui-table-header tr").each(function (index, val) {
                    $(".layui-table-fixed").each(function () {
                        $($(this).find(".layui-table-header thead tr")[index]).height($(val).height());
                    });
                });
                window.onresize = function () {
                    fix()
                }
            }
        });

        var moreShow = 0;
        //监听工具条
        table.on('tool(user-lists)', function(obj){
            if(obj.event === 'custom'){
                var id = obj.data.id;
                layer.open({
                    type: 2
                    ,title: '写入跟进的信息'
                    ,content: '{:url("custom/custom")}?id='+id
                    ,area: ['60%','90%']
                    ,btn: ['返回']
                })

            }
            if(obj.event === 'del'){
                var id = obj.data.id;
                layer.confirm('确定删除客户档案吗', function(index){
                    $.ajax({
                        url:'{:url("custom/del")}',
                        data:{id:id},
                        type:"post",
                        success:function(res)
                        {
                            if(res.code == 1)
                            {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                table.reload('user-lists'); //数据刷新
                                obj.del();
                            }
                        }
                    });
                    layer.close(index);
                });
            
            
            }
            if(obj.event === 'onedit'){
                var id = obj.data.id;
                layer.open({
                    type: 2
                    ,title: '编辑客户'
                    ,content: '{:url("user/onedit")}?id='+id
                    ,area: ['60%', '80%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find('#adjust_user-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(adjust_user-submit)', function(data){
                            var field = data.field;
                            $.ajax({
                                url:'{:url("user/onedit")}',
                                data:field,
                                type:"post",
                                success:function(res)
                                {
                                    if(res.code == 1)
                                    {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    }else{
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })

            }
            if(obj.event === 'info'){
                var id = obj.data.id;
                layer.open({
                    type: 2
                    ,title: '客户档案'
                    ,content: '{:url("user/oneinfo")}?id='+id
                    ,area: ['85%','90%']
                    ,btn: ['返回']
                })
            }
            
            if(obj.event === 'adjust_user'){
                var id = obj.data.id;
                layer.open({
                    type: 2
                    ,title: '财务跳转'
                    ,content: '{:url("user/adjustAccount")}?id='+id
                    ,area: ['90%', '90%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find('#adjust_user-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(adjust_user-submit)', function(data){
                            var field = data.field;
                            $.ajax({
                                url:'{:url("user/adjustAccount")}',
                                data:field,
                                type:"post",
                                success:function(res)
                                {
                                    if(res.code == 1)
                                    {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    }else{
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }

       
       
       
        });
    });
</script>