{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>专家诊断</title>
    <link rel="stylesheet" href="{% static '/lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static '/lib/css/reset.css' %}">
    <style>
        body {
            background-color: #f5f5f5;
        }

        .breadcrumbs {
            padding: 12px 0 12px 30px;
            border-bottom: 1px solid #dbdbdb;
        }

        .layui-breadcrumb>* {
            font-size: 16px;
        }

        .content {
            padding-top: 46px;
        }

        .fixedDiv {
            position: fixed;
            width: 100%;
            top: 0;
            /* z-index: 999;
            box-shadow: 0px 3px 5px 0px #ececec;*/
            background: #fff;
        }

        /* 专家模块 */
        .expertGather {
            display: flex;
            flex-wrap: wrap;
            margin-top: 35px;
        }

        .expertGatherItem {
            width: 31%;
            margin: 1%;
            box-sizing: border-box;
            background: #fff;
            border-radius: 5px;
            position: relative;
            padding: 40px 20px 40px 45px;
        }

        .expertGatherItem>img {
            position: absolute;
            top: -7px;
            left: -8px;
        }

        .expertInner {
            display: flex;
        }

        .expertImg {
            width: 135px;
            height: 135px;
            border: 1px solid #d3d3d3;
        }

        .expertImg>img {
            width: 100%;
            height: 100%;
        }

        .expertTxt {
            flex: 1;
            margin-left: 25px;
        }

        .expertTxt h1 {
            font-size: 16px;
            font-weight: 700;
            display: inline-block;
            border-bottom: 2px solid #f7304d;
            line-height: 25px;
        }

        .expertTxt p {
            font-size: 14px;
            line-height: 35px;
        }

        .expertTxt .desc {
            font-size: 12px;
            line-height: 26px;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            margin-bottom: 10px;
            color: #585858;
        }

        .expertTxt .ctrlBtn {
            text-align: right;
        }

        .expertTxt .ctrlBtn a {
            background: #3f9df4;
            color: #fff;
            padding: 4px 9px;
            border-radius: 5px;
            font-size: 12px;
        }

        .additional {
            /* border: 1px solid #000; */
            /* position: relative; */
            /* padding: 5px 0 5px 10px; */
        }

        /* 弹框 */
        .selectModel {
            display: none;
        }

        .selectModel>div {
            width: 90%;
            margin-top: 30px;
        }

        .uploadImg {
            position: absolute;
            margin: 10px 0;
            width: 92px;
            height: 92px;
            background: none;
            border: 1px solid #b8b8b8;
            cursor: pointer;
        }

        .uploadImgIcon {
            background: url(../../static/imgs/uploadImgIcon.png) no-repeat center;
            background-size: 80% 80%;
        }

        .rightBtn {
            position: absolute;
            right: 40px;
            top: 55px;
        }

        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="floatNav fixedDiv">
            <!-- 导航 -->
            <div class="breadcrumbs">
                <span class="layui-breadcrumb">
                    <a target="BoardRight">首页</a>
                    <a>
                        <cite>在线专家</cite>
                    </a>
                </span>
            </div>
        </div>

        <!-- 新增 -->
        {% if role ==  '1' %} 
        <div class="additional">
            <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm rightBtn"
                onclick="selectModelFun('新增')">新增</button>
        </div>
        {% endif %}
        

        <!-- 内容 -->
        <div class="expertGather" id="expertGather">

        </div>
        <div class="fenpei-bottom" style="padding: 20px 20px 20px 45px;">
            <input type="hidden" id="totalPage" value={{ nums }}>
            <div id="page"></div>
        </div>

        <!-- 筛选框 -->
        <div id="selectModel" class="selectModel">
            <div>
                <form class="layui-form" action="" id="formDiv" lay-filter="newsFormDiv">
                    <input type="hidden" name="id">
                    <div class="layui-form-item">
                        <label class="layui-form-label">专家名称：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入专家名称"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">联系QQ：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="relation" required lay-verify="required" placeholder="请输入联系的QQ号"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">专家照片：</label>
                        <div class="layui-input-inline">
                            <button type="button" class="uploadImg uploadImgIcon" id="pickImg"></button>
                            <!-- 隐藏的input -->
                            <input type="hidden" id="img_url" name="pic" value="" />
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" id="demo1">
                                <p id="demoText"></p>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">专家介绍：</label>
                        <div class="layui-input-block">
                            <textarea name="desc" id="desc" lay-verify="required" placeholder="请输入介绍"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn  layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div>
            <!-- <img src="{% static 'imgs/specialist/intr03.jpg'%}" alt=""> -->
        </div>
    </div>


    <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
    <script src="{% static '/lib/layui/layui.js'%}"></script>
    <script src="{% static '/js/backstageNet/common.js'%}"></script>

</body>
<script>
    var laypage, form, upload,element;
    layui.use(['laypage', 'form', 'upload','element'], function () {
        laypage = layui.laypage;
        form = layui.form;
        element = layui.element;
        upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#pickImg'
            , url: 'expert_img'
            , field: 'upload'
            , accept: 'images' //只允许上传图片
            // , acceptMime: 'image/*' //只筛选图片
            // ,exts: 'jpg|jpeg' 
            , size: 1024 * 2 //限定大小
            , xhr: function () { } //上传视频进度条
            , before: function (obj) {
                $('.uploadImg').removeClass('uploadImgIcon')
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code == 0) {
                    //上传成功
                    document.getElementById("img_url").value = res.data.src;
                } else {
                    return layer.msg('上传失败');
                }


            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            window.id = data.field.id
            var img_url = $('#img_url').val();
            if (!img_url) {
                layer.msg("请上传专家照片", { icon: 5 });
                return false;
            }
            $.ajax({
                url: 'expert_info',
                data: $("#formDiv").serialize(),
                type: 'post',
                success: function (data) {
                    if (data) {
                        getData('expert_info', $('.layui-laypage-curr em').eq(1).html())
                        clearForm()
                    } else {

                    }
                },
                error: function (type) {

                }
            })
            return false;

        });
        getData('expert_info', 1)
        
    })
    function getData(url, page) {
            $.ajax({
                url: url,
                type: 'get',
                data: {
                    page: page
                },
                dataType: 'html',
                success: function (data) {
                    $('#expertGather').html(data);
                    laypage.render({
                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                        // , count: 30
                        , count: $('#totalPage').val()
                        , theme: '#2ca9f9'
                        , curr: page
                        , layout: ['prev', 'page', 'next', 'skip']
                        , jump: function (obj, first) {
                            if (!first) {
                                $.ajax({
                                    url: url,
                                    type: 'get',
                                    data: { page: obj.curr },
                                    dataType: 'html',
                                    success: function (data) {
                                        $('#expertGather').html(data);
                                    },
                                    error: function (type) {
                                        console.log(type)
                                    }
                                })
                            }
                        }
                    });
                },
                error: function (type) {
                    // console.log(type)
                }
            })
        }
    function selectModelFun() {
        layui.use(['layer'], function () {
            layer = layui.layer;
            var index = layer.open({
                type: 1,
                title: '专家添加',
                content: $('#selectModel'),
                area: ['550px', '570px'],
                cancel: function () {
                    // 右上角关闭事件的逻辑
                    clearForm()
                }
            })
        })
    }

    function clearForm() {
        form.val('newsFormDiv', {
            "title": "",
            "writer": "",
            "intro": ""
        });
        $('#img_url').val('');
        $('.uploadImg').addClass('uploadImgIcon');
        $('#demo1').removeAttr('src'); //图片链接（base64）
        layer.closeAll();
    }

    function del(id) {
        $.ajax({
            url: 'expert_info',
            data: {
                id:id
            },
            type: 'post',
            success: function (data) {
                if (data) {
                    getData('expert_info', $('.layui-laypage-curr em').eq(1).html())
                } else {

                }
            },
            error: function (type) {

            }
        })
    }
</script>

</html>