{% 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: 40px;
        }

        .userMsg>img {
            width: 100px;
            height: 100px;
        }

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

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

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

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

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

        /* 筛选框 */

        .selectModel {
            display: none;
        }

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

        .numbox {
            display: none;
        }
        .number {
            width: 50%;
            margin: 15px auto;
        }
        .numberBtn {
            width: 50%;
            margin: 30px auto;
        }

    </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">
                <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm fr" onclick="selectModelFun()">新增</button>
                <h2>
                    库存管理
                    <form class="layui-form" action="" 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="city" id="farmSelect">
                                </select>
                            </div>
                        </div>
                    </form> 
                </h2>
                
            </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="rtype" 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="spec" 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="rese" required lay-verify="required" placeholder="请输入库存" autocomplete="off" class="layui-input">
                    </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 class="numbox" id="numbox">
        <input type="number" id="number"  required lay-verify="required" placeholder="请输入数字" autocomplete="off" class="layui-input number">  
        <div class="numberBtn">
            <button class="layui-btn  layui-btn-normal" lay-submit lay-filter="formNumber">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="formquxiao()">取消</button>
        </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;
        layui.use(['element', 'layer', 'laydate', 'laypage', 'form'], function () {
            // layer = layui.layer;
            laydate = layui.laydate;
            laypage = layui.laypage;
            form = layui.form;
            //执行一个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('repertory_new', 1)
                            function gedata(url, page) {
                                $.ajax({
                                    url: 'repertory_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'
                                            , layout: ['prev', 'page', 'next', 'skip']
                                            , jump: function (obj, first) {
                                                if (!first) {
                                                    $.ajax({
                                                        url: 'repertory_new',
                                                        type: 'get',
                                                        data: { page: obj.curr, ids: $('#farmSelect option:eq(0)').val() },
                                                        dataType: 'html',
                                                        success: function (data) {
                                                            console.log(data);
                                                            $('#box').children('#chunk').html(data);
                                                            console.log($('#totalPage').val());
                                                        },
                                                        error: function (type) {
                                                            console.log(type)
                                                        }
                                                    })
                                                }
                                            }
                                        });
                                    },
                                    error: function (type) {
                                        console.log(type)
                                    }
                                })
                            }
                    },
                    error: function (type) {
                        console.log(type)
                    }
                })
            }

             //编辑新增监听提交
             form.on('submit(formDemo)', function (data) {
                $.ajax({
                    url: 'repertory_view',
                    data: $("#formDiv").serialize(),
                    type: 'post',
                    success: function (res) {
                        clearForm()
                        if (data.field.id !== "") {
                            layer.msg("修改成功", { icon: 1 });
                        } else {
                            layer.msg("新增成功", { icon: 1 });
                        }
                        sessionStorage.removeItem('seelectID')
                        alert('成功')
                    },
                    error: function (type) {
                        if (data.field.id !== "") {
                            layer.msg("修改失败", { icon: 2 });
                        } else {
                            layer.msg("新增失败", { icon: 2 });
                        }
                        sessionStorage.removeItem('seelectID')
                        alert('失败')
                    }
                })
                return false;
            });

             //出库入库监听提交
             form.on('submit(formNumber)', function (data,id) {
                $.ajax({
                    url: 'repertory_new_view',
                    data: {
                        id: sessionStorage.getItem('kuID'),
                        req: sessionStorage.getItem('req'),
                        num: $(" #number ").val(),
                    },
                    type: 'post',
                    success: function (res) {
                        clearForm()
                        layer.msg("操作成功", { icon: 1 });
                        $(" #number ").val("")
                    },
                    error: function (type) {
                        clearForm()
                        layer.msg("操作失败", { icon: 2 });
                        $(" #number ").val("")
                    }
                })
                return false;
                console.log($(" #number ").val())
                console.log(id)

            });

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

            //选择农场的下拉框监听回调
            form.on('select()', function(data){
            sessionStorage.setItem('seelectID', data.value)
            gedata('repertory_new', 1 )
            function gedata(url, page) {
                $.ajax({
                    url: 'repertory_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: 30
                            , count: $('#totalPage').val()
                            , theme: '#2ca9f9'
                            , layout: ['prev', 'page', 'next', 'skip']
                            , jump: function (obj, first) {
                                if (!first) {
                                    $.ajax({
                                        url: 'repertory_new',
                                        type: 'get',
                                        data: { page: obj.curr, ids: sessionStorage.getItem('seelectID') },
                                        dataType: 'html',
                                        success: function (data) {
                                            console.log(data);
                                            $('#box').children('#chunk').html(data);
                                            console.log($('#totalPage').val());
                                        },
                                        error: function (type) {
                                            console.log(type)
                                        }
                                    })
                                }
                            }
                        });
                    },
                    error: function (type) {
                        console.log(type)
                    }
                })
             }
            });
        })

        //修改
        function amend (id) {
            if (sessionStorage.getItem('seelectID') !== null) {
                    $.ajax({
                    url: 'repertory_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="rtype"]').val(data[0].rtype) //物料类别
                        $('input[name="spec"]').val(data[0].spec) //规格
                        $('input[name="rese"]').val(data[0].rese) //库存
                        $('input[name="id"]').val(data[0].id) //id

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

        //删除
        function del (id) {
            layer.confirm('真的删除行么',{title:'删除'}, function (index) {
                console.log(id)
                $.ajax({
                    url: 'repertory_new',
                    type: 'post',
                    data: {
                        id: id,
                        req: 'del',
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            layer.close(index);
                            location.reload();
                            layer.msg("操作成功!", {time: 1000, icon: 1})
                        }
                    }
                })
            });
        }

        //入库
        function enter (id) {
            sessionStorage.setItem('kuID', id)
            sessionStorage.setItem('req', 'add')
            repertory ()
        }

        //出库
        function come (id) {
            sessionStorage.setItem('kuID', id)
            sessionStorage.setItem('req', 'see')
            repertory ()
        }

        //重置
        function clearForm() {
            form.val('newsFormDiv', {
                "name": "",
                "rtype": "",
                "spec": "",
                "rese": "",
                "id": "",
            });
            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: ['450px', '500px'],
                        cancel: function(){
                            // 右上角关闭事件的逻辑
                            clearForm ()
                        }
                    })
                })
            } else {
                layer.msg("请先选择园区！", { time: 1000, icon: 5 })
            }
        }

        //出入库弹出层
        function repertory () {
            layui.use(['layer'], function () {
                layer = layui.layer;
                var index = layer.open({
                    type: 1,
                    title: '入库出库数量',
                    content: $('#numbox'),
                    area: ['400px', '200px'],
                    cancel: function(){
                        // 右上角关闭事件的逻辑
                        $(" #number ").val("")
                        sessionStorage.removeItem('kuID')
                        sessionStorage.removeItem('req')
                        sessionStorage.removeItem('seelectID')
                    }
                })
            })
        }

        //出入库取消
        function formquxiao () {
            layer.closeAll();
            $(" #number ").val("")
            sessionStorage.removeItem('req')
            sessionStorage.removeItem('kuID')
        }
    </script>
</body>

</html>