<?php
$permission  = explode(',',$_SESSION['permission_id']);
if(!in_array(3,$permission) && !in_array(1,$permission) && !in_array(2,$permission) ){

?>
<script>
window.location="starter.php";
</script>
<?php
}
?>

<?php 
// $invoice_type_get = $_GET['inv_type']; // 0 For Raw Materials; 1:for Curtains
?>
<style >
	tfoot {
    display: table-header-group !important;
	}

	.typeahead__container.result .typeahead__list {

    	background-color: bisque;
	}

	.typeahead__container * {
    	margin-left: auto;
	}

	td {word-wrap: break-word !important;
		word-break: break-all;
		}
	div[id*='field_']{
		margin-top: -19px;
	}

	div[id*='edit_field_']{
		margin-top: -19px;
	}
</style>


<div id="add-form-cont" class="panel-collapse collapse panel" aria-expanded="false" style="height: 0px; position: relative; top:-3px; width:98%; margin:0px auto;">
	<div class="panel-body">
		<form class="form-horizontal" method="post" id="add_form" enctype="multipart/form-data">
			<div>
			<input type="hidden" id="access_token" name="access_token"  value="">
			<input type="hidden" id="invoice_type" name="invoice_type" value="0">
			<input type="hidden" id="total_rows" name="total_rows" value="1">
				<div class="col-sm-12">

					<div class="form-group">


						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label">رقم وصل الشراء</label>
							<input type="text" id="customer_id_fk" name="customer_id_fk" class="form-control" placeholder="رقم وصل الشراء" required="required">
							<div class="form-control-feedback">
								<i class="icon-coins text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label">التاريخ</label>
								<input type="text" id="invoice_date" name="invoice_date" class="form-control daterange-single" placeholder="التاريخ" >
								<div class="form-control-feedback">
									<i class=" icon-calendar52 text-muted"></i>
								</div>
						</div>

						
						


					</div>

					<div class="form-group">
						<div class="table-responsive">
						<table class="table col-sm-12 table-bordered .table-sm">
							<thead>
								<tr class="bg-blue ">
									<th>#</th>
									<th>الرمز المحلي</th>
									<th>الرمز الاجنبي</th>
									<th>الكمية</th>
									<th>اطوال المواد</th>
									<th>الطول الكلي</th>
									<th>سعر الشراء</th>
									<th>الكلفة الاجمالية</th>
									<th>ملاحظات</th>
									<th></th>
								</tr>
							</thead>
							<tbody id="purchase_item_body" >
								<tr  id="item_row_1" >
									<td width="5%" id="num_1">1</td>
									<td width="12%">
										<input id="product_name_1" name="product_name_1" type="text" class="form-control" placeholder="رمز المادة" onchange="load_products(this.value,this.id);" >
										<input id="product_id_fk_1" name="product_id_fk_1" type="hidden" placeholder="رمز المادة">
									</td>
									<td width="12%" id="forign_id_1"></td>
									<td width="8%">
										<input type="number" id="purchase_quantity_1" name="purchase_quantity_1"  class="form-control" placeholder="الكمية"  onchange="add_qty_input_fields(this.value,this.id);">
									</td>
									<td id="products_1"></td>
									<td width="7%" >
										<input id="total_length_1" name="total_length_1" type="text" class="form-control" readonly >
									</td>
									<td width="8%">
										<input id="buy_price_per_meter_1" name="buy_price_per_meter_1" type="text" class="form-control" placeholder="سعر شراء المتر" onchange="calc_total_item_cost(this.id);" >
									</td>
									<td width="9%" id="total_item_cost_1"> 0 $</td>
									<td width="10%">
										<textarea type="text" id="purchase_item_note_1" name="purchase_item_note_1" class="form-control" placeholder="ملاحظات" > </textarea>
									</td>
									<td width="6%">
										<button class="btn btn-link btn_add_item" id="">
											<i  class="icon-add text-blue"></i>
										</button>
										
									</td>
								</tr>
								
								
							</tbody>
							<tfoot>
								<tr class="bg-teal-400">
									<th></th>
									<th></th>
									<th></th>
									<th></th>
									<th></th>
									<th></th>
									<th></th>
									<th id="purchase_total">$ 0</th>
									<th></th>
									<th></th>
								</tr>
							</tfoot>
						</table>
					</div>



					</div>

					<div class="form-group">

						<div class="form-group has-feedback has-feedback-left  col-sm-3">
							<label class="control-label ">اختيار المخزن</label>
							
							<select class="form-control" name="customer_id_fk" id="customer_id_fk" data-width="100%" placeholder="المخزن">

							</select>
							<div class="form-control-feedback">
								<i class="icon-store text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label">ملاحظات</label>
							<textarea type="text" id="purchase_note" name="purchase_note" class="form-control" placeholder="ملاحظات" > </textarea> 
							<div class="form-control-feedback">
								<i class="icon-notebook text-muted"></i>
							</div>
						</div>

					</div>

					

					



				</div>
			</div>

			<div class="text-right">
				<button type="submit" id="submit_add_btn" class="btn btn-primary btn-labeled btn-labeled-left">
					Save <b><i class="icon-floppy-disk"></i></b>
				</button>
				<a href="#add-form-cont" id="add_form_cancel" class="btn btn-labeled btn-labeled-left bg-danger heading-btn" data-toggle="collapse">
					Close  <b><i class="icon-cross"></i></b>
				</a>
			</div>
		</form>
	</div>
</div>

<div id="edit_form_cont" class="modal fade" style="display: none;" data-keyboard="false" data-backdrop="static">
	<div class="modal-dialog modal-lg" style="width:1250px;">
		<form class="form-horizontal"  name="edit_form" method="post" id="edit_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="">

			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title">Edit Invoice</h4>
				</div>

				<div class="modal-body">
					<input type="hidden" name="itemid" id="edit_objid" value="">

					<div class="col-sm-12">

						<div class="form-group">

							<div class="form-group has-feedback has-feedback-left  col-sm-6">
								<label class="control-label is-visible">Customer Name</label>
								<div id="add_search_form">
									<div class="typeahead__container">
										<div class="typeahead__field">
											<span class="typeahead__query" >
												<input type="text" id="edit_customer_name" name="edit_customer_name" class="form-control" placeholder="Customer Name" required="required" autocomplete="off">
												<div class="form-control-feedback">
													<i class="icon-user-tie text-muted"></i>
												</div>
											</span>
										</div>
									</div>
								</div>
							</div>
							<input type="hidden"  name="edit_customer_id" id="edit_customer_id">
							<input type="hidden"  name="edit_customer_type" value="0" id="edit_customer_type">
							<input type="hidden"  name="edit_customer_name1" id="edit_customer_name1">


							<div class="form-group has-feedback has-feedback-left  col-sm-6">
								<label class="control-label is-visible"> Payment Term (Days) <i class="icon-bell3 text-muted"></i></label>
								<input type="text" id="edit_pay_term" name="edit_pay_term"  class="1touchspin-basic" placeholder="Payment Term" required="required">

							</div>

						</div>

						
						<!-- Edit Product  -->		

						<input type="hidden" id="edit_field_counter" name="edit_field_counter"  value="2">
					 	<div class="alpha-orange" id="edit_products_array_div" >
							
						</div>

						<!-- End Products -->

						<!-- Edit Payments -->
						<div class="form-group">
							<input type="hidden" name="edit_itemid_payment" id="edit_itemid_payment" value="">
							<input type="hidden" name="edit_itemid_invoice" id="edit_itemid_invoice" value="">
							<div class="col-sm-12" id="edit_edit_payments_fields">
								
								
							</div>
						</div>	
						<!-- End Payments -->

						<div class="form-group">


							<div class="form-group has-feedback has-feedback-left  col-sm-4">
								<label class="control-label is-visible">Total Invoice</label>
								<input type="text" id="edit_total_invoice" name="edit_total_invoice" class="form-control" placeholder="Total Invoice" readonly>
								<div class="form-control-feedback">
									<i class="icon-cash3 text-muted"></i>
								</div>
							</div>

							<div class="form-group has-feedback has-feedback-left  col-sm-4">
								<label class="control-label is-visible">Net Balance</label>
								<input type="text" id="edit_net_balance" name="edit_net_balance" class="form-control" placeholder="Net Balance" required="required">
								<div class="form-control-feedback">
									<i class="icon-cash2 text-muted"></i>
								</div>
							</div>

							<div class="form-group has-feedback has-feedback-left  col-sm-3">
								<label class="control-label is-visible">Total Paid</label>
								<input type="text" id="edit_total_paid" name="edit_total_paid" class="form-control" placeholder="Total Paid" readonly>
								<div class="form-control-feedback">
									<i class=" icon-cart-add text-muted"></i>
								</div>
							</div>
							<style>
								.checker, .checker span, .checker input {
									width: 30px;
									height: 30px;
								}
								.checker span:after {
									    font-size: 28px;
								}
							</style>
							<div class="form-group has-feedback has-feedback-left form-group-material col-sm-1" style="display: grid;">

								<label class="control-label">Offer</label>
								<input type="checkbox" id="edit_is_offer" name="edit_is_offer" class="form-control styled" placeholder="Total Paid" >

							</div>


						</div>



					</div> <!-- col-sm-12 -->

				</div>

				<div class="modal-footer">
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-primary"> Save <b><i class="icon-floppy-disk"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal" id="edit_btn_cancel"> Cancel
					</button>
				</div>
			</div>
		</form>
	</div>
</div>


<div id="edit_form_cont1" class="modal fade" style="display: none;">
		<div class="modal-dialog modal-lg">
		<form class="form-horizontal"  name="invoice_form" method="post" id="invoice_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="">

			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title">Invoice Detials</h4>
				</div>
				<div id="invoce_html">
					<div class="modal-body" >

							<div class="invoice_show">
								<!-- Borderless table -->

								<table width="100%" align="center">
									<tr>
										<td width="40%" id="invoice_detials">

										</td>
										<td width="20%" id="invoice_logo" align="center">
											<img src="<?php echo $_SESSION['company_logo']; ?>" width="100">
										</td>
										<td width="40%" id="invoice_company" align="right">

											<table width="100%">
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Company :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_name']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Address :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_address']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Mobile :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_phone']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">E-Mail :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_email']; ?></span></td>
												</tr>
												<tr class="text-black">
													<td width="40%"><span class="text-primary">Web Site :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf">www.company.com</span></td>
												</tr>
											</table>


										</td>
									</tr>
								</table>
									<hr>
									<div class="table-responsive">
										<table class="table table-borderless table-striped text-center" width="100%">
											<thead class="bg-grey-800">
												<tr class="bg-grey-800 inv_body_hdr">
													<th class="text-center">#</th>
													<th class="text-center">Product</th>
													<th class="text-center">Quantity</th>
													<th class="text-center">Unit Price</th>
													<th class="text-center">Discount</th>
													<th class="text-center">Total Price</th>
												</tr>
											</thead>
											<tbody id="invoice_body">

											</tbody>

										</table>
									</div>
									<div class="panel-footer  ">
										<table width="100%" align="center">
											<tr>
												<td width="30%" id="invoice_detials">
													<div class="text-black">Signature : </div>
													<br>
													<p class="text-black">Date : </p>
												</td>
												<td width="20%" id="invoice_logo1" align="center" style="vertical-align: top; padding-top: 5px;">

												</td>
												<td width="50%" id="invoice_footer">

												</td>
											</tr>
										</table>

									</div>


								<!-- /borderless table -->
							</div>
					</div>
				</div>
				<div class="modal-footer">
					<div id="link_pdf"></div>
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-primary" id="pdf"> Generate PDF <b><i class="icon-file-pdf"></i></b>
					</button>
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-success" id="print"> Print <b><i class=" icon-printer2"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> Cancel
					</button>
				</div>
			</div>
		</form>
	</div>
</div>



<div id="show_payments_modal" class="modal fade" style="display: none;">
		<div class="modal-dialog modal-lg">
		<form class="form-horizontal"  name="show_payments_form" method="post" id="show_payments_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="">

			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title">Invoice Payments Detials</h4>
				</div>
				<div id="invoce_html_pay">
					<div class="modal-body" >

							<div class="invoice_show">
								<!-- Borderless table -->

								<table width="100%" align="center">
									<tr>
										<td width="40%" id="invoice_detials_payment">

										</td>
										<td width="20%" id="invoice_logo" align="center">
											<img src="<?php echo $_SESSION['company_logo']; ?>" width="100">
										</td>
										<td width="40%" id="invoice_company" align="right">

											<table width="100%">
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Company :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_name']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Address :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_address']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">Mobile :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_phone']; ?></span></td>
												</tr>
												<tr class="text-bold">
													<td width="40%"><span class="text-primary">E-Mail :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf"><?php echo $_SESSION['company_email']; ?></span></td>
												</tr>
												<tr class="text-black">
													<td width="40%"><span class="text-primary">Web Site :</span></td>
													<td width="60%"><span class="text-danger" id="invoice_No_pdf">www.company.com</span></td>
												</tr>
											</table>


										</td>
									</tr>
								</table>
									<hr>
									<div class="table-responsive">
										<table class="table table-borderless table-striped text-center" width="100%">
											<thead class="bg-grey-800">
												<tr class="bg-grey-800 inv_body_hdr">
													<th class="text-center">#</th>
													<th class="text-center">Paid Price</th>
													<th class="text-center">Date of Paying</th>

												</tr>
											</thead>
											<tbody id="invoice_body_payment">

											</tbody>

										</table>
									</div>
									<div class="panel-footer  ">
										<table width="100%" align="center">
											<tr>
												<td width="45%" id="invoice_detials">
													<div class="text-black">Signature : </div>
													<br>
													<p class="text-black">Date : </p>
												</td>
												<td width="25%" id="invoice_logo1" align="center" style="vertical-align: top; padding-top: 5px;">

												</td>
												<td width="35%" id="invoice_footer_payment">

												</td>
											</tr>
										</table>

									</div>


								<!-- /borderless table -->
							</div>
					</div>
				</div>
				<div class="modal-footer">
					<div id="link_pdf_pay"></div>
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-primary" id="pdf_pay"> Generate PDF <b><i class="icon-file-pdf"></i></b>
					</button>
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-success" id="print_pay"> Print <b><i class=" icon-printer2"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> Cancel
					</button>
				</div>
			</div>
		</form>
	</div>
</div>





<div id="add_payments_modal" class="modal fade" >
		<div class="modal-dialog modal-lg">
		<form class="form-horizontal"  name="add_payments_form" method="post" id="add_payments_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="<?php echo $_SESSION['access_token']; ?>">
			<input type="hidden" name="itemid_payment" id="itemid_payment" value="">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title"> <i class="icon-bag"></i> Add New Payment</h4>
				</div>

				<div class="modal-body">
						<div>
							<div class="col-sm-12">

								<div class="form-group has-feedback has-feedback-left form-group-material col-sm-12">
									<label class="control-label">Payment Amoun</label>
									<input type="text" id="paid_price" name="paid_price" class="form-control" placeholder="Payment Amoun" required="required">
									<div class="form-control-feedback">
										<i class="icon-cash text-muted"></i>
									</div>
								</div>
							</div>
						</div>
				</div>
				<div class="modal-footer">
					<button type="submit" id="submit_add_pay_btn" class="btn btn-labeled btn-labeled-left heading-btn btn-primary"> Save <b><i class="icon-floppy-disk"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> Cancel
					</button>
				</div>
			</div>
		</form>
	</div>
</div>


<div id="edit_payments_modal" class="modal fade" >
		<div class="modal-dialog modal-lg">
		<form class="form-horizontal"  name="edit_payments_form" method="post" id="edit_payments_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="">
			<input type="hidden" name="itemid_payment" id="itemid_payment" value="">
			<input type="hidden" name="itemid_invoice" id="itemid_invoice" value="">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title"> <i class="icon-pencil15"></i> Edit Payments</h4>
				</div>

				<div class="modal-body">
							<div class="col-sm-12" id="edit_payments_fields">

								<div class="form-group has-feedback has-feedback-left form-group-material col-sm-12">
									<label class="control-label">Payment Amoun</label>
									<input type="text" id="paid_price" name="paid_price" class="form-control" placeholder="Payment Amoun" required="required">
									<div class="form-control-feedback">
										<i class="icon-cash text-muted"></i>
									</div>
								</div>
							</div>
				</div>
				<div class="modal-footer">

					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> Close
					</button>
				</div>
			</div>
		</form>
	</div>
</div>




				<!-- Individual column searching (text inputs) -->
				<div class="panel panel-flat">
					
					<table class="table datatable-column-search-inputs">
						<thead>
							<tr>
				                <th width="5%" >ID</th>
								<th >Invoice Number</th>
								<th >Customer Name</th>
								<th >Total Price</th>
								<th >Net Balance</th>
								<th >Total Paid</th>
								<th >Total Remained</th>
								<th >Invoice Date</th>
								<th >Invoice Status</th>
								<!--<th >Payment Term</th>-->
								<th width="10%" class="text-center">Actions</th>
				            </tr>
						</thead>
						<tbody>
							
						</tbody>
						<tfoot>
							<tr>
				                <td></td>
				                <td>username</td>
				                <td>full_name</td>
				                <td>EMail</td>
				                <td>Phone</td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				            </tr>
						</tfoot>
					</table>
				</div>
				<!-- /individual column searching (text inputs) -->



<script type="text/javascript" src="controllers/invoice.js"></script>