<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" />
<meta name="author" content="siweiyong 2602812659@qq.com"/>
<title>设置商品</title>
<link rel="stylesheet" href="../script/lib/miniui/themes/default/miniui.css">
<link rel="stylesheet" href="../script/lib/layui/css/layui.css" />
<link rel="stylesheet" href="../script/lib/cashier/common.css">
<link rel="stylesheet" href="../script/lib/layui/admin/modules/plugin/formselects/formselects.css" media="all" />
<script src="../script/lib/jquery/jquery.min.js"></script>
<script src="../script/lib/miniui/miniui.js"></script>
<script src="../script/lib/layui/layui.js"></script>
<script><{Dever::script()}></script>
<script src="../script/dever/core.js"></script>
</head>
<body style="background: #F6F7F9;">
<div class="main">
<form class="layui-form" lay-filter="form">
    <div class="layui-row">
        <div class="main_left layui-col-xs12 layui-col-md3">
            <div class="layui-row">
                <div class="top">
                    <i class="layui-icon layui-icon-app"></i>商品列表

                    <a class="mini-button" onclick="javascript:history.back()">返回上一页</a>
                </div>
                <div class="left_main">
                    <input id="key" class="mini-textbox" placeholder="请输入商品名称" onenter="onKeyEnter"/>
                    <a class="mini-button" onclick="search()">查询</a> 
                    <ul id="tree1" class="mini-tree" url="<{$url}>" showTreeIcon="true" textField="name" idField="id" expandOnLoad="true" onNodeClick="onNodeClick">
                    </ul>
                </div>
            </div>
        </div>
        <div class="main_right layui-col-xs12 layui-col-md9">
            <div class="layui-row">
                <div class="goods_list" style="height:645px;">
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th lay-data="{field:'name'}">商品名称</th>
                                <th lay-data="{field:'price', edit: 'text'}">价值(元)</th>
                                <th lay-data="{field:'number', edit: 'text'}">更改总数量</th>
                                <th lay-data="{field:'operation'}">操作</th>
                            </tr>
                        </thead>
                        <tbody id="goods">
                            
                        </tbody>
                    </table>
                </div>
                <div class="total ft16">
                    合计:【<span> 当前总数量:<i class="totalNum">0</i> </span>】
                </div>
                <table class="reading layui-table" lay-even="">
                    <tbody>
                        <tr>
                            <td>备注(本次调整说明):</td>
                            <td><textarea class="layui-textarea" name="desc" id="desc" rows="" cols="" maxlength="200" placeholder="请输入备注"></textarea></td>
                        </tr>
                    </tbody>
                </table>
                <div class="towbtn">
                    <input type="button" id="settlement" value="确认修改权益">
                </div>
            </div>
        </div>
    </div>
</div>
</form>
</div>
<script>
    var form;
    layui.config(
    {
        base: '../script/lib/layui/admin/' //静态资源所在路径
        ,version: true
    }).extend(
    {
        index: 'lib/index', //主入口模块
        formSelects: 'plugin/formselects/formselects'
    }).use(['index', 'contlist', 'table', 'form', 'formSelects','layer'], function()
    {
        var table = layui.table;
        form = layui.form; 
        var formSelects = layui.formSelects;
        form.render(); 
    });

    $(function()
    {
        $("#settlement").click(function(){
            var url = '<{$submit}>';
            var mid = '<{$mid}>';
            var search_api_mid = '<{$search_api_mid}>';

            layui.layer.confirm('确定进行此项设置吗?', function() {
                var desc = $('#desc').val();
                var goods_string = JSON.stringify(goods);

                var data = {};
                data = {mid:mid,search_api_mid:search_api_mid, desc:desc, goods:goods_string};
                $.post(url, data, function(t) {
                    t = JSON.parse(t);
                    if (t.status == 1) {
                        layui.layer.alert('权益调整成功', function(index){
                          location.href = t.data;
                          layer.close(index);
                        });
                    } else {
                        layui.layer.alert(t.msg);
                    }
                });
            });
        })

        $("#goods").on("keyup", ".goods_num", function() {
            var num = parseInt($(this).val());
            if (!num || num <= 0) {
                num = 0;
                $(this).val(0);
            }
            var id = $(this).parent().attr('value');
            goods[id].total = num;
            setTotal();
        });
    })
    var goods = {};
    var del_goods = {};
    mini.parse();
    var tree = mini.get("tree1");
    for (var i in tree.data) {
        if (typeof(tree.data[i].children) == 'object') {
            if (tree.data[i].children.length > 0) {
                for (var j in tree.data[i].children) {
                    if(tree.data[i].children[j].select == 1) {
                        setGoods(tree.data[i].children[j]);
                    }
                }
            } else if(tree.data[i].select == 1) {
                setGoods(tree.data[i]);
            }
        }
    }
    function search() 
    {
        var key = mini.get("key").getValue();
        if (key == "") {
            tree.clearFilter();
        } else {
            key = key.toLowerCase();                
            tree.filter(function (node) {
                var name = node.name ? node.name.toLowerCase() : "";
                if (name.indexOf(key) != -1) {
                    return true;
                }
            });
        }
    }
    function onKeyEnter(e)
    {
        search();
    }

    function onNodeClick()
    {
        var tree=mini.get("tree1");
        node=tree.getSelectedNode();
        if(node.end) {
            setGoods(node);
        }
    }

    function setGoods(node)
    {
        if (node.id) {
            var key = node.id;
            if (!goods[key] || (goods[key] && goods[key].del == 2)) {
                goods[key] = node;
                goods[key].price = parseFloat(goods[key].price);
                goods[key].price_type = parseInt(goods[key].price_type);
                goods[key].total = parseFloat(goods[key].total);
                goods[key].total = goods[key].total > 0 ? goods[key].total : 1;
                goods[key].num = 0;
                goods[key].del = 1;
                create(node);
                setTotal();
            } else if(node.price_type < 4) {
                add(key);
            }
        }
    }

    function get(key)
    {
        return $('#goods_' + key);
    }

    function create(node)
    {
        if (!get(node.id).length) {
            var addtr = '<tr class="mytr" id="goods_'+node.id+'">';
            //addtr += '<td class="goods_id">'+node.id+'</td>';
            addtr += '<td class="goods_name">'+node.name+'</td>';     
            addtr += '<td class="goods_price">'+node.price+'</td>';     
            if (node.id.indexOf('_2') != -1 && node.id.indexOf('-') != -1) {
                addtr += '<td><input type="hidden" value="'+node.total+'" class="goods_num">'+node.total+'</td>'; 
            } else {
                addtr += '<td><div class="jiajian" value='+node.id+'><span class="jian" onclick="dec(\''+node.id+'\')">-</span><input type="text" value="'+node.total+'" class="goods_num"><span class="jia" onclick="add(\''+node.id+'\')">+</span></div></td>'; 
            }
            
            if (node.total <= 0) {
                addtr += '<td><a class="delete_btn" onclick="del(\''+node.id+'\')">删除</a></td>';
            } else {
                addtr += '<td></td>';
            }
                                  
            addtr += '</tr>';
            $("#goods").append(addtr);
        }
    }

    function add(id)
    {
        if (id.indexOf('_2') != -1 && id.indexOf('-') != -1) {
            
        } else {
            goods[id].total += 1;
            get(id).find('.goods_num').val(goods[id].total);
            setTotal();
        }
    }
    
    function dec(id)
    {
        goods[id].total -= 1;
        if (goods[id].total < 1) {
            goods[id].total = 0;
        }

        get(id).find('.goods_num').val(goods[id].total);
        setTotal();
    }

    function del(id)
    {
        if (confirm('确定删除吗?')) {
            goods[id].del = 2;
            get(id).remove();
            setTotal();
        }
    }

    function setTotal()
    {
        var cash = 0;
        var num = 0;
        var total = 0;
        for (var i in goods) {
            //num += goods[i].num;
            total += goods[i].total;
        }
        //$(".totalQuantity").html(num);
        $(".totalNum").html(total);
    }

    function setShop()
    {
        var give = $('#give').val();
        if (!give) {
            layui.layer.alert('请选择赠送类型');
            return;
        }
        layui.layer.confirm('确定切换赠送类型吗?切换后现在选择的商品都将清空', function() {
            location.href = '<{$host}>&give=' + give;
        });
    }
</script>
</body>
</html>