<%@ 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}/static/css/reset.css" media="all">
    <style>
        body{
            height: 450px !important;
        }

        .layui-card-body {
            line-height: 32px;
        }
    </style>

</head>
<body style="">

<div class="" id="" style="height: 100%;">
    <div class="layui-card" style="height: 100%;padding: 20px;">
        <div class="layui-card-body">
            <form class="layui-form" action="" lay-filter="component-form-element">
                <input value="${type}" hidden name="type" id="type">
                <%--<c:if test="${type eq '2'}">
                    <div class="layui-form-item layui-col-xs6 mb-5">
                        <label class="layui-form-label">项目公司：</label>
                        <div class="layui-input-block">
                            <select name="companyId" id="companyId" lay-verify="required" lay-search>
                                <option value="">请选择</option>
                                <c:forEach var="company" items="${company}">
                                    <option value="${company.columns.id}"  &lt;%&ndash;<c:if test="${apply.columns.trader_id eq company.columns.id}"> selected</c:if>&ndash;%&gt;>${company.columns.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                </c:if>--%>
                <div class="layui-col-xs12 mb-10 pr-30">
                    <c:if test="${type eq '2'}">
                        <div class="layui-col-xs6">
                            <label class="layui-form-label">备案类型：</label>
                            <div class="layui-input-block">
                                <select name="filingType" id="filingType" lay-verify="required" lay-search>
                                    <option value="">请选择</option>
                                    <option value="1">农户备案</option>
                                    <option value="2">项目公司备案</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-xs12"></div>
                    </c:if>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">模板名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="contractName" id="contractName" lay-verify="required" autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label" >所属资方：</label>
                    <div class="layui-input-block" >
                        <select  name="partner"  lay-verify="required" id="patern">
                            <option value="">请选择</option>
                            <c:forEach items="${paterns}" var="patern">
                                <option value="${patern.columns.id}" >${patern.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                </div>


                <div class="layui-form-item mb-5">
                    <label class="layui-form-label"></label>
                    <div class="layui-upload-drag" id="test10">
                        <i class="layui-icon"></i>
                        <p id="fileName" style="color: red"></p>
                        <p>点击上传，或将文件拖拽到此处</p>
                    </div>
                    <input name="fileId" hidden id="fileId">
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label" style="color: darkorange;">温馨提示：</label>
                    <div class="layui-input-block" style="padding-top: 5px;color: darkorange;">
                        <p>1.您可以上传需要用到的模板文件，每份最大30MB，支持格式：jpg、png、gif、doc、docx、ppt、pdf、xls、xlsx</p>
                        <p>2.禁止上传违反能辉相关规定的内容，一切风险均由本人承担！</p>
                    </div>
                </div>

                <div class="layui-form-item text-c mt-40" style="text-align: center;">
                    <input type="button" lay-submit lay-filter="component-form-element" id="saveTemplate" class="layui-btn " value="保存">
                </div>
            </form>
        </div>
    </div>
</div>

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

<script type="text/javascript">
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'jquery', 'layer','upload'], function(){
        var $ = layui.$
            ,upload = layui.upload
            ,element = layui.element
            ,form = layui.form
            ,index = parent.layer.getFrameIndex(window.name); //获取窗口索引;


        //绑定原始文件域
        upload.render({
            elem: '#test10'
            ,url: '${ctx}/contractTemplate/getTemplateUploadUrl'
            ,size: 10*1024*3
            ,number: 1
            ,accept:'file'
            ,exts: 'jpg|png|gif|doc|docx|ppt|pdf|xls|xlsx'
            ,done: function(res){
                if('200' ==res.code){
                    var fileData=res.data;
                    $("#fileId").val(fileData.fileId);
                    $("#fileName").text(fileData.name);
                    layer.msg('上传成功！', {icon: 1, time: 2000});
                }else{
                    layer.msg(data.errMsg, {icon: 2});
                }
            }
        });

        var checkAdd = false;
      form.on('submit(component-form-element)', function(data){
          if(checkAdd){
              return;
          }
          var formData = new FormData();
          var contractName=$("#contractName").val();
          var patern=$("#patern").val();
          var type=$("#type").val();
          formData.append("type",type)
          var companyId="";
          var filingType='';
          if('2'==type){
              companyId=$("#companyId").val();
              filingType=$("#filingType").val();
              if(isBlank(filingType)){
                  layer.msg("请选择备案类型!");
                  return;
              }
          }
          formData.append("filingType",filingType);
          formData.append("companyId",companyId);
          formData.append("name",contractName);
          formData.append("patern",patern);
          var fileId=$("#fileId").val();
          if(isBlank(fileId)){
              layer.msg("请先上传文件!");
              return;
          }
          formData.append("fileId",fileId);
          checkAdd=true;
          $.ajax({
              type: 'post',
              url: '${ctx}/contractTemplate/addTemplateUrl',
              contentType: false,
              processData: false,
              data: formData,
              dataType: 'json',
              success: function (data) {
               if('200' ==data.code){
                   var da=data.data;
                   layer.msg('请制作合同模板中的内容！', {icon: 1, time: 2000}, function () {
                       parent.layer.close(index);
                       parent.reload();
                       window.open(da.fileUploadUrl);
                   });
               }else{
                   layer.msg(data.errMsg, {icon: 2});
                   checkAdd=false;
               }
              },
              complete: function () {
                  checkAdd=false;
              }
          });
        });
    });

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

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

