<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <title>采购审批</title>
    <meta name="decorator" content="base" />
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/formSelects-v4.css" media="all">
    <style>
        .layui-card{box-shadow: none;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab  layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="GYSDD_CGSP_CGSP_T" html='<li class="layui-this sf-tab"><a href="${ctx}/supplyOrder/purchaseAudit/purchaseAuditPage">采购审批</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 "  id="searchBtn"  lay-submit lay-filter="LAY-user-back-search" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="number"  autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单来源：</label>
                    <div class="layui-input-block">
                        <select name="type"  >
                            <option value="">请选择</option>
                            <option value="1">电站下单</option>
                            <option value="2">物料储备</option>
                            <option value="3">平台采购</option>
                            <option value="4">合并发货</option>
                        </select>
                    </div>
                </div>

                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单类型：</label>
                    <div class="layui-input-block">
                        <select name="pattern"  >
                            <option value="">请选择</option>
                            <option value="1">平台采购</option>
                            <option value="2">供应商直发</option>
                        </select>
                    </div>
                </div>

                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">供应商名称：</label>
                    <div class="layui-input-block">
                        <select name="supplyId">
                            <option value="">请选择</option>
                            <c:forEach items="${supplyList}" var="supply">
                                <option value="${supply.columns.id}">${supply.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单备注：</label>
                    <div class="layui-input-block">
                        <input type="text" name="remarks"  autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">代理商名称：</label>
                    <div class="layui-input-block">
                        <select name="traderId" id="traderId" xm-select="status" xm-select-type="1" xm-select-height="25px" xm-select-show-count="6" >
                            <option value="">请选择</option>
                            <c:forEach items="${traderList}" var="trader">
                                <option value="${trader.columns.id}">${trader.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">下单时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMin" id="createTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMax" id="createTimeEnd" class="layui-input"  placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">分配时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="confirmTimeSta" id="confirmTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="confirmTimeEnd" id="confirmTimeEnd" class="layui-input"  placeholder="结束日期">
                        </div>
                    </div>
                </div>


            </div>
        </div>
    </div>

    <div class='layui-card'>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test-table-reload" lay-filter="table-toolbar"></table>
            <script type="text/html" id="toolbarDemo">
                <sfTags:flowableAddPermission formId="${formId}" html='<button class="layui-btn layui-btn-sm" id="add" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增采购单</button>'/>
                <button class="layui-btn layui-btn-sm" id="export" lay-event="export" ><i class="layui-icon layui-icon-export"></i>导出</button>
            </script>

        </div>
    </div>

</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/lib/formSelects-v4.js"></script>

<script type="text/javascript">
    var table;
    var $;
    var formSelects;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laydate','form','formSelects'], function(){
        var tableExt = layui.tableExt;
        var layer = layui.layer;
        $ = layui.jquery;
        var laydate = layui.laydate;
        var  form = layui.form;
        formSelects = layui.formSelects;

        table = tableExt.getLayTable();

        //下单时间
        var insStart = laydate.render({
            elem: '#createTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });

        var insEnd = laydate.render({
            elem: '#createTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //确认时间
        var confirmStart = laydate.render({
            elem: '#confirmTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                confirmEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                confirmEnd.config.elem[0].focus();
            }
        });

        var confirmEnd = laydate.render({
            elem: '#confirmTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                confirmStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        tableExt.render({
            elem: '#test-table-reload'
            ,toolbar : '#toolbarDemo'
            , height: 'full-218'
            ,defaultToolbar : false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,url: '${ctx}/supplyOrder/purchaseAudit/purchaseAuditList?status=4,b'
            ,cols: [[
                {type: 'numbers', title: '序号' ,fixed: 'left'}
                ,{field : 'id', checkbox : true,fixed: 'left' }
                ,{field:'id', title: '操作',align: 'center',minWidth: 140,fixed: 'left' , templet: function (d) {
                        var htm="";
                        var fg=d.audit;
                        if(d.purchase_order_examine_status == '1'){
                             if("1"==fg){
                                 htm+= '<a class="layui-table-opt-link" lay-event="audit">审批</a>';
                             }
                        }else if(d.orderStatus == 'b' || (d.purchase_order_examine_status == '0' && d.create_by == '${userId}')){
                            htm+='<a class="layui-table-opt-link ml-10" lay-event="edit">编辑</a>&nbsp;&nbsp;';
                        }
                        if(d.purchase_order_examine_status != '3' && d.type =='3'  && d.create_by == '${userId}'){
                            htm+='&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="delete">撤销采购</a>';
                        }
                        return htm;
                    }}
                ,{field:'number', title: '订单编号',align: 'center',width: 180,fixed: 'left'
                    ,templet:function(d){
                        if(d.lookAuth=='1' || d.audit=='1'){
                            return '<a href="#" class="layui-table-link" onclick="orderDetail(\''+d.id+'\')">'+d.number+'</a>'
                        }else {
                            return d.number;
                        }
                    }
                }
                ,{field:'task_name', title: '审批状态',align: 'center',fixed:'left',width: 180,exportable:true}
                ,{field:'supplyName', title: '供应商名称',align: 'center',width: 110}
                ,{field:'address', title: '物料清单',align: 'center',minWidth: 90
                    ,templet:function(d){
                        return '<a href="#" class="layui-table-link" onclick="materialDetail(\''+d.id+'\')">查看</a>';
                    }
                }
                ,{field:'inWarehouseName', title: '入库仓库',align: 'center',width:110}
                ,{field:'allocationTime', title: '分配时间',align: 'center',width: 180 }
                ,{field:'allocation_remark', title: '分配说明',align: 'center',width: 180}
                ,{field:'type', title: '订单来源',align: 'center',width: 120
                    ,templet:function(d){
                    if('1'=== d.type){
                        return "电站下单";
                    }else if('2' === d.type){
                        return "物料储备";
                    }else if('3' === d.type){
                        return "平台采购";
                    }else if('4' === d.type){
                        return "合并发货";
                    }else{
                        return "";
                    }
                    }, expFormat: function (d){
                        if('1'=== d.type){
                            return "电站下单";
                        }else if('2' === d.type){
                            return "物料储备";
                        }else if('3' === d.type){
                            return "平台采购";
                        }else if('4' === d.type){
                            return "合并发货";
                        }else{
                            return "";
                        }
                    }
                }
                ,{field:'pattern', title: '订单类型',align: 'center',width: 90
                    ,templet:function(d){
                        if('1'=== d.pattern){
                            return "平台采购";
                        }else if('2' === d.pattern){
                            return "供应商直发";
                        }else if('3' === d.pattern){
                            return "平台发货";
                        }else{
                            return "";
                        }
                    }, expFormat: function (d){
                        if('1'=== d.pattern){
                            return "平台采购";
                        }else if('2' === d.pattern){
                            return "供应商直发";
                        }else if('3' === d.pattern){
                            return "平台发货";
                        }else{
                            return "";
                        }
                    }
                }
                ,{field:'outWarehouseName', title: '发货仓库',align: 'center',width: 110}
                ,{field:'traderName', title: '代理商名称',align: 'center',width: 110}
                ,{field: 'person_in_charge',title: '接货人',sort: false,align: 'center',exportable: true,minWidth: 120}
                ,{field: 'deliverMobile',title: '联系电话',sort: false,align: 'center',exportable: true,minWidth: 120}
                ,{field: 'warehouseAddress',title: '仓库地址',sort: false,align: 'center',exportable: true,minWidth: 150}
                ,{field:'createTime', title: '下单时间',align: 'center',width: 180}
                ,{field:'remarks', title: '订单备注',align: 'center',width: 110}

            ]]
            ,done: function(res, curr, count){

            },exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '采购审批',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '采购审批',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(table-toolbar)',function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event){
                case 'add':
                    layer.open({
                        title:'新增',
                        type: 2,
                        area: ['80%', '80%'],
                        fixed: false,
                        content: '${ctx}/supplyOrder/purchaseAudit/addPurchasePage',
                        success: function (layero, index) {
                            childWin = window[layero.find('iframe')[0]['name']];
                        }
                    })

                    break;
                case 'export':
                    location.href='${ctx}/purchaseManage/exportPurchaseOrder?name=采购单&' + $('#searchForm').serialize();
                    break;
            }
        })

        //监听行工具事件
        table.on('tool(table-toolbar)', function(obj){
            var data = obj.data;
            var id = data.id;
            if(obj.event === 'cancel'){
                layer.prompt({
                    formType: 2,
                    title: '撤销原因',
                    area: ['380px', '160px'],
                    btnAlign: 'c',
                    id: 'prompt',
                    yes: function (index, layero) {
                        // 获取文本框输入的值
                        var value = layero.find(".layui-layer-input").val();
                        $.ajax({
                            type:"POST",
                            url:"${ctx}/purchaseManage/cancelPurchaseOrder",
                            async:false,
                            data:{
                                id:id,
                                remark:value
                            },
                            success:function(result){
                                if(result.code =='200'){
                                    layer.msg("保存成功！")
                                    reload();
                                }else{
                                    layer.msg("保存失败！")
                                }
                                layer.close(index);//关闭弹框
                            }
                        })

                    }
                });
            } else if(obj.event === 'audit'){
                layer.open({
                    type: 2,
                    title: '审批',
                    content: '${ctx}/supplyOrder/purchaseAudit/auditPurchaseOrderPage?id=' + id+'&from=audit',
                    area: ['80%', '90%'],
                });
            }else if(obj.event ==='edit'){
                layer.open({
                    title:'编辑',
                    type: 2,
                    area: ['80%', '80%'],
                    fixed: false,
                    content: '${ctx}/supplyOrder/purchaseAudit/editPurchasePage?id=' + id,
                    success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                })
            }else if(obj.event === 'delete'){
                layer.confirm('确认删除该流程？',{icon:3}, function(index){
                    $.post('${ctx}/supplyOrder/purchaseAudit/deletePurchaseOrder', {id: id}, function(result){
                        if("200" == result.code){
                            layer.msg("删除成功");
                            layer.close(index);
                            reload();
                        }else{
                            layer.msg(result.errMsg);
                        }
                    });
                });
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('test-table-reload', {
                where: field,
                page:{
                    curr:1
                }
            });
        });

        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
                //执行相关操作
                $("#searchBtn").click();
            }
        });

    });

    window.importMaterial = function (materials) {
        childWin.importMaterial(materials);
    };

    function orderDetail(orderId){
        layer.open({
            type: 2,
            title: '详情',
            content: '${ctx}/supplyOrder/purchaseAudit/auditPurchaseOrderPage?id=' + orderId+'&from=detail',
            area: ['80%', '90%'],
        });
    }

    function materialDetail(id) {
        layer.open({
            type: 2,
            title: "查看",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/order/orderMaterialList?id=' + id+'&from=purchase'
        });
    }

    function reload(){
        table.reload('test-table-reload', {
            page:{
                curr:1
            }
        });
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }

</script>
</body>
</html>
