<style>
    .layui-table-cell{
        height:auto !important;
    }
</style>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">

            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">姓名:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入姓名"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">电话:</label>
                        <div class="layui-input-inline">
                            <input name="phone" class="layui-input" placeholder="输入电话"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">备注:</label>
                        <div class="layui-input-inline">
                            <input name="note" class="layui-input" placeholder="输入备注"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">城市</label>
                        <div class="layui-input-inline">
                            <input name="city" class="layui-input" placeholder="输入城市"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">日期范围</label>
                        <div class="layui-inline" id="ID-laydate-range">
                            <div class="layui-input-inline">
                                <input name="date_start" type="text" autocomplete="off" id="ID-laydate-start-date" class="layui-input" placeholder="开始日期" value="<?= date('Y-m-d', strtotime('-1 day')) ?>">
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline">
                                <input name="date_end" type="text" autocomplete="off" id="ID-laydate-end-date" class="layui-input" placeholder="结束日期" value="<?= date('Y-m-d') ?>">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="tbAdvTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <a class="layui-btn" search-expand>展开 <i class="layui-icon layui-icon-down"></i></a>
                    </div>
                </div>
            </form>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<script type="text/html" id="tableBarUser1221">
    <!-- <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a> -->
    <a class="layui-btn layui-btn-xs" data-dropdown="#userTbDrop{{d.id}}" no-shade="true">
        更多<i class="layui-icon layui-icon-drop" style="font-size: 12px;margin-right: 0;"></i></a>
    <!-- 下拉菜单 -->
    <ul class="dropdown-menu-nav dropdown-bottom-right layui-hide" id="userTbDrop{{d.id}}">
        <div class="dropdown-anchor"></div>
        <li><a lay-event="genjin"><i class="layui-icon layui-icon-edit"></i>跟进</a></li>
<!--        <li><a lay-event="genjin"><i class="layui-icon layui-icon-edit"></i>客户跟进</a></li>-->
<!--        <li><a lay-event="gjlist"><i class="layui-icon layui-icon-edit"></i>跟进列表</a></li>-->
        <!--<li><a lay-event="tuisong"><i class="layui-icon layui-icon-file"></i>推送</a></li>-->
    </ul>
</script>

<script type="text/html" id="tableBarUser">
    <div class="layui-clear-space">
<!--        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
        <a class="layui-btn layui-btn-xs" lay-event="genjin">
            跟进
            <i class="layui-icon layui-icon-down"></i>
        </a>
    </div>
</script>
<script>
    layui.use(function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var formX = layui.formX;
        var notice = layui.notice;

        var laydate = layui.laydate
        var xmSelect = layui.xmSelect
        var dropdown = layui.dropdown;
        // 日期
        laydate.render({
            elem: '#ID-laydate-range',
            range: ['#ID-laydate-start-date', '#ID-laydate-end-date']
        });
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            id: 'clueTable',
            url: 'api/xiansuo-private',
            page: true,
            limit:10,
            limits:[10, 50],
            toolbar: true,
            defaultToolbar:['exports','print'],
            cellMinWidth: 100,
            cols: [[
                {field: 'clue_id', title: '线索ID',width: 90, align: 'center'},
                {field: 'promotion_name', title: '推广计划名称',align: 'center'},
                {field: 'name', title: '姓名',align: 'center'},
                {field: 'telephone', title: '电话',align: 'center'},
                {field: 'convert_status', title: '转化状态',align: 'center'},
                {field: 'auto_province_name', title: '系统识别省',align: 'center'},
                {field: 'auto_city_name', title: '系统识别市',align: 'center'},
                {field: 'note', title: '备注',align: 'center'},
                {field: 'create_time_detail', title: '线索创建时间',align: 'center'},
                {fixed: 'right', title: '操作', toolbar: '#tableBarUser', align: 'center', width: 80},
            ]],
        });
        /* 表格搜索 */
        form.on('submit(tbAdvTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });
        // /* 添加时间 */
        // laydate.render({
        //     elem: '#formAdvDateSel1',
        //     range: true,
        //     trigger: 'click'
        // });
        // 添加
        $('#btnAddUser123').click(function () {
            showEditModel();
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(tableUser)', function (obj) {
            console.log(obj)
            var data = obj.data;
            if (obj.event === 'edit') { // 修改
                showEditModel(data);
            } else if (obj.event === 'zhengzhuang') { // 删除
                zhengzhuang(data);
            } else if (obj.event === 'gjlist') { // 跟进列表
                gjlist(data);
            }else if (obj.event === 'genjin') { // 跟进
                genjin(data);
            }else if(obj.event === 'tuisong'){ // 推送
                tuisong(data);
            } else if (obj.event === 'more') {
                // 更多 - 下拉菜单
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '查看',
                        id: 'detail'
                    },{
                        title: '删除',
                        id: 'del'
                    }],
                    click: function(menudata){
                        if(menudata.id === 'detail'){
                            layer.msg('查看操作，当前行 ID:'+ data.id);
                        } else if(menudata.id === 'del'){
                            layer.confirm('真的删除行 [id: '+ data.id +'] 么', function(index){
                                obj.del(); // 删除对应行（tr）的DOM结构
                                layer.close(index);
                                // 向服务端发送删除指令
                            });
                        }
                    },
                    id: 'dropdown-table-tool',
                    align: 'right', // 右对齐弹出
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                });
            }
            // dropdown.hideAll();
        });
        // 显示表单弹窗
        function showEditModel(mUser) {
            var url_get = mUser ? 'users/' + mUser.id + '/edit' : 'users/create';
            var layIndex = admin.open({
                title: (mUser ? '修改' : '添加') + '用户',
                url: url_get,
                data: {user: mUser},     // 传递数据到表单页面
                end: function () {
                    //
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    var mUser = admin.getLayerData('#modelUserForm').user;  // 列表页面传递的数据，#modelUserForm这个只要写弹窗内任意一个元素的id即可
                    console.log(mUser);
                    form.val('modelUserForm', mUser);
                    // 回显数据
                    if (mUser) {
                        $('.hide').remove()
                    }
                    // 表单提交事件
                    var id = mUser ? mUser.id : 0;
                    form.on('submit(modelSubmitUser)', function (data) {
                        admin.showLoading( 'body',1, '.7');
                        var url = mUser ? 'users/' + mUser.id : 'users';
                        if (mUser) {
                            data.field._method = 'PUT'
                        }
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 666) {
                                msg('消息通知',res.msg,1)
                                insTb.reload({}, 'data');
                                admin.closeDialog('#modelUserForm');  // 关闭页面层弹窗
                            } else {
                                msg('消息通知',res.msg,2)
                            }
                            admin.removeLoading( 'body',true, true);
                        }, 'json');
                        return false;
                    });
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        function tuisong(mUser, _this){
            var data = {'xs_id':mUser.id}
            layer.confirm('确定推送？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function(i){
                layer.close(i);
                admin.showLoading('body', 1, '.7');
                $.post('xiansuo/tuisong', {
                    _token: 'lY9I6mn5ij6xKtr0iil9xVXbyaHaowdYC3IWSovG',
                    data: data
                }, function (res) {
                    if (res.code == 666) {
                        msg('提示',res.msg,1)
                        insTb.reload({}, 'data');
                    } else {
                        msg('提示',res.msg,2)
                    }
                    admin.removeLoading('body', true, true);
                }, 'json');
            })
        }

        function zhengzhuang(mUser){

            var layIndex = admin.open({
                title:'查看:'+mUser.name+' 症状',
                url: 'xiansuo/zhengzhuang/'+mUser.id,
                end: function () {
                    //
                },
                success: function (layero, dIndex) {
                    form.val('addmoneyForm', mUser);
                }
            });
        }
        // 跟进状态
        function genjin(data) {
            console.log(data.clue_id)
            layer.open({
                type: 2,
                area: ['800px', '300px'], // 宽高
                title: "跟进",
                shade: 0.1,
                content: 'genjin?clue_id='+ data.clue_id
            });
        }
        function gjlist(mUser){
            var layIndex = admin.open({
                area:'90%',
                title:mUser.name+'      跟进列表',
                url: 'xiansuo/gjlist/'+mUser.id,
                end: function () {
                    //
                },
                success: function (layero, dIndex) {

                }
            });
        }
    });
</script>
<script>
    window.clearTimeout(localStorage.getItem("in_user"))
    localStorage.removeItem("in_user")
    localStorage.removeItem("in_user_box")

</script>