<?php	
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
?>
<br>
<div class="container">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>&nbsp;เพิ่มบันทึกข้อความ</h3>
    </div>
    <div class="panel-body">
      <form data-toggle="validator" role="form" enctype="multipart/form-data" class="form-horizontal" method="POST" action="?option=<?php echo $_REQUEST['option'];?>&task=save&action=insert">
        <div class="form-group">
          <label for="workgroup" class="col-sm-2 control-label">ส่วนราชการ</label>
          <div class="col-sm-10">
<?php
//print_r($_SESSION);
$WorkGroupName = array();
$SelectWorkGroup = "";
$sql = "SELECT * FROM `system_workgroup`";
$dbquery = mysqli_query($connect,$sql);
while($dataSelectWorkGroup = mysqli_fetch_assoc($dbquery)){
  $WorkGroupName[$dataSelectWorkGroup['workgroup']] = $dataSelectWorkGroup['workgroup_desc'];
  if($_SESSION['workgroup'] == $dataSelectWorkGroup['workgroup']){
    $selected = " selected " ;
    echo "<input type='hidden' name='workgroup_txt' value='".$dataSelectWorkGroup['workgroup_desc']." " . $_SESSION['office_name']."'>";
  }else{
    $selected = "";
  }
  $SelectWorkGroup .= "<option value='".$dataSelectWorkGroup['workgroup']."' $selected>".$dataSelectWorkGroup['workgroup_desc']." " . $_SESSION['office_name']."</option>\n";
}
?>
        <select class="form-control" name="workgroup">
          <option>เลือกส่วนราชการ</option>
          <?php echo $SelectWorkGroup;?>
        </select>
          </div>
        </div>
        <div class="form-group">
          <label for="bookNoText" class="col-sm-2 control-label">ที่</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" name="bookNoText" id="bookNoText" placeholder="เลขที่ออกอัตโนมัติ" disabled="true">
          </div>
          <label for="bookdate" class="col-sm-2 control-label">วันที่</label>
          <div class="col-sm-2">
            <input class = "form-control" data-provide="datepicker" value="<?php echo date("m/d/Y");?>">
          <script>
            $('.datepicker').datepicker({
                format: 'dd/mm/yyyy'//,
                //startDate: '-3d'
            });
            </script>
          </div>
        </div>
        <div class="form-group">
          <label for="subject" class="col-sm-2 control-label">เรื่อง</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" name="subject" id="subject" placeholder="ส่วนสำหรับพิมพ์ชื่อเรื่อง" required>
          </div>
        </div>
        <!--div class="form-group">
          <label class="col-sm-2 control-label" for="btn_predoc">ร่างหนังสือส่ง</label>
          <div class="col-sm-8">
            <input type="text" class="form-control " name="prebooktxt" id="prebooktxt" placeholder="" readonly>
            <input type="hidden" class="form-control " name="predoc_id" id="predoc_id" value="">
          </div>
          <div class="col-sm-2">
            <input type="button" class="form-control btn-primary" name="btn_predoc" id="btn_predoc" value="เลือกร่างหนังสือราชการ" data-toggle="modal" data-target="#SelectPreDoc">
          </div>
        </div-->
        <div class="form-group">
          <label for="book_to" class="col-sm-2 control-label">เรียน</label>
          <div class="col-sm-10">
            <input type="text" class="form-control " name="book_to" id="book_to" placeholder="" value="ผู้อำนวยการ<?php echo $_SESSION['office_name']; ?>">
<!--            
            <select name="ss" class="form-control selectpicker show-tick">
<?php
$strSQL = "SELECT * FROM `person_main`  where position_code <4 and status = '0' ORDER BY position_code, person_order, name";
$Dbquery = mysqli_query($connect,$strSQL);
while($dataPerson = mysqli_fetch_assoc($Dbquery)){

$txtGroup = ($dataPerson['position_code']>0)?$WorkGroupName[$dataPerson['department']]:"";
$txtGroup = str_replace("กลุ่ม","", $txtGroup);
echo "<option value='". $dataPerson['person_id']."'>".$dataPerson['prename'].$dataPerson['name']." ".$dataPerson['surname'].":".$Position[$dataPerson['position_code']].$txtGroup."</option>";
}
?>                            
            </select>

-->

          </div>
        </div>

        <div class="form-group">
          <label for="book_Refto" class="col-sm-2 control-label">อ้างถึง</label>
            <div class="col-sm-10">
            <input name="cntrow" type="hidden" id="cntrow" value="0">
              <table border="0" cellspacing="0" cellpadding="0" id="myTable">
                <tr>
                  <td width="80%">
                      <div class="col-xs-4"  id="UploadedFile" >
                        <input class="form-control" name="UploadedFile[]" type="file" class="BrowsFile" size="55">
                        </div>
                        <div class="col-xs-8"  id="UploadedFileComment" >
                        <input type="text" class="form-control" name="UploadedFileComment[]" placeholder="คำอธิบายหนังสืออ้างถึง">
                      </div>
                  </td>
                    <td width="20%">&nbsp;<div class="col-xs-6"><a href="javascript:insRow();" class="btn btn-success"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span>&nbsp;เพิ่มช่องรับเอกสาร</a></div>
                  </td>
                </tr>
              </table>
            </div>
        </div>

        <div class="form-group">
          <label for="book_attachment" class="col-sm-2 control-label">สิ่งที่ส่งมาด้วย</label>
            <div class="col-sm-10">
            <input name="cntrow2" type="hidden" id="cntrow2" value="0">
              <table border="0" cellspacing="0" cellpadding="0" id="myTable2">
                <tr>
                  <td width="80%">
                      <div class="col-xs-4"  id="UploadedFile2" >
                        <input class="form-control" name="UploadedFile2[]" type="file" class="BrowsFile" size="55">
                        </div>
                        <div class="col-xs-8"  id="UploadedFileComment2" >
                        <input type="text" class="form-control" name="UploadedFileComment2[]" placeholder="คำอธิบายเอกสารสิ่งที่ส่งมาด้วย">
                      </div>
                  </td>
                    <td width="20%">&nbsp;<div class="col-xs-6"><a href="javascript:insRow2();" class="btn btn-success"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span>&nbsp;เพิ่มช่องรับเอกสาร</a></div>
                  </td>
                </tr>
              </table>
            </div>

        </div>

        <div class="form-group">
          <label for="content1" class="col-sm-2 control-label">ย่อหน้า 1</label>
          <div class="col-sm-10">
            <textarea class="form-control" rows="6" name="content1" id="content1" placeholder="ส่วนสำหรับพิมพ์เนื้อหา"></textarea>
          </div>
        </div>
        <div class="form-group">
          <label for="content2" class="col-sm-2 control-label">ย่อหน้า 2</label>
          <div class="col-sm-10">
            <textarea class="form-control" rows="6" name="content2" id="content2" placeholder="ส่วนสำหรับพิมพ์เนื้อหา"></textarea>
          </div>
        </div>
        <div class="form-group">
          <label for="content3" class="col-sm-2 control-label">ย่อหน้า 3</label>
          <div class="col-sm-10">
            <textarea class="form-control" rows="6" name="content3" id="content3" placeholder="ส่วนสำหรับพิมพ์เนื้อหา">จึงเรียนมาเพื่อ</textarea>
          </div>
        </div>
        <!--div class="form-group">
          <input name="cntrow" type="hidden" id="cntrow" value="0">
          <label for="file" class="col-sm-2 control-label">เอกสารแนบ</label>
            <div class="col-sm-10">
              <table border="0" cellspacing="0" cellpadding="0" id="myTable">
                <tr>
                  <td width="80%">
                      <div class="col-xs-4"  id="UploadedFile" >
                        <input class="form-control" name="UploadedFile[]" type="file" class="BrowsFile" size="55">
                        </div>
                        <div class="col-xs-8"  id="UploadedFileComment" >
                        <input type="text" class="form-control" name="UploadedFileComment[]" placeholder="คำอธิบายเอกสารแนบ">
                      </div>
                  </td>
                    <td width="20%">&nbsp;<div class="col-xs-6"><a href="javascript:insRow();" class="btn btn-success"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span>&nbsp;เพิ่มช่องรับเอกสาร</a></div>
                  </td>
                </tr>
              </table>
            </div>
        </div-->
        <hr>
        <!--div class="form-group">      
          <label class="col-sm-2 control-label"></label>
          <div>
            <input type='checkbox'  name='no_comment' id='no_comment' value='1'>&nbsp;&nbsp;<label for="no_comment">  ไม่ต้องผ่านผู้บังคับบัญชาขั้นต้น (เลือกกรณีผู้บังคับบัญชาขั้นต้นไม่ได้ปฏิบัติราชการ)</label>
          </div>
        </div-->        
        <div class="form-group">      
          <label class="col-sm-2 control-label">ส่งบันทึกข้อความถึง</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" name="booktotxt" id="booktotxt" placeholder="เลือกบุคคลพิจารณา/เห็นชอบ" readonly>
            <input type="hidden" class="form-control " name="bookto_id" id="bookto_id">
          </div>
          <div class="col-sm-4">
            <input type="button" class="form-control btn-info" name="SelectBookto" id="SelectBookto" value="เลือกบุคคลพิจารณา/เห็นชอบ" data-toggle="modal" data-target="#ModalSelectBookto">
          </div>
        </div> 
        <hr>
        <div class="form-group">
          <label for="post_personname" class="col-sm-2 control-label">บันทึกโดย</label>
          <div class="col-sm-3">
            <input type="text" class="form-control" id="post_personname" value="<?php echo $_SESSION['login_prename'].$_SESSION['login_name']." ".$_SESSION['login_surname'];?>"  name="officer_name">
            <input type="hidden" class="form-control" name="officer" id="officer" value="<?php echo $_SESSION['login_user_id'];?>">
          </div>
        </div>        
        <div class="form-group">
          <label for="post_position" class="col-sm-2 control-label">ตำแหน่ง</label>
          <div class="col-sm-3">
            <input type="text" class="form-control" id="post_position" value="<?php echo $_SESSION['login_userposition'];?>" name="officer_position">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-6">
            <button type="submit" name = "draft" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span>&nbsp;บันทึกฉบับร่าง</button>&nbsp;
            <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok"></span>&nbsp;บันทึก</button>&nbsp;
            <button type="submit" class="btn btn-default" onClick="history.go(-1);return true;"><span class="glyphicon glyphicon-remove"></span>&nbsp;ยกเลิก</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</div> 
<!-- Validate Form-->
<!-- Bootstrap Popover -->
<script>
	$(function () {
 		$('[data-toggle="popover"]').popover()
	})
</script>

<!-- Bootstrap Confirmation -->
<script>
	$('[data-toggle="confirmation"]').confirmation()
</script>

<!-- Bootstrap Confirmation -->
<script>
	$('[data-toggle="validator"]').validator()
</script>


<!-- Show and Hide File Upload -->
<script type="text/javascript">
function insRow()
  {	
  	var CntRow=document.getElementById('cntrow');
		var idTR=(parseInt(CntRow.value)+1);
		  var x=document.getElementById('myTable').insertRow(idTR);
		  var Col0=x.insertCell(0);
		  var Col1=x.insertCell(1);
      var OldObj1=document.getElementById("UploadedFile").outerHTML;
      var OldObj2=document.getElementById("UploadedFileComment").outerHTML;
      var NewObj1=OldObj1.replace("id=UploadedFile","id=UploadedFile"+idTR);      
      var NewObj2=OldObj2.replace("id=UploadedFileComment","id=UploadedFileComment"+idTR);      
      Col0.innerHTML=NewObj1+NewObj2;
      //Col0.innerHTML=NewObj2;
		  Col1.innerHTML='&nbsp;<a href="javascript:delRow('+idTR+');" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>';
 CntRow.value=idTR;//ค่าต่อไป
  }

function insRow2()
  {	
  	var CntRow2=document.getElementById('cntrow2');
		var idTR=(parseInt(CntRow2.value)+1);
		  var x=document.getElementById('myTable2').insertRow(idTR);
		  var Col0=x.insertCell(0);
		  var Col1=x.insertCell(1);
      var OldObj1=document.getElementById("UploadedFile2").outerHTML;
      var OldObj2=document.getElementById("UploadedFileComment2").outerHTML;
      var NewObj1=OldObj1.replace("id=UploadedFile2","id=UploadedFile2"+idTR);      
      var NewObj2=OldObj2.replace("id=UploadedFileComment2","id=UploadedFileComment2"+idTR);      
      Col0.innerHTML=NewObj1+NewObj2;
      //Col0.innerHTML=NewObj2;
		  Col1.innerHTML='&nbsp;<a href="javascript:delRow2('+idTR+');" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>';
 CntRow2.value=idTR;//ค่าต่อไป
  }

function delRow(obj)
  {
    	var CntRow=document.getElementById('cntrow');
		if(obj==CntRow.value){ // ลบด้านล่างก่อน
				CntRow.value=(parseInt(CntRow.value)-1);
				document.getElementById('myTable').deleteRow(obj);
		}else{
			alert('ลบช่องเอกสารแนบจากด้านล่างก่อน');
		}
  }

function delRow2(obj)
  {
    	var CntRow2=document.getElementById('cntrow2');
		if(obj==CntRow2.value){ // ลบด้านล่างก่อน
				CntRow2.value=(parseInt(CntRow2.value)-1);
				document.getElementById('myTable2').deleteRow(obj);
		}else{
			alert('ลบช่องเอกสารแนบจากด้านล่างก่อน');
		}
  }

$(function(){
	$("select#department").change(function(){
		var datalist2 = $.ajax({	
			  url: "modules/ioffice/return_ajax_sub_department.php", 
			  data:"department="+$(this).val(),
			  async: false
		}).responseText;		
		$("select#sub_department").html(datalist2); 
	});
});

$(function(){
	$("select#sub_department").change(function(){
		var datalist2 = $.ajax({	
			  url: "modules/ioffice/return_ajax_person_subdepartment.php", 
			  data:"sub_department="+$(this).val(),
			  async: false
		}).responseText;		
		$("select#person").html(datalist2); 
	});
});
</script>
	<!-- End Content -->

<!-- Modal -->
<div class="modal fade" id="SelectPreDoc" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">เลือกร่างหนังสือราชการ</h5>
      </div>
      <div class="modal-body">
<table class="table table-striped table-bordered"  style="width:100%" id="dataPrebook">
  <thead>
    <tr>
        <th>เรื่อง</th>
    </tr>
  </thead>
  <tbody>
<?php
$strSQL = "SELECT * FROM `bookregister_send_pre` ORDER BY ms_id DESC limit 100";
$Dbquery = mysqli_query($connect,$strSQL);
while($dataPrebook = mysqli_fetch_assoc($Dbquery)){
?>
    <tr>
        <td>
<div class="form-group">
 <div class="col-sm-12">
   <label class="radio-inline"><input type="radio" name="prebookid" id="prebook<?php echo $dataPrebook['ms_id']; ?>" value="<?php echo $dataPrebook['ms_id']; ?>"><?php echo $dataPrebook['subject']; ?></label>
 </div>
</div>
        </td>
    </tr>
<?php  
}
?> 
  </tbody>    
</table>      

<script>
$(document).ready(function() {
    $('#dataPrebook').DataTable();
} );

$("[name='prebookid']").change(function(){
  document.getElementById('prebooktxt').value = $('[name="prebookid"]:checked').closest('label').text();
  document.getElementsByName('predoc_id').value = $('[name="prebookid"]:checked').val();
  $('#SelectPreDoc').modal('hide');
});
</script>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal -->
<div class="modal fade" id="ModalSelectBookto" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">เลือกบุคคลพิจารณา/เห็นชอบ</h5>
      </div>
      <div class="modal-body">
<table class="table table-striped table-bordered table-hover"  style="width:100%" id="dataPerson">
  <thead>
    <tr>
        <th width = "50%"> ชื่อ - สกุล </th>
        <th width = "50%"> ตำแหน่ง </th>
        <th width = "0"> รหัสตำแหน่ง </th>
    </tr>
  </thead>
  <tbody>
<?php
$strSQL = "SELECT * FROM `person_main`  where status = '0' ORDER BY position_code, person_order, name";
$Dbquery = mysqli_query($connect,$strSQL);
while($dataPerson = mysqli_fetch_assoc($Dbquery)){
?>
    <tr>
        <td>
<div class="form-group">
 <div class="col-sm-12">
   <label class="radio-inline"><input type="radio" name="bookPersonid" id="bookPersonid<?php echo $dataPerson['id']; ?>" value="<?php echo $dataPerson['person_id']; ?>"><?php echo $dataPerson['prename']."".$dataPerson['name']." ".$dataPerson['surname']; ?></label>
 </div>
</div>
        </td>
        <td><?php echo $Position[$dataPerson['position_code']]; ?></td>
        <td><?php echo $dataPerson['position_code']; ?></td>

    </tr>

<?php  
  
}
?> 
  </tbody>    
</table>      

<script>
$(document).ready(function() {
    $('#dataPerson').DataTable({
    "aoColumnDefs": [
      { "iDataSort": 2, "aTargets": [ 0 ] },
      {"visible": false, "targets":2}
    ]
  });
} );

$("[name='bookPersonid']").change(function(){
  document.getElementById('booktotxt').value = $('[name="bookPersonid"]:checked').closest('label').text();
  document.getElementById('bookto_id').value = $('[name="bookPersonid"]:checked').val();
  $('#ModalSelectBookto').modal('hide');
  //alert(document.getElementById('bookto_id').value);
});
</script>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<?php
//print_r($_SESSION);
?>