{% 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/font/fontIcon/iconfont.css' %}">
    <link rel="stylesheet" href="{% static '/lib/css/reset.css' %}">
    <style>
        /* 面包屑导航 */

        .breadcrumbs {
            /* line-height: 38px; */
            padding: 12px 0 12px 30px;
            border-bottom: 1px solid #dbdbdb;
        }

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

        /* 内容 */

        .mainContent {
            padding: 30px;
        }

        .widget-title {
            position: relative;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title::after {
            position: absolute;
            content: '';
            left: 0;
            top: 9px;
            height: 15px;
            border-left: 5px solid #23a3ee;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title h2 {
            font-weight: 700;
            line-height: 33px;
        }

        .widget-title h2 span {
            font-size: 13px;
            font-weight: 100;
            margin-left: 20px;
            color: #333;
        }

        .mainContent>div:nth-child(2) {
            margin-top: 30px;
        }

        /*  */

        .userItem {
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px 10px 0 10px;
        }

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

        .userMsg>img {
            width: 100px;
            height: 80px;
            margin-top: 20px;
        }

        .userInfo {
            line-height: 28px;
            margin-left: 130px;
        }

        .userInfo>p {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .operateDiv {
            text-align: right;
            margin: 10px;
        }

        .layui-row>div {
            padding: 10px;
        }


        /* 筛选框 */

        .selectModel {
            display: none;
        }

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

        /*  */

        /* 暂无数据 */
        /* .not span {
            display: inline-block;
            margin: 0 0 0 45%;
            font-size: 25px;
            font-weight: 500;
            color: rgb(139, 139, 139);
        } */

        .layui-col-xs6 {
            padding: 10px 10px 0 10px;
        }

        /* 图片预览 */

        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }

        #progressBar {
            display: none;
        }

        /* 表单样式 */

        .layui-form-item .layui-input-inline {
            width: 50%;
            /* margin: 10px 0 0 0; */
        }

        .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%;
        }
    </style>
</head>

<body>
    <div class="content">
        <!-- 导航 -->
        <div class="breadcrumbs">
            <span class="layui-breadcrumb">
                <a target="BoardRight">首页</a>
                <a>
                    <cite>农场管理</cite>
                </a>
            </span>
        </div>
        <!-- 内容 -->
        <div class="mainContent">
            <div class="widget-title">
                <h2>农场管理
                    <form class="layui-form" action="" id="formID" style="display:inline-block;vertical-align: middle;">
                        <div class="layui-form-item" style="margin-bottom: 0;">
                            <label class="layui-form-label" style="color:#029eef">选择园区：</label>
                            <div class="layui-input-inline">
                                <select name="identity" id="farmSelect">
                                </select>
                            </div>
                        </div>
                    </form>
                </h2>
                
                <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm fr"
                    onclick="selectModelFun()">新增</button>
            </div>
            <div class="">
                <div class="layui-row" id="box">
                    <div id="chunk">

                    </div>
                </div>
                <div class="fenpei-bottom">
                    <div id="page"></div>
                </div>
            </div>
        </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">基地面积(亩)：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="area" required lay-verify="required" placeholder="请输入基地面积"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">当前品种：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="curr_var" required lay-verify="required" placeholder="请输入品种名称"
                            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="img_url" 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-inline">
                        <textarea name="content" id="editor1" 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>

    <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
    <script src="{% static '/lib/layui/layui.js'%}"></script>
    <script>
        var element, laydate, laypage, form, upload;
        layui.use(['element', 'layer', 'laydate', 'laypage', 'form', 'upload'], function () {
            laydate = layui.laydate;
            laypage = layui.laypage;
            form = layui.form;
            upload = layui.upload;
            $ = layui.jquery;
            //执行一个laydate实例
            laydate.render({
                elem: '#timSelectBox' //指定元素
                , range: true
                , done: function (value, date) {
                    var dateArray = value.split(' - ')
                    $('#tableBegin').val(dateArray[0])
                    $('#tableEnd').val(dateArray[1])
                }
            });

            //园区下拉列表数据渲染
            gedataA('park_land')
            function gedataA(url) {
                $.ajax({
                    url: 'park_land',
                    type: 'get',
                    data: {

                    },
                    dataType: 'json',
                    success: function (data) {
                        var d = data
                        for (var i = 0; i < d.length; i++) {
                            var name = data[i].name
                            var id = data[i].id
                            var opt = "<option value='" + id + "'>" + name + "</option>"
                            $('#farmSelect').append(opt);
                            form.render('select'); //刷新select选择框渲染
                        }


                        //页面数据渲染
                        gedata('garden_manage_new', 1)
                        function gedata(url, page) {
                            sessionStorage.setItem('seelectID', $('#farmSelect option:eq(0)').val())
                            $.ajax({
                                url: 'garden_manage_new',
                                type: 'get',
                                data: {
                                    page: page,
                                    ids: $('#farmSelect option:eq(0)').val(),
                                },
                                dataType: 'html',
                                success: function (data) {
                                    $('#box').children('#chunk').html(data);
                                    laypage.render({
                                        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                                        , count: $('#totalPage').val()
                                        , theme: '#2ca9f9'
                                        , limit: '9'
                                        , layout: ['prev', 'page', 'next', 'skip']
                                        , jump: function (obj, first) {
                                            if (!first) {
                                                $.ajax({
                                                    url: 'garden_manage_new',
                                                    type: 'get',
                                                    data: { page: obj.curr, ids: $('#farmSelect option:eq(0)').val() },
                                                    dataType: 'html',
                                                    success: function (data) {
                                                        $('#box').children('#chunk').html(data);
                                                    },
                                                    error: function (type) {
                                                        console.log(type)
                                                    }
                                                })

                                            }
                                        }
                                    });
                                },
                                error: function (type) {
                                    console.log(type)
                                }
                            })
                        }
                    },
                    error: function (type) {
                        console.log(type)
                    }
                })
            }


            //选择农场的下拉框监听回调
            form.on('select()', function (data) {
                sessionStorage.setItem('seelectID', data.value)
                gedata('garden_manage_new', 1)
                function gedata(url, page) {
                    $.ajax({
                        url: 'garden_manage_new',
                        type: 'get',
                        data: {
                            page: page,
                            ids: data.value,
                        },
                        dataType: 'html',
                        success: function (data) {
                            $('#box').children('#chunk').html(data);
                            laypage.render({
                                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                                , count: $('#totalPage').val()
                                , limit: 9
                                , theme: '#2ca9f9'
                                , layout: ['prev', 'page', 'next', 'skip']
                                , jump: function (obj, first) {
                                    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                                    console.log(obj.limit); //得到每页显示的条数
                                    if (!first) {
                                        $.ajax({
                                            url: 'garden_manage_new',
                                            type: 'get',
                                            data: { page: obj.curr, ids: sessionStorage.getItem('seelectID') },
                                            dataType: 'html',
                                            success: function (data) {
                                                $('#box').children('#chunk').html(data);
                                                console.log($('#totalPage').val());
                                            },
                                            error: function (type) {
                                                console.log(type)
                                            }
                                        })
                                    }
                                }
                            });
                        },
                        error: function (type) {
                            console.log(type)
                        }
                    })
                }
            });


            //图片上传
            var uploadInst = upload.render({
                elem: '#pickImg'
                , url: 'pick_photo'
                , field: 'gar_img'
                , 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 = $('#demoText1');
                    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) {
                var postData = $.param({ ids: sessionStorage.getItem('seelectID') }) + '&' + $("#formDiv").serialize();
                var img_url = $('#img_url').val();
                if (!img_url) {
                    layer.msg("请上传农场封面图", { icon: 5 });
                    return false;
                }
                $.ajax({
                    url: 'garden_manage_view',
                    data: postData,
                    type: 'post',
                    success: function (res) {
                        clearForm()
                        if (data.field.id !== "") {
                            layer.msg("修改成功", { icon: 1 });
                        } else {
                            layer.msg("新增成功", { icon: 1 });
                        }
                        sessionStorage.removeItem('seelectID')
                    },
                    error: function (type) {
                        if (data.field.id !== "") {
                            layer.msg("修改失败", { icon: 2 });
                        } else {
                            layer.msg("新增失败", { icon: 2 });
                        }
                        sessionStorage.removeItem('seelectID')
                    }
                })
                return false;
            });

            //监听用户是否刷新，刷新页面删除本地存储的ID
            $(function () {
                $(window).on('beforeunload', function () {
                    sessionStorage.removeItem('seelectID')
                });
            });

        })

        //修改
        function amend(id) {
            if (sessionStorage.getItem('seelectID') !== null) {
                $.ajax({
                    url: 'garden_manage_new',
                    type: 'post',
                    async: false,
                    data: {
                        id: id,
                        req: 'edit'
                    },
                    dataType: 'json',
                    success: function (data) {
                        selectModelFun()
                        $('input[name="name"]').val(data[0].name) //名称
                        $('input[name="area"]').val(data[0].area) //面积
                        $('input[name="manage_user"]').val(data[0].manage_user) //管理者
                        $('input[name="curr_var"]').val(data[0].curr_var) //品种
                        $('textarea[name="content"]').val(data[0].farm_desc) //介绍
                        $('input[name="id"]').val(data[0].id) //id

                    },
                    error: function (type) {
                        console.log(type)
                    }
                })
            } else {
                layer.msg("请先选择园区！", { time: 1000, icon: 5 })
            }
        }


        //删除
        function del(id) {
            if (sessionStorage.getItem('seelectID') !== null) {
                layer.confirm('真的删除行么', { title: '删除' }, function (index) {
                    $.ajax({
                        url: 'garden_manage_new',
                        type: 'post',
                        data: {
                            id: id,
                            req: 'del',
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data) {
                                layer.close(index);
                                location.reload();
                                parent.layer.msg("操作成功!", { time: 1000, icon: 1 })
                            }
                        }
                    })
                });
            } else {
                layer.msg("请先选择园区！", { time: 1000, icon: 5 })
            }
        }

        function clearForm() {
            form.val('newsFormDiv', {
                "name": "",
                "area": "",
                "curr_var": "",
                "farm_img": "",
                "content": "",
                "id": "",
            });
            $('#img_url').val('');
            $('.uploadImg').addClass('uploadImgIcon');
            $('#demo1').removeAttr('src'); //图片链接（base64）
            layer.closeAll(); //疯狂模式，关闭所有层
            location.reload()
        }

        function selectModelFun() {
            if (sessionStorage.getItem('seelectID') !== null) {
                layui.use(['layer'], function () {
                    layer = layui.layer;
                    var index = layer.open({
                        type: 1,
                        title: '园区筛选',
                        content: $('#selectModel'),
                        area: ['550px', '570px'],
                        cancel: function () {
                            // 右上角关闭事件的逻辑
                            clearForm()
                            sessionStorage.removeItem('seelectID')
                        }
                    })
                })
            } else {
                layer.msg("请先选择园区！", { time: 1000, icon: 5 })
            }


        }

    </script>
</body>

</html>