<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../../common.jsp"/>
</head>
<body>
<div class="row animated fadeInRight" style="margin: 0;">
    <div class="wrapper wrapper-content mt5 right-content ml5 mr5">
        <div class="toolbar-panel bdd">
            <div class="toolbar-box">
                <div class="toolbar-head clearfix lgraybg pt5 pb10 pl10 bdbd">
                    <!-- 顶部按钮 -->
                    <div class="fl">
                        查询条件
                    </div>
                    <div class="tools fr">
                        <a href="javascript:void(0);" class="collapse dblock f14 dgray mr5">
                            <i class="fa-2x fa fa-angle-double-up"></i>
                        </a>
                    </div>
                </div>
            </div>
            <!--搜索条件 -->
            <div class="toolbar-body">
                <form class="layui-form" id="queryForm" action="" style="margin-top: 10px">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-block">
                                <input type="text" name="userId" id="userId" placeholder="请输入用户ID" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" id="name" placeholder="请输入商品名称" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">兑换人</label>
                            <div class="layui-input-block">
                                <input type="text" name="userName" id="userName" placeholder="请输入兑换人" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系方式</label>
                            <div class="layui-input-block">
                                <input type="text" name="phone" id="phone" placeholder="请输入联系方式" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">支付状态</label>
                            <div class="layui-input-block">
                                <select class="layui-select" name="status" id="status">
                                    <option value="">请选择</option>
                                    <option value="1">已支付</option>
                                    <option value="0">待支付</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">发货状态</label>
                            <div class="layui-input-block">
                                <select class="layui-select" name="deliveryStatus">
                                    <option value="">请选择</option>
                                    <option value="1">已发货</option>
                                    <option value="0">待发货</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- 表单数据 -->
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="officialGoodsExchangeTab" lay-filter="officialGoodsExchangeTab"></table>
        </div>
    </div>
</div>

<script type="text/html" id="leftBar">
    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="search"><i class="layui-icon">&#xe615;</i>查询
    </button>
</script>


<script type="text/html" id="toolBar">
    {{# if (d.status === 1)  {}}
    <a class="layui-btn layui-btn-xs" lay-event="deliverGoods"><i class="layui-icon">&#xe642;</i>确认发货</a>
    {{# } }}
</script>

<script type="text/template" id="deliverGoods">
    <form class="layui-form" id="form2" style="margin-top: 2rem;">
        <input name="id" id="id" hidden>
        <input name="deliveryStatus" id="deliveryStatus" hidden value="1">
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">发货单号</label>
            <div class="layui-input-block">
                <input type="text" name="expressNo" id="expressNo" style="cursor: pointer" placeholder="请输入发货单号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</script>

<script>
    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        let form = layui.form;

        var tableIns = table.render({
            elem: '#officialGoodsExchangeTab'
            , url: ctx + '/officialGoodsExchange/page'
            , id: 'officialGoodsExchange'
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //默数据列表的字段名称，认：data
                , statusCode: 200
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , toolbar: '#leftBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter']
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '商品兑换信息表'
            , cols: [[
                {type: 'checkbox', fixed: 'left', hidden: true}
                , {field: 'numbers', title: '序号', fixed: 'left', unresize: true, width: 80, type: 'numbers'}
                , {field: 'userId', title: '用户ID', width: 80}
                , {field: 'name', title: '商品名称'}
                , {
                    field: 'price', title: '商品价格', width: 90, templet: function (d) {
                        return d.price.toFixed(2);
                    }
                }
                , {field: 'userName', title: '兑换人', width: 100}
                , {field: 'phone', title: '联系方式', width: 130}
                , {field: 'address', title: '收货地址'}
                , {field: 'orderNumber', title: '订单号'}
                , {field: 'expressNo', title: '快递单号'}
                , {
                    field: 'status', title: '支付状态', width: 90, templet: function (d) {
                        if (d.status === 1) {
                            return "<span style='color: green'>已支付</span>"
                        } else {
                            return "<span style='color: red'>待支付</span>"
                        }
                    }
                }
                , {
                    field: 'deliveryStatus', title: '发货状态', width: 90, templet: function (d) {
                        if (d.deliveryStatus === 0) {
                            return "<span style='color: blue'>待发货</span>"
                        } else {
                            return "<span style='color: green'>已发货</span>"
                        }
                    }
                }
                , {
                    field: 'createTime',
                    title: '创建时间',
                    width: 160,
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作', toolbar: '#toolBar'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
        });
        table.on('tool(officialGoodsExchangeTab)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'deliverGoods') { //编辑
                // console.log(123);
                //do something
                deliverGoods(obj);
                //同步更新缓存对应的值
                // obj.update({
                //     username: '123'
                //     ,title: 'xxx'
                // });
            }
        });

        //监听头工具栏事件
        table.on('toolbar(officialGoodsExchangeTab)', function (obj) {
            switch (obj.event) {
                case 'search':
                    var params = serializeObj($('#queryForm'));
                    table.reload('officialGoodsExchange', {
                        where: params
                        , page: {
                            size: 10
                            , current: 1
                        }
                    });
                    break;
            }
        });

        function e(obj) {
            layer.confirm('确定' + (obj.status === '2' ? '上架' : '下架') + '当前商品么？', {icon: 3}, function (index) {
                var index = layer.load(1, {shade: [0.1, '#000']});
                $.ajax({
                    url: '${ctx}/officialGoods/saveOrUpdate',
                    method: 'post',
                    data: JSON.stringify(obj),
                    dataType: 'JSON',
                    contentType: "application/json",
                    success: function (res) {
                        layer.close(index);
                        if (res.code == 200) {
                            layer.msg(res.message, {
                                icon: 1
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(res.message, {
                                icon: 2
                            });
                        }
                    }
                })
            });
        }

        function deliverGoods(obj) {
            var index = layer.open({
                type: 1
                , shade: 0.5
                , title: '确认发货'
                , maxmin: true
                , content: $("#deliverGoods").html()
                , area: ['400px', '200px']
                , btn: ['保存', '取消',]
                , shadeClose: true
                , btn1: function (index, layero) {
                    if (!$("#expressNo").val()) {
                        return layer.msg("请输入发货单号!");
                    }
                    saveDeliverGoods(obj, index);
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
                , success: function () {
                    $("#id").val(obj.data.id);
                }
            });
            form.render('select');
        }

        function saveDeliverGoods(obj, index) {
            var loadindex = layer.load(1, {shade: [0.1, '#000']});
            var data = serializeObj($("#form2"));
            $.ajax({
                url: ctx + '/officialGoodsExchange/save',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    layer.close(loadindex);
                    console.log(obj, "12323");
                    if (res.code == 200) {
                        layer.close(index);
                        layer.msg(res.message, {
                            icon: 1
                        });
                        obj.update({
                            expressNo: $("#expressNo").val(),
                            deliveryStatus: $("#deliveryStatus").val()
                        });
                    } else {
                        layer.msg(res.message, {
                            icon: 2
                        });
                    }
                }
            })
        }
    });

    /*function openImg(obj) {
        var url = $(obj).text();
        if(url){
            var img = new Image();
            img.src = url;
            //var height = img.height + 50; // 原图片大小
            //var width = img.width; //原图片大小
            var imgHtml = "<img src='" + url + "' width='500px' height='500px'/>";
            //弹出层
            layer.open({
                type: 1,
                shade: 0.8,
                offset: 'auto',
                area: [500 + 'px',550+'px'],  // area: [width + 'px',height+'px']  //原图显示
                shadeClose:true,
                scrollbar: false,
                title: "预览", //不显示标题
                content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                cancel: function () {
                    //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
                }
            });
        }
    }*/
</script>
</body>
</html>
