<%@ 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-card md-0">
    <div class="layui-card-body">
        <div class="layui-form h-246" lay-filter="layuiadmin-form-admin" >
            <div class="layui-form-item layui-col-xs12 ">
                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label">物料号：</label>
                    <div class="layui-input-block">
                        <select name="fittingCode" id="fittingCode" lay-search lay-filter="test">
                            <option value="">请选择</option>
                            <c:forEach var="fitting" items="${list}">
                                <option value="${fitting.columns.id}" <c:if test="${fitting.columns.id eq apply.columns.fitting_id}"> selected </c:if>>${fitting.columns.code}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label">备件名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="fittingName" id="fittingName" class="layui-input layui-bg-gray" value="${apply.columns.fitting_name}" disabled>
                    </div>
                </div>
                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label">备件型号：</label>
                    <div class="layui-input-block">
                        <input type="text" id="fittingVersion" name="fittingVersion" class="layui-input layui-bg-gray" value="${apply.columns.fitting_version}" disabled>
                    </div>
                </div>
                <div class="layui-col-xs6 mb-5">
                    <label class="layui-form-label">申请数量：</label>
                    <div class="layui-input-block">
                        <input type="text" lay-verify="ling" name="applyNum" id="applyNum" class="layui-input " value="<fmt:formatNumber type="number" value="${apply.columns.apply_num}" maxFractionDigits="2"/>" >
                    </div>
                </div>
                <div class="layui-col-xs12 mb-5">
                    <label class="layui-form-label">申请备注：</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" class="layui-textarea" id="applyMarks" name="applyMarks" >${apply.columns.apply_marks}</textarea>
                    </div>
                </div>
            </div>
            </br>
        </div>
    </div>
    <div class="operate-footer">
        <input type="hidden" id="applyId" name="applyId" value="${apply.columns.id}" />
        <button class="layui-btn" onclick="adoptApply('${apply.columns.id}')">保存</button>
        <button class="layui-btn" onclick="refuseFitting('${apply.columns.id}')">撤销</button>
        <button class="layui-btn layui-btn-primary" onclick="cancel()">取消</button>
    </div>
</div>

<div class="layui-form  layui-card hide" id="Reasons">
    <div class="layui-card ">
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <label class="layui-form-label"><i class="mark">*</i>拒绝理由：</label>
                    <div class="layui-input-block">
                        <textarea name="refuseReason" id="refuseReason" class="layui-textarea" ></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    //通过申请
    var $;
    var noRepeatSub = false;
    function adoptApply(id){
        if(noRepeatSub){
            return;
        }
        var fitId = $("#fittingCode").val();  //返还旧件
        var fitCode = $("select option:selected").text();  //返还旧件
        var fittingName = $("#fittingName").val();  //返还旧件
        var fittingVersion = $("#fittingVersion").val();  //返还旧件
        var applyNum = $("#applyNum").val();  //返还旧件
        var applyMarks = $("#applyMarks").val();

        var checkTe = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
        if(isBlank(fitId)){
            layer.msg("请选择备件", {icon: 5, anim: 6});
            return;
        }else if (!checkTe.test(applyNum)) {
            layer.msg("备件申请数量格式不正确", {icon: 5, anim: 6});
            return;
        }else{
            $.ajax({
                url: '${ctx}/site/apply/saveApply',
                type: 'post',
                data: {
                    id: id,
                    fitId: fitId,
                    fitCode: fitCode,
                    fittingName:fittingName,
                    fittingVersion:fittingVersion,
                    applyNum:applyNum,
                    applyMarks:applyMarks
                },
                success: function(data) {
                    if(data.code=="200"){
                        layer.msg("保存成功！",{time:1000},function(){
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layerIFrameReload(index); //再执行关闭
                            parent.layer.closeAll();
                            window.parent.location.reload();
                        });
                    }else if(data.code=="401"){
                        layer.msg(data.errMsg,{icon:2})
                    }else{
                        layer.msg("保存失败！");
                    }
                },
                complete:function(){

                }
            });
        }
    }

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

        form.on('select(test)', function(data){
            var id = $("#fittingCode").val();
            $.ajax({
                type:"post",
                url:"${ctx}/site/apply/changeApply",
                data:{id:id},
                success:function(data){
                    $("#fittingName").val(data.name);
                    $("#fittingVersion").val(data.version);
                    $("#applyNum").val('');
                    $("#applyMarks").val('');
                },
                error:function(e){
                    layer.msg("请重新选择.");
                }
            });
        });

    });

    function refuseFitting(id){
        layer.open({
            type: 0
            ,content:"确定要撤销申请吗？"
            ,title: "撤销申请"
            ,btn:['确定', '取消']
            ,area:["300px","160px"]
            ,yes: function(index, layero){
                $.ajax({
                    url: '${ctx}/site/apply/revokeApply',
                    type: 'post',
                    data: {
                        id: id
                    },
                    success: function(data) {
                        if(data.code=="200"){
                            layer.msg("撤销成功！",{time:1000},function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layerIFrameReload(index); //再执行关闭
                                parent.layer.closeAll();
                                window.parent.location.reload();
                            });
                        }else if (data.code == "401") {
                            layer.msg(data.errMsg, {icon: 2});
                        } else {
                            layer.msg("撤销失败！");
                        }
                    },
                    complete:function(){
                        noRepeatSub = false;
                    }
                });
            }
        })

    }





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

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


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