<%@ 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_BHSQ_BHSQ_T" html='<li class="layui-this sf-tab"><a href="${ctx}/fittingStockUp/fittingStockUpPage">备货申请</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">
                        <select name="status">
                            <option value="">请选择</option>
                            <option value="0">待提交</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">
                        <input type="text" name="number"  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">
                        <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-lg2 layui-col-sm3">
                    <label class="layui-form-label">申请人：</label>
                    <div class="layui-input-block">
                        <input type="text" name="createName"  autocomplete="off" class="layui-input">
                    </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="importFittingStock" lay-event="importFittingStock"><i class="layui-icon layui-icon-add-1"></i>导入备货申请</button>
                <button class="layui-btn layui-btn-sm" lay-event="export" id="exportBtn"><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
                });
            }
        });
        var ppTag = '${fns:checkPagePermission(request, "GYSDD_BHSQ_BHSQ_PP_B")}';
        tableExt.render({
            elem: '#test-table-reload'
            ,toolbar : '#toolbarDemo'
            , height: 'full-180'
            ,defaultToolbar : false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,url: '${ctx}/fittingStockUp/fittingStockUpList'
            ,cols: [[
                {type: 'numbers', title: '序号' ,fixed: 'left'}
                ,{field : 'id', checkbox : true,fixed: 'left' }
                ,{field:'id', title: '操作',align: 'center',minWidth: 180,fixed: 'left' , templet: function (d) {
                        var htm="";
                        var fg=d.audit;
                        if(d.status == '1'){
                            if("1"==fg){
                                htm+= '<a class="layui-table-opt-link" lay-event="audit">审批</a>';
                            }
                        }else if(d.status == '0' || (d.status == '2' && d.create_by == '${userId}')){
                            htm+='<a class="layui-table-opt-link ml-10" lay-event="edit">编辑</a>&nbsp;&nbsp;';
                        }else if(d.status == '3'){
                            if(ppTag =='0'){
                                htm+='<a class="layui-table-opt-link ml-10" lay-event="mateFitting">匹配</a>&nbsp;&nbsp;';
                            }
                        }
                        return htm;
                    }}
                ,{field:'number', title: '申请编号',align: 'center',minWidth: 180,fixed: 'left' ,exportable:true
                    ,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;
                        }
                    }
                    ,expFormat: function (d){
                        return d.number;
                    }
                }
                ,{field:'status', title: '申请状态',align: 'center',minWidth: 120,exportable:true
                    ,templet:function(d){
                        if('0'=== d.status){
                            return "待提交";
                        }else if('1'=== d.status){
                            return "待审核";
                        }else if('2' === d.status){
                            return "审核驳回";
                        }else if('3' === d.status){
                            return "待匹配";
                        }else if('4' === d.status){
                            return "已完成";
                        }else{
                            return "";
                        }
                    }, expFormat: function (d){
                        if('0'=== d.status){
                            return "待提交";
                        }else if('1'=== d.status){
                            return "待审核";
                        }else if('2' === d.status){
                            return "审核驳回";
                        }else if('3' === d.status){
                            return "待匹配";
                        }else if('4' === d.status){
                            return "已完成";
                        }else{
                            return "";
                        }
                    }
                }
                ,{field:'inWarehouseName', title: '备货仓库',align: 'center',minWidth: 110,exportable:true}
                ,{field:'total_apply_amount', title: '备货数量',align: 'center',minWidth:110,exportable:true}
                ,{field:'create_name', title: '申请人',align: 'center',minWidth: 110,exportable:true}
                ,{field:'createTime', title: '申请时间',align: 'center',minWidth: 180,exportable:true}
            ]]
            ,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}/fittingStockUp/addFittingStockUpPage',
                    })
                    break;
                case 'importFittingStock':
                    layer.open({
                        type: 2,
                        title:"导入备货申请",
                        resize:false,
                        area: ['40%', '43%'],
                        content: '${ctx}/fittingStockUp/toImportFittingStockPage'
                    });
                    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}/fittingStockUp/fittingStockUpDetailPage?id=' + id+'&from=audit',
                    area: ['80%', '90%'],
                });
            }else if(obj.event ==='edit'){
                layer.open({
                    title:'编辑',
                    type: 2,
                    area: ['80%', '80%'],
                    fixed: false,
                    content: '${ctx}/fittingStockUp/addFittingStockUpPage?id=' + id,
                   /* success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }*/
                })
            }else if(obj.event ==='mateFitting'){
                layer.open({
                    title:'匹配',
                    type: 2,
                    area: ['80%', '80%'],
                    fixed: false,
                    content: '${ctx}/fittingStockUp/mateFitting?id=' + id,
                })
            }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}/fittingStockUp/fittingStockUpDetailPage?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>
