<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html>
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
    <!--validate css-->
    <link rel="stylesheet" href="plugins/jquery-validation/css/validate.css">
    <!--fileinput css-->
    <link rel="stylesheet" href="plugins/bootstrap-fileinput/bootstrap-fileinput.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        td{
            padding:5px;
        }
    </style>
</head>
<!-- END HEAD -->
<body class="hold-transition skin-blue sidebar-mini">

<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">

        <section class="content-header">
            <h1>
                类型申请单管理
                <small>类型申请单</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>首页</a></li>
                <li><a class="active">类型申请单</a></li>
            </ol>
        </section>


        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <!-- general form elements disabled -->
                    <div class="box box-primary">
                        <form role="form" id="myForm">
                            <div class="box-body">
                                <input name="form-id" style="display: none;" value="$!{data.id}">
                                <table id="form-table" style="width: 100%;">
                                    <tr>
                                        <!--<td style="display: none;">所属单据</td>-->
                                        <!--<td><input type="text" style="display: none;" class="form-control"-->
                                                   <!--name="typeformName" readonly value="$!{data.typeformName}"></td>-->
                                        <td>申请人</td>
                                        <td><input type="text" class="form-control"
                                                   name="form-name" readonly value="$!{data.name}"></td>
                                        <td>职能部门</td>
                                        <td> <input type="text" class="form-control"
                                                    name="form-departmentName" readonly value="$!{data.dname}"></td>
                                    </tr>
                                    <tr>
                                        <td>申请时间</td>
                                        <td><input type="text" class="form-control"
                                                   name="form-appTime" readonly value="$!date.format('yyyy-MM-dd HH:mm',$!data.createTime)"></td>
                                        <td>审批状态</td>
                                        <td><input type="text" class="form-control"
                                                   name="form-status" readonly value="$!{data.statusName}"></td>
                                        <td>发放状态</td>
                                        <td> <input type="text" class="form-control"
                                                    name="form-issuingStateName" readonly value="$!{data.issuingStateName}"></td>
                                    </tr>
                                </table>
                                <p style="font-weight: bold;">申请列表</p>
                                <table id="appitems" style="border: #d2d6de 1px solid;width: 100%;">
                                    <thead style="    background: #F0F0F0;border-bottom: #d2d6de solid 1px;font-weight: bold;">
                                    <td>物品名称</td>
                                    <td>物品规格</td>
                                    <td>申请数量</td>
                                    <td>单价</td>
                                    <td>管理员剩余库存</td>
                                    <td>实际发放数量</td>
                                    <td>退库数量</td>
                                    </thead>
                                    <tbody>
                                    #foreach( $item in $items )
                                    <tr style='border-bottom: #d2d6de solid 1px;'
                                        data-price='${item.price}'
                                        data-id='${item.id}'
                                        data-appitem='${item.appitem}'
                                        data-num="${item.appCount}"
                                        data-itemname="${item.itemname}"
                                        data-realityGrantCount="$!{item.realityGrantCount}"
                                        data-managerGrantCount="$!{item.managerGrantCount}"
                                        data-disGrantCount="${item.disGrantCount}"
                                    >
                                        <td>${item.itemname}</td>
                                        <td>${item.specifications}</td>
                                        <td>${item.appCount}</td>
                                        <td>${item.price}</td>
                                        <td>${item.authenticStock}</td>
                                        <td>$!{item.realityGrantCount}</td>
                                        <td><input class='form-control'/></td>
                                    </tr>
                                    #end
                                    </tbody>
                                </table>
                                <textarea placeholder="请输入退库原因" #if($type == 1) readonly #end id="desc" style="width: 50%;margin-top: 15px;"></textarea>
                            </div>
                            <div class="box-footer">
                                #if($shiro.hasPermission("qy:typeAppForm:edit"))
                                <a href="javascript:void(0)" onclick="grantSubmit()" class="btn btn-primary">退库</a>
                                #end
                                <a href="#springUrl('/a/typeAppForm/list')" class="btn btn-default">取消</a>
                            </div>
                        </form>
                        <!-- /.box-body -->
                    </div><!-- /.box -->


                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->


    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<!--fileinput js-->
<script src="plugins/bootstrap-fileinput/bootstrap-fileinput.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script src="plugins/bootstrap-maxlength/bootstrap-maxlength.min.js" type="text/javascript"></script>
<script src="plugins/security/sha256.js" type="text/javascript"></script>
<script src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script src="js/jquery.form.min.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/ueditor.all.js"></script>
<script type="text/javascript" charset="utf-8" src="plugins/ueditor-min-1.4.3/lang/zh-cn/zh-cn.js"></script>
<script src="common/js/cfapp.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script>
    var csrfheader = {name: '_csrf_header', value: '${_csrf.headerName}'};
    var csrftoken = {name: '_csrf', value: '${_csrf.token}'};
</script>
<script>
    $().ready(function () {
        Cfapp.init();
        recdTypeAdd.init();



    });


    function grantSubmit(){
        var obj = $("#appitems tbody tr");
        var formData = {
            _csrf_header:csrfheader.value,
            _csrf:csrftoken.value,
            id:$("input[name='form-id']").val(),
        }
        var array = [];
        var items = eval($!{data.items});
        /**
         * 遍历原有json，填充计算最新发放数量
         */
        var isCancel = false;
        outer :for(var i=0;i<obj.length;i++){
            for(var k = 0;k<items.length;k++){
                if(items[k].id==$(obj[i]).attr("data-appitem")){
                    var o =items[k];
                    o.realityGrantCount =$(obj[i]).attr("data-realityGrantCount");//实际发放数量
                    o.managerGrantCount = $(obj[i]).attr("data-managerGrantCount")//管理员发放数量
                    array.push(o);
                    var cancelCount = $(obj[i]).find("input").val();//退库数量
                    if(!cancelCount || cancelCount == null || cancelCount =="" || cancelCount == '0'){
                        continue outer;
                    }
                    if(parseInt(o.realityGrantCount)<parseInt(cancelCount)){
                        Cfapp.alert({
                            message: "实际发放数量小于退库数量，无法进行退库",
                            btntext: "确定",
                            success: function () {
                            }
                        });
                        return false;
                    }else{
                        o.realityGrantCount = parseInt(o.realityGrantCount)-parseInt(cancelCount);
                        if(o.managerGrantCount && o.managerGrantCount!=null && o.managerGrantCount!=''){
                            o.managerGrantCount = parseInt(o.managerGrantCount)-parseInt(cancelCount);
                        }
                    }
                    formData["cancelitems["+i+"].grantId"]=$(obj[i]).attr("data-id");
                    formData["cancelitems["+i+"].appitem"]=o.id;
                    formData["cancelitems["+i+"].itemname"]=o.name;
                    formData["cancelitems["+i+"].price"]=o.price;
                    formData["cancelitems["+i+"].cancelCount"]=cancelCount;
                    formData["cancelitems["+i+"].realityGrantCount"]=o.realityGrantCount;
                    formData["cancelitems["+i+"].managerGrantCount"]=o.managerGrantCount;
                    formData["cancelitems["+i+"].description"]=$("#desc").val();

                    isCancel = true;
                }
            }

        }
        console.log(array);
        if(!isCancel){
            Cfapp.alert({
                message: "请正确填写退库数量后在进行退库",
                btntext: "确定",
                success: function () {
                }
            });
            return false;
        }
        formData.items = JSON.stringify(array);
        $.ajax({
            type: "post",
            url: "#springUrl('/a/typeAppForm/cancelSubmit')",
            data: formData,
            dataType: "json",
            success: function (data) {
                if (data.errorNo == 0) {
                    Cfapp.alert({
                        message: "退库成功",
                        btntext: "确定",
                        success: function () {
                            location.href = "#springUrl('/a/typeAppForm/list')";
                        }
                    });
                }
            },
            error: function () {
            }
        })
    }


    var recdTypeAdd = function () {
        var initForm = function () {
            var initFormCtrl = function () {
                bindEvent();
            };
            var bindEvent = function () {

                $("#myForm").validate({
                    rules: {},
                    messages: {},
                    submitHandler: function (form) {
                        $("#save").attr("disabled", true);
                        $.getJSON("#springUrl('/a/typeAppForm/formData')", $("#myForm").serialize(), function (returnobj) {
                            $("#save").attr("disabled", false);
                            if (returnobj.errorNo == 2) {  //保存成功
                                Cfapp.confirm({
                                    message: "添加成功",
                                    btnoktext: "继续添加",
                                    btncanceltext: "关闭",
                                    success: function () {
                                        location.href = "#springUrl('/a/typeAppForm/form')";
                                    },
                                    cancel: function () {
                                        location.href = "#springUrl('/a/typeAppForm/list')";
                                    }
                                });
                            } else if (returnobj.errorNo == 0) {  //修改成功
                                Cfapp.alert({
                                    message: "更新成功",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/typeAppForm/list')";
                                    }
                                });
                            } else {
                                Cfapp.alert({
                                    message: "创建失败",
                                    btntext: "确定",
                                    success: function () {
                                        location.href = "#springUrl('/a/typeAppForm/list')";
                                    }
                                });
                            }
                        });
                    }
                })
            }
            initFormCtrl();
        }
        return {

            //main function to initiate the module
            init: function () {
                initForm();
            }

        };
    }();
</script>
</body>
<!-- END BODY -->
</html>
