<%@ 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;}
    </style>
</head>

<body>
<div class="layui-form">
    <div class="layui-card">


            <div class="layui-col-xs12 mb-10 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料代码：</label>
                    <div class="layui-input-block ">
                        <input type="text"  name="code" readonly autocomplete="off" placeholder="请输入" class="layui-input layui-bg-gray" value="${material.code}">
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料名称：</label>
                    <div class="layui-input-block  w-200">
                        <input type="text" name="materialName" lay-verify="required" autocomplete="off" placeholder="请输入"
                               class="layui-input" value="${material.materialName}">
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 mb-10 pr-30">

                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料类型：</label>
                    <div class="layui-input-block w-200" >
                        <select name="materialTypeId" id="materialTypeId" lay-verify="required" lay-filter="materialTypeId" >
                            <option value="">请选择</option>
                            <c:forEach items="${types}" var="ty">
                                <option value="${ty.columns.id}" <c:if test="${material.materialTypeId eq ty.columns.id}"> selected</c:if>  data-value="${ty.columns.attribute}" >${ty.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料规格：</label>
                    <div class="layui-input-block  w-200">
                        <input type="text" name="materialSpecs" id="materialSpecs" lay-verify="required" autocomplete="off" placeholder="请输入"
                               class="layui-input" value="${material.materialSpecs}">
                    </div>
                </div>

            </div>
            <div class="layui-col-xs12 mb-10 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料型号：</label>
                    <div class="layui-input-block  w-200">
                        <input id="materialModel" type="text" name="materialModel" lay-verify="required" autocomplete="off" placeholder="请输入"
                               class="layui-input" value="${material.materialModel}">
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>物料品牌：</label>
                    <div class="layui-input-block w-200">
                            <select name="materialBrandId" id="materialBrandId" lay-verify="required" >
                                <option value="">请选择</option>
                                <c:forEach items="${brands}" var="brand">
                                    <option value="${brand.columns.id}" <c:if test="${material.materialBrandId eq brand.columns.id}"> selected</c:if>  data-value="${brand.columns.name}" >${brand.columns.name}</option>
                                </c:forEach>
                            </select>
                    </div>
                </div>

            <%--    <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>单位：</label>
                    <div class="layui-input-block w-200">
                        <input type="text" name="unit" lay-verify="required" autocomplete="off" placeholder="请输入"
                               class="layui-input" value="${material.unit}">
                    </div>
                </div>--%>
            </div>
            <div class="layui-col-xs12 mb-10 pr-30">
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130"><i class="red">*</i>库存单位：</label>
                    <div class="layui-input-block">
                        <select name="unitCode" id="unitCode" lay-verify="required">
                            <option value="">请选择</option>
                            <c:forEach items="${unitList}" var="unit">
                                <option value="${unit.columns.unit_code}" <c:if test="${material.unitCode eq unit.columns.unit_code}"> selected</c:if> >${unit.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="layui-inline layui-col-xs6">
                    <label class="layui-form-label w-130" style="white-space: nowrap;"><i class="red">*</i>最小发货单位：</label>
                    <div class="layui-input-block  w-200" >
                        <input id="minApplyAmount" type="number" name="minApplyAmount" lay-verify="required" autocomplete="off" placeholder="请输入"
                               class="layui-input" value="${material.minApplyAmount}">
                    </div>
                </div>



            </div>


  <%--      <div class="layui-col-xs12 mb-10 pr-30 hide">
            <div class="layui-inline layui-col-xs6">
                <label class="layui-form-label w-130">金蝶属性：</label>
                <div class="layui-input-block">
                    <select name="materialProperties" id="materialProperties"  >
                        <option value="1" <c:if test="${material.materialProperties eq '1'}"> selected</c:if>>外购</option>
                        <option value="6" <c:if test="${material.materialProperties eq '6'}"> selected</c:if>>服务</option>
                    </select>
                </div>
            </div>
        </div>--%>
        <div class="layui-col-xs12 mb-10 pr-30">
            <div class="layui-inline layui-col-xs6">
                <label class="layui-form-label w-130"><i class="red">*</i>外部编码：</label>
                <div class="layui-input-block  w-200">
                    <input id="qqCode" type="text" name="qqCode" lay-verify="required" autocomplete="off" placeholder="请输入"
                           class="layui-input" value="${material.qqCode}">
                </div>
            </div>
        </div>

            <div class="layui-col-xs12 layui-form-item">
                <label class="layui-form-label">物料描述：</label>
                <div class="layui-input-block">
                    <textarea name="remarks" id="remarks"  placeholder="" class="layui-textarea"  style="width: 96%">${material.remarks}</textarea>
                </div>
            </div>
    </div>
    <div class="operate-footer">
        <input type="hidden" name="id" value="${material.id}">
        <input type="button" lay-submit lay-filter="confirm" id="confirm" class="layui-btn " value="确认">
        <input type="button"   value="取消" class="layui-btn layui-btn-primary " onclick="closeDiv();" >
    </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=/^\d+(\.\d{1,2})?$/;
    var reg = /\./;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['form', 'layedit', 'laydate','jquery','layer'], function () {
        $ = layui.jquery;
        form = layui.form;
        $form = $('form');
        //初始化，当物料类型为3（组件）时，显示组件玻型和电池参数
        var materialTypeId = $("#materialTypeId").find("option:selected").val();


        var saveFlag = false;
        form.on('submit(confirm)', function(data){
            if(saveFlag){
                return false;
            }
            var materialAttribute = $("#materialTypeId").find("option:selected").attr("data-value");
            data.field.materialAttribute = materialAttribute;

            var materialTypeName = $("#materialTypeId").find("option:selected").text();
            if('组件'==materialTypeName){   //物料类型为组件时，物料规格必须是纯数字。
                var spec=$("#materialSpecs").val();
                if(!myreg.test($.trim(spec))){
                    layer.msg("物料类型为组件时，物料规格必须是纯数字", {time: 3000});
                    return false;
                }
            }
            var materialBrand=$("#materialBrandId").find("option:selected").attr("data-value");
            data.field.materialBrand=materialBrand;

            var customerPrice = $("#customerPrice").val();
            // if (!isPrice(customerPrice)) {
            //     layer.msg("零售价格格式不正确！", {icon: 2});
            //     return false;
            // }
            var unitName = $("#unitCode").find("option:selected").text();
            data.field.unitName=unitName;

            var minApplyAmount = $("#minApplyAmount").val();
            if(reg.test($.trim(minApplyAmount))){
                layer.msg("物料最小发货单位必须是纯数字", {time: 3000});
                return false;
            }
            if(minApplyAmount <= 0){
                layer.msg("物料最小发货单位必须为正整数",{time:3000})
                return false;
            }
            data.field.minApplyAmount=minApplyAmount;

            var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
            saveFlag=true;
            $.ajax({
                url:'${ctx}/material/saveMaterial',
                method:'post',
                data:data.field,
                dataType:'JSON',
                success:function(res){
                    if(res.code=="200"){
                        parent.layer.msg("保存成功",{time:1000});
                        closeDiv();
                        parent.table.reload('test');
                    }else if(res.code=="2004") {
                        layer.msg("该物料代码已存在，不可重复添加!",{time:1500});
                        saveFlag = false;
                    }  else{
                        layer.msg(res.errMsg,{time:1000});
                        saveFlag = false;
                    }
                },complete: function () {
                    layer.close(index);
                },
                error:function (data) {
                }
            }) ;
        });

        form.on('select(materialTypeId)', function(data){
            var typeId = $(data.elem).find("option:selected").val();
            if(isBlank(typeId)){
                $("#unitCode").empty();
                $("#materialBrandId").empty();
                form.render('select');
                return;
            }
            $.ajax({
                url: '${ctx}/crmSupplyMaterialSetting/getListByTypeId',
                type: 'GET',
                data: {
                    typeId: typeId
                },
                success: function(data) {
                    $("#materialBrandId").empty();
                    let html = '<option value="">请选择</option>';
                    for (let i = 0; i < data.length; i++) {
                        let brand = data[i];
                        html += '<option value="' + brand.columns.id + '" data-value="' + brand.columns.name + '">' + brand.columns.name + '</option>';
                    }
                    $("#materialBrandId").append(html);
                    form.render('select');
                },
                complete:function(){

                }
            });
            $.ajax({
                url: '${ctx}/crmSupplyMaterialSetting/getMaterialUnitByTypeId',
                type: 'post',
                data: {
                    typeId: typeId
                },
                success: function(data) {
                    if(isBlank(data)){
                        layer.msg("暂无数据!");
                        $("#unitCode").empty();
                        form.render('select');
                        return
                    }
                    $("#unitCode").empty();
                    var html ="";
                    for(var i=0;i<data.length;i++){
                        var type = data[i];
                        html =html+ '<option value="'+type.columns.unit_code+'" >'+type.columns.name+'</option>';
                    }
                    $("#unitCode").append(html);
                    form.render('select');
                },
                complete:function(){

                }
            });
        });


    })

    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;
    }

    function isPrice(price) {
        var checkTe = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        if (checkTe.test(price)) {
            return true;
        }else{
            return false;
        }
    }



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