<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>备件手动入库</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <style>
        html{
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-form-admin">
    <div class="layui-card md-0">
        <div class="layui-card-body">
            <table class="layui-table ">
                <colgroup>
                    <col width="13%">
                    <col width="13%">
                    <col width="13%">
                    <col width="10%">
                    <col width="10%">
                    <col width="10%">
                    <col width="10%">
                    <col width="20%">
                </colgroup>
                <thead>
                <tr>
                    <th>物料号</th>
                    <th>备件名称</th>
                    <th>备件型号</th>
                    <th>计量单位</th>
                    <th>入库数量</th>
                    <th>服务商价格</th>
                    <th>库位</th>
                    <th>入库备注</th>
                </tr>
                </thead>
                <tbody id="zhijiepaidan">
                <c:forEach items="${list}" var="si">
                    <tr>
                        <td>${si.columns.code}<input type="hidden" name="fittingId" class="fittingId"
                                                     value="${si.columns.id}"></td>
                        <td>${si.columns.name}</td>
                        <td>${si.columns.version}</td>
                        <td>${si.columns.unit_name}</td>
                        <td><input type="text" name="amount" class="layui-input amount" autocomplete="off"></td>
                        <td><input type="text" name="sitePrice" class="layui-input sitePrice" autocomplete="off"
                                   value="${si.columns.site_price}"></td>
                        <td><input type="text" name="storehouse" class="layui-input" autocomplete="off"
                                   value="${si.columns.storehouse}"></td>
                        <td><input type="text" name="remarks" class="layui-input" autocomplete="off"></td>
                    </tr>
                </c:forEach>

                </tbody>
            </table>
        </div>
    </div>
    <div class="operate-footer">
        <button type="button" class="layui-btn" id="tijiaoBtn" onclick="addFitting()">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" onclick="cancels()">取消</button>
    </div>
</div>


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

    layui.use(['form', 'jquery','laydate','upload'], function () {
        $ = layui.$
            , element = layui.element
            , form = layui.form
            , laydate = layui.laydate;

    });


//手动入库
    var noRepeatSub = false;
    function addFitting(){
        if(noRepeatSub){
            return;
        }
        var checkTe = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
        var fick = false;
        var data1 = {};

       $("#zhijiepaidan tr").each(function(i){
           var fiId = $(this).find(".fittingId").val();
           var warning = $(this).find(".amount").val();
           var sitePrice = $(this).find(".sitePrice").val();//入库价格
           var storehouse = $(this).find("input[name='storehouse']").val();//入库价格
           var remarks = $(this).find("input[name='remarks']").val();//入库价格
           if(isBlank(warning)){
               layer.msg("请输入第" + (i + 1) + "行的入库数量！", {icon: 5, anim: 6});
               $(this).find(".amount").focus();
               fick = true;
               return;
           }
           if(!checkTe.test(warning)){
               layer.msg("第" + (i + 1) + "行的入库数量格式有误！", {icon: 5, anim: 6});
               $(this).find(".amount").focus();
               fick = true;
               return;
           }
           if(isBlank(sitePrice)){
               layer.msg("请输入第" + (i - 1) + "行的最新入库价格！", {icon: 5, anim: 6});
               $(this).find(".sitePrice").focus();
               fick = true;
               return;
           }
           if(!checkTe.test(sitePrice)){
               layer.msg("第" + (i + 1) + "行的最新入库价格格式有误！", {icon: 5, anim: 6});
               $(this).find(".sitePrice").focus();
               fick = true;
               return;
           }
           var entity = {};
           entity.fitId = fiId;
           entity.amount = warning;
           entity.sitePrice = sitePrice;
           entity.storehouse = storehouse;
           entity.remarks = remarks;
           data1[i]=entity;
       });
        if(fick){
            return
        }
        var data = {};
        data.data1 = JSON.stringify(data1);
        if(!$("#tijiaoBtn").hasClass("layui-btn-disabled")){
            console.log("layui-btn-disabled")
            $("#tijiaoBtn").addClass("layui-btn-disabled");
        noRepeatSub = true;
            $.ajax({
                url: '${ctx}/platform/fiting/fittingInstockSave',
                type: 'post',
                data: data,
                success: function(result) {
                    if(result.code=="200"){
                        layer.msg("保存成功！",{time:1000},function(){
                            $("#tijiaoBtn").removeClass("layui-btn-disabled");
                            parent.layer.closeAll()
                            parent.window.location.reload();
                        });
                    }else if(result.code=="203"){
                        layer.msg(result.errMsg, {icon: 2});
                    }else{
                        layer.msg("保存失败！");
                    }
                },
                complete:function(){
                    noRepeatSub = false;
                }
            });

}
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

    function cancels(){
        parent.layer.closeAll();
    }


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