<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE HTML>
<html>
<head>
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.css" media="all">
    <style>html {
        background-color: #fff;
        padding-bottom: 50px;
    }

    td, th {
        text-align: center !important;
    }
    .hh{
        line-height: 34px;
        margin: 0 0 0 20px;
        padding: 0 0 0 15px;
        font-size: 16px;
    }

    hr {
        margin:0px
    }
    .tot{
        margin: 6px 0px 0px 20px;
    }

    </style>
</head>

<body>
<div class="layui-form">
    <div class="layui-card">
        <span class="hh" >平台库存信息</span>
        <hr>
        <div class="tot">总申请数量：${amount}</div>
        <div class="layui-col-xs12 mt-10 mb-10 pr-30 pl-30">
            <table class="layui-table">
                <thead>
                <th>序号</th>
                <th>物料代码</th>
                <th>物料名称</th>
                <th>物料类型</th>
                <th>物料品牌</th>
                <th>物料规格</th>
<%--                <th>库存数量</th>--%>
                <th>零售单价</th>
                <c:if test="${'bh' eq from}">
                  <th>匹配数量</th>
                </c:if>
                <c:if test="${'bh' ne from}">
                    <th>发货数量</th>
                </c:if>
                <th>选择</th>
                </thead>
                <tbody id="materialTbody">
                <c:forEach items="${platStock}" var="applyDetail" varStatus="vs">
                    <tr data-materialId="${applyDetail.columns.id}">
                        <td>${vs.count}</td>
                        <td class="code">${applyDetail.columns.code}</td>
                        <td class="materialName">${applyDetail.columns.material_name}</td>
                        <td class="typeName" data-value="${applyDetail.columns.material_type_id}">${applyDetail.columns.typeName}</td>
                        <td class="materialBrand">${applyDetail.columns.material_brand}</td>
                        <td class="materialSpec" data-value="${applyDetail.columns.unit_name}">${applyDetail.columns.material_specs}</td>
<%--                        <td>${applyDetail.columns.warning}</td>--%>
                        <td class="salePrice"  data-minAmount="${applyDetail.columns.min_apply_amount}" data-value="${applyDetail.columns.cost_price}">${applyDetail.columns.qq_price}</td>
                        <c:if test="${platStock.size() == 1}">
                            <td> <input class="layui-input outAmount" value="${amount}"  onkeyup="value=(value.replace(/\D/g,'')==''?'':parseInt(value))"> </td>
                            <td>  <input type="checkbox" checked name="confirmChoice"  lay-skin="primary" value="${applyDetail.columns.material_id}"> </td>
                        </c:if>
                        <c:if test="${platStock.size() != 1}">
                            <td> <input class="layui-input outAmount"  onkeyup="value=(value.replace(/\D/g,'')==''?'':parseInt(value))"> </td>
                            <td>  <input type="checkbox" name="confirmChoice"  lay-skin="primary" value="${applyDetail.columns.material_id}"> </td>
                        </c:if>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
   <c:if test="${'bh' ne from}">
       <div class="layui-card">
           <span class="hh" >代理商库存信息</span>
           <hr>
           <div class="layui-col-xs12 mt-10 mb-10 pr-30 pl-30">
               <table class="layui-table">
                   <thead>
                   <th>序号</th>
                   <th>物料代码</th>
                   <th>物料名称</th>
                   <th>物料类型</th>
                   <th>物料品牌</th>
                   <th>物料规格</th>
                   <th>库存数量</th>
                   <th>零售单价</th>
                   </thead>
                   <tbody id="materialTbody2">
                   <c:forEach items="${tradeStock}" var="applyDetail" varStatus="vs">
                       <tr data-id="${applyDetail.columns.id}"
                           data-materialId="${applyDetail.columns.id}"
                           data-applyAmount="${applyDetail.columns.apply_amount}">
                           <td>${vs.count}</td>
                           <td>${applyDetail.columns.code}</td>
                           <td>${applyDetail.columns.material_name}</td>
                           <td>${applyDetail.columns.typeName}</td>
                           <td>${applyDetail.columns.material_brand}</td>
                           <td>${applyDetail.columns.material_specs}</td>
                           <td>${applyDetail.columns.warning}</td>
                           <td>${applyDetail.columns.qq_price}</td>
                       </tr>
                   </c:forEach>
                   </tbody>
               </table>
           </div>
       </div>
   </c:if>

    <div class="operate-footer">
        <button type="button"  id="confirmChoice" class="layui-btn" onclick="confirmChoice();">确定</button>
        <button type="button" class="layui-btn layui-btn-primary " onclick="closeDiv();">取消</button>
    </div>
</div>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    //初始数据
    var $form;
    var form;
    var $;
    var imgs = '';
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['form', 'layedit', 'laydate', 'jquery', 'layer', 'element', 'upload'], function () {
        $ = layui.jquery;
        form = layui.form;
        var element = layui.element;
        $form = $('form');
    })

    function confirmChoice(){
        if($("input[type=checkbox]:checked").length==0){
            layer.msg("请选择匹配的物料!");
            return;
        }

        var materials=[];
        var fg=true;
        var outAmountTotal = 0;
        $("input[type=checkbox]:checked").each(function (){
            var obj= $(this).parents('tr');

            var outAmount=obj.find(".outAmount").val();
            if (isBlank(outAmount)) {
                fg = false;
            } else {
                outAmountTotal = Number(outAmountTotal) + Number(outAmount);
            }

            var materialId=obj.attr("data-materialId");
            var costPrice = 0;
            //请求 查询 这个物料的成本价
            $.ajax({
                type:"post",
                url:"${ctx}/material/order/findPurchasePrice",
                async:false,
                data:{
                    materialId:materialId
                },
                success:function(result){
                    console.log("result:"+result);
                    costPrice = result;
                }
            })
            var code=obj.find(".code").text();
            var materialName=obj.find(".materialName").text();
            var typeName=obj.find(".typeName").text();
            var materialBrand=obj.find(".materialBrand").text();
            var materialSpec=obj.find(".materialSpec").text();
            var salePrice=obj.find(".salePrice").text();
            // var costPrice=obj.find(".salePrice").attr("data-value");
            var minApplyAmount=obj.find(".salePrice").attr("data-minAmount");
            var unitName=obj.find(".materialSpec").attr("data-value");
            var materialTypeId=obj.find(".typeName").attr("data-value");
            var obData={
                materialId:materialId
                ,code:code
                ,materialName:materialName
                ,typeName:typeName
                ,materialBrand:materialBrand
                ,materialSpec:materialSpec
                ,salePrice:salePrice
                ,outAmount:outAmount
                ,costPrice:costPrice
                ,unitName:unitName
                ,materialTypeId:materialTypeId
                ,minApplyAmount:minApplyAmount
            }
            materials.push(obData);
        })
        if(!fg){
            layer.msg("请输入发货数量!");
            return;
        }
        if (outAmountTotal > ${amount} && ${'bh' ne from}) {
            layer.confirm('计划数量与订单不符，是否确认编辑订单!', function(index){
                parent.getMerchMaterial(materials);
                closeSelf();
            });
        }else if(outAmountTotal != ${amount} && ${'bh' eq from}){
            layer.msg("计划数量与订单不符!");
            return;
        } else {
            parent.getMerchMaterial(materials);
            closeSelf();
        }
    }

    function closeSelf(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    function closeDiv() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    }

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


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