<?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>
                    </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 style="width:80px">最大數量</th><th>建立時間</th><th style="width:170px">限時時段</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"><a href="#"><?php echo $product->product_code; ?> </a></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:129px font-size:12px"><?php echo $product->selling_price; ?> / <?php echo $product->price; ?></td>
		    <td style="width:129px 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: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 Form::select('is_available', array('上架' => '上架', '下架' => '下架') , $product->is_available, array('class' => 'form-control')); ?></td>
            <td style="width:100px font-size:12px">
                <?php echo Form::open(array(
                    'route'=>array('admin.product.destroy', $product->id, "activity=true"),
                    '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 URL::to('admin/product/limitededit/'); ?>/<?php echo $product->id; ?>" class="btn btn-primary">
                    <span class="glyphicon glyphicon-pencil"></span>
                    編輯
                </a>

                <a href="#" data-toggle="modal" data-target="#delModal" class="btn btn-danger" onclick="Show('<?php echo $product->id; ?>');"> <?php /*$(this).closest('form').submit()"> */ ?>
                    <span class="glyphicon glyphicon-remove"></span>
                    中止
                </a>
                <?php echo Form::close(); ?>

            </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' , 'id' => 'seqid')); ?>

						</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' , 'id' => 'numid')); ?>

						</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('available_amount', '', array('class' => 'form-control' , 'id' => 'avail_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::number('stock_amount', ' ', array('class' => 'form-control' , 'id' => 'activities_stock')); ?>

						</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" style="width:45%">  
							<input type="text" name="starting_time" class="form-control" id="activities_start" style="width:70%"></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" style="width:45%">  
							<input type="text" name="starting_time" class="form-control" id="activities_end" style="width:70%"></input>  
							<span class="glyphicon glyphicon-calendar add-on btn" data-time-icon="icon-time" data-date-icon="icon-calendar"></span>  
						</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('build_time', ' ', array('class' => 'form-control')); ?>

						</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>

	<!-- Modal -->
	<div id="delModal" 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::label('lcode', ' ', array('class' => 'form-control' )); ?>

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

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

						</div>
					</div> <br><br>
					<div class="form-group">
						<label class="control-label col-sm-2">限時售價</label>
						<div class="col-sm-4">
							<?php echo Form::label('lselling', ' ', array('class' => 'form-control')); ?>

						</div>
					</div> <br><br>
					<div class="form-group">
						<label class="control-label col-sm-2">架上數量</label>
						<div class="col-sm-4">
							<?php echo Form::label('lamount', ' ', array('class' => 'form-control' )); ?>  
						</div>
					</div> <br><br>
					<div class="form-group">
						<label class="control-label col-sm-2">顯示狀態</label>
						<div class="col-sm-4">
							<?php echo Form::label('lstatus', ' ', array('class' => 'form-control' )); ?>

						</div>
					</div> <br><br>
					<div class="form-group">
						<label class="control-label col-sm-2">建立時間</label>
						<div class="col-sm-4">
							<?php echo Form::label('lbuild', ' ', array('class' => 'form-control')); ?>

						</div>
					</div> <br><br>
					<div class="form-group">
						<label class="control-label col-sm-2">限時時段</label>
						<div class="col-sm-4">
							<?php echo Form::label('lduring', ' ', array('class' => 'form-control' )); ?>  
						</div>
					</div> <br><br>
				</div>
				<div class="modal-footer">
					<button class="btn btn-danger" data-dismiss="modal" id="stopid">Stop</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;
            });

            $("select[name=id]").change(function(){
                var t = $("select[name=id]").val();
                var base_url = "<?php echo URL::to('admin/product/limited'); ?>";
                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=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({"type":"limited", "cid":<?php echo $cid; ?>, "id":unique[i].toString(), "available_amount":data.children("[name=available_amount]").val(), "stock_amount":data.children("[name=stock_amount]").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 activity_pro = <?php echo $activity_pro; ?>;

//			console.log(activity_pro);
			<?php foreach($productlist as $pro): ?>
				productlist.push("<?php echo $pro; ?>");
			<?php endforeach; ?>

			var id = 0;
			var title = "";
			var format = "";
			$("#codeid").change(function(){
				var current = new Date();
//				console.log($(this).val());
				if($(this).val() == "test")
					$(".collapse").collapse('hide');

				for(var i=0 ; i<allproducts.length ; i++){
					if(allproducts[i]['product_code'] == $(this).val()){
						id = allproducts[i]['id'];
						$(".collapse").collapse('show');
						title = allproducts[i]['title'];
						format = allproducts[i]['format'];
						$("#activities_price").val(allproducts[i]['price']);
						$("#activities_selling_price").val(allproducts[i]['selling_price']);
						$("#activities_avail").val(allproducts[i]['is_available']);
						$("#avail_amount").val(allproducts[i]['available_amount']);
						$("#activities_stock").val(allproducts[i]['stock_amount']);
						var str = current.getFullYear()+"-"+current.getMonth()+"-"+current.getDate()+" "+current.getHours()+":"+current.getMinutes()+":"+current.getSeconds(); 
						$("label[for=build_time]").text(str);
						break;
					}
				}

				var count_seq = 0;
				var count_num = 0;
				for(var i=0 ; i<activity_pro.length ; i++){
//					console.log(activity_pro[i]);
					if(activity_pro[i]['product_id'] == id && activity_pro[i]['status']!=2 ){
						count_seq++;
						if(activity_pro[i]['status'] == '0' || activity_pro[i]['status'] == '1')
							count_num++;
					}	
				}
				$("#seqid").text(count_seq+1);
				$("#numid").text(count_num);
			});

			$("#instock_submit").click(function(){
				var obj = [];
				obj.push({"product_code":$("#codeid").val(), "title":title, "format":format,"price":$("#activities_price").val(), "selling_price":$("#activities_selling_price").val(), "is_available":$("#activities_avail").val(), "starting_time":$("#activities_start").val(), "ended_time":$("#activities_end").val(), "available_amount":$("#avail_amount").val(), "stock_amount":$("#activities_stock").val() });
				console.log(obj);
				window.location = "<?php echo URL::to('admin/product/createlimited/'); ?>" + "/" + JSON.stringify(obj);
			});
		
			$("#stopid").click(function(){
				var url = "<?php echo URL::to('admin/product/limiteddelete/'); ?>" + "/" + did;
				window.location = url;
			});
        });

		var activity_pro = <?php echo $activity_pro; ?>;
		var did;
		function Show(id){
			did = id;
			console.log(id);
			for(var i=0 ; i<activity_pro.length ; i++){
				if(activity_pro[i]['id'] == id){
					$("label[for=lcode]").text(activity_pro[i]['product_code']);
					$("label[for=lname]").text(activity_pro[i]['title']);
					$("label[for=lformat]").text(activity_pro[i]['format']);
					$("label[for=lselling]").text(activity_pro[i]['selling_price']+"/"+activity_pro[i]['price']);
					$("label[for=lamount]").text(activity_pro[i]['available_amount']+"/"+activity_pro[i]['stock_amount']);
					$("label[for=lstatus]").text(activity_pro[i]['is_available']);
					$("label[for=lbuild]").text(activity_pro[i]['created_at']);
					$("label[for=lduring]").text(activity_pro[i]['starting_time']+"~"+activity_pro[i]['ended_time']);
				}	
			}
		};

		$("#datetimepicker1, #datetimepicker2").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(); ?>