<?php $__env->startSection('header'); ?>
<script src="<?php echo App::make("myadmin")->asset('/ckeditor/ckeditor.js'); ?>"></script>
<script src="../../../js/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="<?php echo asset('css/bootstrap-datetimepicker.min.css'); ?>" />
<?php $__env->stopSection(); ?>

<?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 class="btn btn-primary btn-custom" style="margin-top:10px" data-toggle="modal" data-target="#wsModal">
                        <span class="glyphicon glyphicon-file"></span>
                        新增集量活動
                    </a>
<?php /*
                    <a href="<?php echo route('admin.product.create', 'category='.$cid); ?>" class="btn btn-primary btn-custom" style="margin-top:10px">
                        <span class="glyphicon glyphicon-file"></span>
                            新增集量活動
                    </a>
*/ ?>
                    </td><td>
                    <a href="#" class="btn btn-primary btn-custom store-fix" style="margin-top:10px">
                        <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 code" id="search_input">
					<span class="input-group-btn">
						<button class="btn btn-default" type="button" name="search">Seach</button>
					</span>
				</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>集量時段</th><th>完成數量</th><th>顯示狀態</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->status]; ?></td>
		    <td style="width:129px font-size:12px" id="td_pro_code"><?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->selling_price; ?> / <?php echo $product->price; ?></td>
		    <td style="width:100px font-size:12px"><?php echo $product->created_at; ?></td>
		    <td style="width:100px font-size:12px"><?php echo $product->starting_time; ?> ~ <?php echo $product->ended_time; ?></td>
		    <td style="width:150px font-size:12px"><?php echo $product->activities_num; ?>/<?php echo $product->deal_lowerbound; ?>~<?php echo $product->deal_upperbound; ?></td>
		    <td style="width:150px font-size:12px"><?php echo $product->is_available; ?></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::select('product_code', $productlist, '', 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('sequence', ' ', array('class' => 'form-control')); ?>

						</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('activities_num', ' ', array('class' => 'form-control')); ?>

						</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::number('price', '', array('class' => 'form-control' , 'id' => 'activities_price')); ?>

						</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::number('selling_price', '', array('class' => 'form-control' , 'id' => 'activities_selling_price')); ?>

						</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::select('is_available', array('上架'=>'上架', '下架'=>'下架'), '上架', array('class' => 'form-control' , 'id' => 'activities_avail')); ?>

						</div>
					</div> <br><br>
					<div class="form-group collapse">
						<label class="control-label col-sm-2 change-num">起始時間</label>
						<div id="datetimepicker1" class="col-sm-4 input-group date">  
							<input type="text" name="starting_time" class="form-control"></input>  
							<span class="glyphicon glyphicon-calendar add-on btn" data-time-icon="icon-time" data-date-icon="icon-calendar"></span>  
						</div>  
					</div>
					<div class="form-group collapse">
						<label class="control-label col-sm-2 change-num">結束時間</label>
						<div id="datetimepicker2" class="col-sm-4 input-group date">  
							<input type="text" name="starting_time" class="form-control"></input>  
							<span class="glyphicon glyphicon-calendar add-on btn" data-time-icon="icon-time" data-date-icon="icon-calendar"></span>  
						</div>  
					</div><br><br>
<?php /*
					<div class="form-group collapse">
						<label class="control-label col-sm-2 change-num">起始時間</label>
						<div class="col-sm-4">
							<?php echo Form::text('starting_time', '', array('class' => 'form-control' , 'id' => 'activities_start')); ?>

						</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::text('ended_time', '', array('class' => 'form-control' , 'id' => 'activities_end')); ?>

						</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('deal_lowerbound', '', array('class' => 'form-control' , 'id' => 'activities_lower')); ?>

						</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('deal_upperbound', '', array('class' => 'form-control' , 'id' => 'activities_upper')); ?>

						</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">
//	$(function() {
//	});
</script>
    <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('admin.product.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_code:contains-ci('"+$(this).val()+"')").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 productlist = [];
//			var count = 0;
			console.log(allproducts);
			<?php foreach($productlist as $pro): ?>
				productlist.push("<?php echo $pro; ?>");
//				count++;
			<?php endforeach; ?>
			console.log(productlist);

			var id = 0;
			$("#codeid").change(function(){
//				$(".collapse").collapse('hide');
				for(var i=0 ; i<allproducts.length ; i++){
					if(allproducts[i]['product_code'] == $(this).val()){
						console.log(allproducts[i]['product_code']);
						console.log($(this).val());
						$(".collapse").collapse('show');
						var stock = allproducts[i]['stock_amount'];
						id = allproducts[i]['id'];
						$("#activities_price").val(allproducts[i]['price']);
						$("#activities_price").text(allproducts[i]['price']);
						$("#activities_selling_price").val(allproducts[i]['selling_price']);
						$("#activities_avail").val(allproducts[i]['is_available']);
						break;
					}
//					else
//						$(".collapse").collapse('hide');
				}
			});

			// in/out stock submit 
			$("#instock_submit").click(function(){
			//	var total = $("#total_amount").text();
				console.log($("#activites_price").text());
				console.log($("#activites_selling_price").val());
				console.log($("#activites_avail").val());
				console.log($("#activites_start").val());
				console.log($("#activites_end").val());
				console.log($("#activites_lower").val());
				console.log($("#activites_upper").val());
//				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");
			});

        });

		$("#datetimepicker2").datetimepicker({
			language: 'en',
			pickTime: true,
			pickDate: true,
			format: 'yyyy-MM-dd hh:mm:ss',
		});
		$("#datetimepicker1").datetimepicker({
			language: 'en',
			pickTime: true,
			pickDate: true,
			format: 'yyyy-MM-dd hh:mm:ss',
		});
		$(".glyphicon-calendar").click(function(){ $(".icon-time").addClass("glyphicon glyphicon-time btn")});

		$.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(); ?>