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

        /*  */
    </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> -->
                <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm fr" onclick="selectModelFun('新增')">新增</button>
                <h2>销售管理
                    <!-- <span id="Uptime">草莓园商情站-设备：20126555</span> -->
                </h2>
            </div>
            <div class="">
                <div class="layui-row" id="sellList">
            
                    <!-- <div class="layui-col-xs6 layui-col-sm6 layui-col-md4">
                        <div class="userItem">
                            <p class="username">牛奶草莓园</p>
                            <div class="userMsg">
                                <img src="{% static 'imgs/Customer.png'%}" class="fl" alt="">
                                <div class="userInfo">
                                    <p>客户： 牛奶草莓园</p>
                                    <p>出货数量： 1.00()</p>
                                    <p>金额/元： 20000.00</p>
                                    <p>时间： 2019-06-20</p>
                                </div>
                            </div>
                            <div class="operateDiv">
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal">修改</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger">删除</button>
                            </div>
                        </div>
                    </div> -->

                </div>
                <div class="fenpei-bottom">
                    <div id="page"></div>
                </div>
                <input type="hidden" value="{{nums}}" id="totalPage">
            </div>
        </div>

    </div>
    <!-- 筛选框 -->
    <div id="selectModel" class="selectModel">
        <!-- <div class="widget-title">
            <h2>条件帅选
                <span id="Uptime"></span>
            </h2>
        </div> -->
        <div>
            <form class="layui-form" action="" lay-filter="sellForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">农场</label>
                    <div class="layui-input-block">
                        <select name="farm" lay-verify="required">
                            {% for farm in farm_list %}
                            <option value="{{farm.name}}">{{farm.name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <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-block">
                        <input type="text" name="time" class="layui-input" required lay-verify="required" id="timSelectBox" autocomplete="off" placeholder="请输入销售时间">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">数量/公斤</label>
                    <div class="layui-input-block">
                        <input type="number" name="num" 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-block">
                        <input type="text" name="money" required lay-verify="required" placeholder="请输入金额" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" name="id" value="">
                        <button class="layui-btn  layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="cancel()">取消</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;
        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])
                }
            });



            //监听提交
            form.on('submit(formDemo)', function (data) {
                // layer.msg(JSON.stringify(data.field));
                $.ajax({
                    url: 'sell_new',
                    type: 'post',
                    data: data.field,
                    dataType: 'json',
                    success: function (data) {
                        layer.closeAll();
                        cancel()
                        getSellList(1, currPage)
                    }
                })
                return false;
            });

            getSellList(1, currPage)

        })

        var currPage = 1;
        function getSellList(page, currPage) {
            $.ajax({
                url: 'sell_view',
                type: 'post',
                data: { page: page },
                dataType: 'json',
                success: function (data) {
                    if (data.dat.length) {
                        $('.fenpei-bottom').show();
                        var html = '';
                        var dat = data.dat;
                        for (var i = 0; i < dat.length; i++) {
                            var time = dat[i].time.split(' ');
                            var datMsg = JSON.stringify(dat[i])
                            html += '<div class="layui-col-xs6 layui-col-sm6 layui-col-md4 sellItem"><input type="hidden" class="currId" value="' + dat[i].id + '">' +
                                '<div class="userItem"><p class="username">' + dat[i].farm + '</p>' +
                                '<div class="userMsg"><img src="../static/imgs/Customer.png" class="fl" alt="">' +
                                '<div class="userInfo"><p>商品名称： ' + dat[i].name + '</p><p>出货数量： ' + dat[i].num + '(公斤)</p><p>金额/元： ' + dat[i].money + '</p>' +
                                '<p>时间： ' + time[0] + '</p></div></div><div class="operateDiv">' +
                                '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick=\'edit(' + datMsg + ')\'>修改</button>' +
                                '<button type="button" class="layui-btn layui-btn-sm layui-btn-danger" onclick="del(' + dat[i].id + ')">删除</button>' +
                                '</div></div></div>';
                            if (i == dat.length - 1) {
                                $('#sellList').html(html);
                            }
                        }
                        // 设计分页
                        setPage(data.nums, currPage);
                    } else {
                        var html = '<div class="layui-col-xs12 " style="line-height:100px;text-align:center">暂无数据</div>'
                        $('#sellList').html(html);
                        $('.fenpei-bottom').hide();
                    }
                }
            })
        }


        function setPage(totalPage, curr) {
            laypage.render({
                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                , count: totalPage
                , curr: curr
                , limit: 9
                , theme: '#2ca9f9'
                , layout: ['prev', 'page', 'next', 'skip']
                , jump: function (obj, first) {
                    if (!first) {
                        currPage = obj.curr;
                        getSellList(obj.curr, currPage);
                    }
                }
            });
        }

        function edit(dat) {
            form.val("sellForm", {
                "farm": dat.farm
                , "name": dat.name
                , "num": dat.num
                , "money": dat.money
                , "time": dat.time
                , "id": dat.id
            })
            selectModelFun('编辑')
        }

        function del(id) {
            layer.confirm('是否删除此项', {
                btn: ['确定', '取消'] //按钮
                , icon: 0
            }, function () {
                layer.closeAll();
                $.ajax({
                    url: 'sell_del',
                    type: 'post',
                    data: { id: id },
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            getSellList(currPage);
                        } else {
                            layui.use(['layer'], function () {
                                layer = layui.layer;
                                layer.msg('删除失败！')
                            })
                        }
                    }
                })

            });


        }



        function selectModelFun(title) {
            layui.use(['layer'], function () {
                layer = layui.layer;
                var index = layer.open({
                    type: 1,
                    title: title,
                    content: $('#selectModel'),
                    area: ['500px', '450px'],
                    cancel: function (index, layero) {
                        cancel()
                    }
                })
            })

        }

        function cancel() {
            form.val("sellForm", {
                // "farm": ""
                "name": ""
                , "num": ""
                , "money": ""
                , "time": ""
                , "id": ""
            })
            layer.closeAll();
        }
    </script>
</body>

</html>