<?php $__env->startSection('content'); ?>
<style type="text/css" media="screen">
	.btn-custom{  
		width: 150px;  
    } 

    .table-btn td{
        padding:10px;
    }
</style>

    <div class="input-prepend input-append">
    </div>
    <h4 class="nametag"><?php echo e(trans($_CONST["LANG_DEST"].".title")); ?></h4>
	<br><br><br>
    <?php echo Form::open(array(  'class'=>'form-horizontal well')); ?>

    <div class="input-group">
        <span class="input-group-addon"><?php echo e(trans($_CONST["LANG_DEST"].".select_class")); ?>：</span>
        <?php echo Form::select('id', $categories, $cid, array('class' => 'form-control') ); ?>

    </div>
    <?php echo Form::close(); ?>

	<hr>

		<div class="row">	
            <div class="col-xs-8 col-sm-8 col-md-8">
                <table class="table-btn">
                    <tr><td>
                    <a href="<?php echo route($_CONST["CONTROLLER_ROUTE"].'.create', 'category='.$cid); ?>" class="btn btn-primary btn-custom" style="margin-top:10px">
                        <span class="glyphicon glyphicon-file"></span>
                            新增一般商品
                    </a>
                    </td><td>
                    <a href="<?php echo route('admin.product.create', 'id=1'); ?>" class="btn btn-primary btn-custom" style="margin-top:10px">
                        <span class="glyphicon glyphicon-file"></span>
                        新增集量商品
                    </a>
                    </td><td>
                    <a href="<?php echo route('admin.product.create', 'id=2'); ?>" class="btn btn-primary btn-custom" style="margin-top:10px">
                        <span class="glyphicon glyphicon-file"></span>
                        新增限時商品
                    </a>
                    </td></tr>
                    <tr><td>
                    <a href="#" class="btn btn-primary btn-custom store-fix" style="margin-top:10px">
                        <span class="glyphicon glyphicon-file"></span>
                        儲存修改
                    </a>
                    </td><td>
                    <a class="btn btn-primary btn-custom" style="margin-top:10px" data-toggle="modal" data-target="#wsModal">
                        <span class="glyphicon glyphicon-file"></span>
                        入倉
                    </a>
                    </td><td>
                    <a class="btn btn-primary btn-custom out-button" style="margin-top:10px" data-toggle="modal" data-target="#wsModal">
                        <span class="glyphicon glyphicon-file"></span>
                        出倉
                    </a>
                    </td></tr>
                </table>
			</div>

			<div class="col-md-4 col-sm-4 col-xs-4 pull-right" style="margin-top:10px">
				<div class="input-group">
					<input type="text" class="form-control" placeholder="Product's name" id="search_input">
					<span class="input-group-btn">
						<button class="btn btn-default" type="button" name="search">Seach</button>
					</span>
				</div>
			<?php /* Notification that number of products in warehouse is smaller than stock threshold!! */ ?>
			<div class="dropdown pull-right">
		        <button class="btn btn-warning btn-custom" style="margin-top:10px" type="button" data-toggle="dropdown" aria-expanded="true" title="最大數量小於最低水位的通知">
    	    	    <span class="glyphicon glyphicon-bell"></span>
					<?php echo e(trans($_CONST["LANG_DEST"].".notification")); ?>

				</button>
				<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
					<?php foreach($notifications as $noti): ?>
						<li role="presentation" ><a role="menuitem" tabindex="-1" href="#" class="noti_list" value="testvalue"><?php echo $noti; ?></a></li>
					<?php endforeach; ?>	
				</ul>
			</div>
			</div>

		</div>
		<hr>

    <table class="table table-bordered" id="product-table">
		<thead>
			<tr><th>商品分類</th><th>商品編號</th><th>商品名稱</th><th>商品條件</th><th>商品定價</th><th>商品售價</th><th style="width:150px">架上數量/許可數量</th><th style="width:80px">最大數量</th><th style="width:125px">特殊條件</th><th>紅利類別</th><th style="width:100px">商品顯示</th><th style="width:100px">商品狀態</th><th style="width:180px">功能按鈕</th></tr>
		</thead>
		<tbody>
        <?php foreach($products as $product): ?> 
        <tr data-id=<?php echo $product->id; ?> >
	    	<td style="width:129px font-size:12px"><?php echo $categories[$product->category_id]; ?></td>
            <td style="width:129px font-size:12px"><?php echo $product->product_code; ?></td>
            <td style="width:129px font-size:12px" id="td_pro_title"><?php echo $product->title; ?></td>
            <td style="width:129px font-size:12px"><?php echo $product->format; ?></td>
		    <td style="width:100px font-size:12px"><?php echo $product->price; ?></td>
            <td style="width:129px font-size:12px"><?php echo $product->selling_price; ?></td>
		    <td style="width:100px font-size:12px"><?php echo Form::text('available_amount', $product->available_amount, array('class' => 'form-control')); ?>/<?php echo $product->stock_amount; ?></td>
		    <td style="width:100px font-size:12px"><?php echo Form::text('stock_amount', $product->stock_amount, array('class' => 'form-control')); ?></td>
		    <td style="width:150px font-size:12px"><?php echo Form::select('special_case', array('無' => '無', '限量商品' => '限量商品', '熱銷商品' => '熱銷商品') , $product->special_case, array('class' => 'form-control' )); ?></td>
		    <td style="width:100px font-size:12px"><?php echo $bonusoption[$product->bonus_category_id]; ?></td>
		    <td style="width:150px font-size:12px"><?php echo Form::select('is_available', array('上架' => '上架', '下架' => '下架') , $product->is_available, array('class' => 'form-control')); ?></td>
			<td style="width:100px font-size:12px"><?php echo $product->type; ?>

			<?php 
				if($product->type == '集量商品'){
		    		echo "<br><{$product->deal_baseline}></td>";
				}
				else if($product->type == '限時商品'){
		    		echo "<br>{$product->close_date}</td>";
				}
			?>

            <td style="width:100px font-size:12px">
                <?php echo Form::open(array(
                    'route'=>array('admin.product.destroy', $product->id),
                    'method' => 'delete')
                ); ?>

                <a href="#" onclick="AddOperating('<?php echo $product->is_available; ?>','<?php echo $product->available_amount; ?>','<?php echo $product->stock_amount; ?>','<?php echo $product->id; ?>');" class="btn btn-primary hidden">
                    <span class="glyphicon glyphicon-pencil"></span>
                    操作
                </a>
                <a href="#" onclick="Broadcast()" class="btn btn-primary hidden">
                    <span class="glyphicon glyphicon-pencil"></span>
                    推播
                </a>
                <a href="<?php echo route('admin.product.edit', $product->id); ?>" class="btn btn-primary">
                    <span class="glyphicon glyphicon-pencil"></span>
                    編輯
                </a>

                <a href="#" onclick="if (confirm('<?php echo trans('Myadmin::layout.whether_to_delete'); ?>'))$(this).closest('form').submit()" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove"></span>
                    刪除
                </a>
                <?php echo Form::close(); ?>

            </td>

<?php /*			<td><img src="<?php echo e(img_upload_url($product->img_path)); ?>" width="200px"></td>	*/ ?>
        </tr>
        <?php endforeach; ?>
		<input type="hidden" value="" class="id-change"></input>
		</tbody>
    </table>

	<!-- Modal -->
	<div id="wsModal" class="modal fade" role="dialog">
		<div class="modal-dialog">

		<!-- Modal content-->
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">&times;</button>
					<h4 class="modal-title">商品入倉</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label class="control-label col-sm-2">商品編碼</label>
						<div class="col-sm-4">
							<?php echo Form::text('product_code', '', array('class' => 'form-control' , 'id' => 'codeid')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2">商品名稱</label>
						<div class="col-sm-4">
							<?php echo Form::label('title', '', array('class' => 'form-control' , 'id' => 'inproduct_title')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2">商品條件</label>
						<div class="col-sm-4">
							<?php echo Form::label('format', '', array('class' => 'form-control' , 'id' => 'inproduct_format')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2">倉儲數量</label>
						<div class="col-sm-4">
							<?php echo Form::label('stock_amount', '', array('class' => 'form-control' , 'id' => 'inproduct_stocknum')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2 change-num">入倉數量</label>
						<div class="col-sm-4">
							<?php echo Form::number('instock_amount', '', array('class' => 'form-control' , 'id' => 'instock_amount')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2">總和數量</label>
						<div class="col-sm-4">
							<?php echo Form::label('total_amount', ' ', array('class' => 'form-control' , 'id' => 'total_amount')); ?>

						</div>
					</div>  <br><br>
				</div>
				<div class="modal-footer">
					<button class="btn btn-success" id="instock_submit" data-dismiss="modal">submit</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				</div>
			</div>

		</div>
	</div>

    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            var base_url = "<?php echo route('admin.product.index'); ?>";
			var c_array = [];
			var unique;

            $("#search").click(function(){
                var redirect = base_url+'/'+$("input[name=search]").val();
                window.location.href = redirect;
            });
            $("#reset").click(function(){
                $("input[name=search]").val('');
            });

            $("select[name=id]").change(function(){
                var t = $("select[name=id]").val();
                var base_url = "<?php echo route($_CONST["CONTROLLER_ROUTE"].".index"); ?>";
                if (t==0) {
                    var target = base_url;
                }else {
                    var target = base_url + '/' + t;
                }
                window.location = target;
            });

			$("#search_input").keyup(function(){
				// When value of the input is not blank
				if( $(this).val() != ""){
				// Show only matching TR, hide rest of them
			 		$("#product-table tbody>tr").hide();
					$("#product-table #td_pro_title:contains-ci('"+$(this).val()+"')").parent("tr").show();
		 		}
		 		else{
			 		// When there is no input or clean again, show everything back
			 		$("#product-table tbody>tr").show();
		 		}
			});

			$(".noti_list").click(function(){
				if( $(this).text() != ""){
				// Show only matching TR, hide rest of them
			 		$("#product-table tbody>tr").hide();
					$("#product-table #td_pro_title:contains-ci('"+$(this).text()+"')").parent("tr").show();
		 		}
		 		else{
			 		// When there is no input or clean again, show everything back
			 		$("#product-table tbody>tr").show();
		 		}
			});

			$("[name=available_amount], [name=stock_amount]").keyup(small_change);
			$("[name=special_case], [name=is_available]").change(small_change);
		
			// store the small changes.	
			$(".store-fix").click(function(){
				var obj = [];
				if(unique != null){
					for (var i = 0 ; i < unique.length ; i++){
						var data = $("tr[data-id="+unique[i]+"]").children("td");//.children("[name=stock_amount]").val();
						obj.push({"id":unique[i].toString(), "available_amount":data.children("[name=available_amount]").val(), "stock_amount":data.children("[name=stock_amount]").val(), "special_case":data.children("select[name=special_case]").val(), "is_available":data.children("select[name=is_available]").val()});
					}
					window.location = "<?php echo URL::to('admin/product/fix/'); ?>" + "/" + JSON.stringify(obj);
					alert("store success !!");
				}
				else
					alert("nothing has to be stored");
			});

			function small_change(){
				var change = $(this).parents("tr").data("id");
				c_array.push(change);
				unique = c_array.filter(function(item,i,c_array){
					return i==c_array.indexOf(item);
				});
				$(".id-change").val(unique);
			};

			// input number of products you want to put in your stock amount.
			var allproducts = <?php echo $allproducts; ?>;
			var id = 0;
			$("#codeid").keyup(function(){
				$(".collapse").collapse('hide');
				for(var i=0 ; i<allproducts.length ; i++){
					if(allproducts[i]['product_code'] == $(this).val()){
						var stock = allproducts[i]['stock_amount'];
						id = allproducts[i]['id'];
						$(".collapse").collapse('show');
						$("#inproduct_title").text(allproducts[i]['title']);
						$("#inproduct_format").text(allproducts[i]['format']);
						$("#inproduct_stocknum").text(allproducts[i]['stock_amount']);
					}
				}
				$("#instock_amount").change(function(){
					console.log($(this).val());
					$('#total_amount').text(parseInt($(this).val())+parseInt(stock) );
				});
				$("#outstock_amount").change(function(){
					console.log($(this).val());
					$('#total_amount').text(parseInt(stock) - parseInt($(this).val()) );
				});
			});

			// in/out stock submit 
			$("#instock_submit").click(function(){
				var total = $("#total_amount").text();
				if(total == parseInt(total, 10))
					window.location = "<?php echo URL::to('admin/product/instock'); ?>" + "/" + id + "/" + total;
			});
			
			$(".out-button").click(function(){
				$(".modal-title").text("商品出倉");
				$(".change-num").text("出倉數量");
				$("#instock_amount").attr("id", "outstock_amount");
			});

        });

		$.extend($.expr[":"],
		{
			"contains-ci": function(elem, i, match, array)
			{
				return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
			}
		});
    </script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('Myadmin::layout.basic', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>