<?php $__env->startSection('css_page_level'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css')); ?>"/>
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/bootstrap-tagsinput/bootstrap-tagsinput.css')); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.css')); ?>"/>
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/select2/css/select2.css')); ?>"/>
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/select2/css/select2-bootstrap.min.css')); ?>"/>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('page-content-wrapper'); ?>
	<div class="page-content-wrapper" id="main">
		<div class="page-content">
			<!-- BEGIN PAGE HEADER-->
			<h3 class="page-title">
			<?php $setting = app('anlutro\LaravelSettings\Facade'); ?>
			<?php echo e($setting::get('system_name','全方位建築智慧化管理系統')); ?> <small><?php echo e($setting::get('company_name','新保科技')); ?></small>
			</h3>
			<div class="page-bar">
				<ul class="page-breadcrumb">
					<li>
						<i class="fa fa-home"></i>
						<a href="<?php echo e(route('manage::index')); ?>">首頁</a>
						<i class="fa fa-angle-right"></i>
					</li>
					<li>
						<a href="javascript:;">設備管理</a>
						<i class="fa fa-angle-right"></i>
					</li>
					<li>
						<a href="<?php echo e(action('InstEquipmentController@index')); ?>">設備登陸</a>
					</li>
				</ul>
				<div class="page-toolbar">
				</div>
			</div>
			<!-- END PAGE HEADER-->
			<!-- BEGIN DASHBOARD STATS -->
			<div class="row">
				<div class="col-md-12">
					<!-- BEGIN EXAMPLE TABLE PORTLET-->
					<div class="portlet light bordered">
						<div class="portlet-body">
							<div class="table-toolbar">
								<div class="row">
									<div class="col-md-6">
										<div class="btn-group">
											<a href="<?php echo e(action('InstEquipmentController@create')); ?>" class="btn green">
											新增 <i class="fa fa-plus"></i>
											</a>
											<button id="btn_mass_delete" class="btn red" style="display:none;margin-left:10px;margin-bottom:10px;">刪除 <i class="fa fa-trash"></i></button>
											<button id="btn_tag" class="btn blue pull-right" style="margin-left:10px;margin-bottom:10px;"> 項目管理 <i class="fa fa-cog"></i></button>
										</div>
									</div>
										<a href="<?php echo e(action('InstEquipmentController@index')); ?>" class="btn blue pull-right" style="margin-left:10px;margin-bottom:10px;">全部</a>
										<a href="<?php echo e(action('InstEquipmentController@index')); ?>?filter=0" class="btn green pull-right" style="margin-left:10px;margin-bottom:10px;">未借出</a>
										<a href="<?php echo e(action('InstEquipmentController@index')); ?>?filter=1" class="btn red pull-right" style="margin-left:10px;margin-bottom:10px;">借用中</a>
								</div>
							</div>
							<table class="table table-striped table-bordered table-hover" id="table_list">
							<thead>
							<tr>
								<th style="width:2%" class="table-checkbox">
									<input type="checkbox" class="group-checkable" data-set="#table_list .checkboxes"/>
								</th>
								<th style="width:2%">
									 #
								</th>
								<th style="width:15%">
									 財產編號
								</th>
								<th style="width:15%">
									 設備名稱
								</th>
								<th style="width:8%">
									 保管人
								</th>
								<th style="width:8%">
									 放置位置
								</th>
								<th style="width:15%">
									 最後一次借用日期
								</th>
								<th style="width:15%">
									 最後一次借用人
								</th>
								<th style="width:8%">
									 狀態
								</th>
								<th style="width:8%">
									 動作
								</th>
							</tr>
							</thead>
							<tbody>
							<?php foreach($inst_equipments as $one): ?>
							<tr class="odd gradeX">
								<td>
									<input type="checkbox" class="checkboxes" value="<?php echo e($one->id); ?>"/>
								</td>
								<td>
									<?php echo e($one->id); ?>

								</td>
								<td>
									<?php echo e($one->equipment_no); ?>

								</td>
								<td>
									<?php echo e($one->equipment_name); ?>

								</td>
								<td>
									<?php echo e($one->custodian_name); ?>

								</td>
								<td>
									<?php if($one->placement == '無'): ?>
										<?php echo e($one->meeting_room); ?>

									<?php else: ?>
										<?php echo e($one->placement); ?>

									<?php endif; ?>
								</td>
								<td>
									<?php echo e($one->borrowing_time); ?>

								</td>
								<td>
									<?php echo e($one->borrower); ?>

								</td>
								<td>
									<?php if($one->status == '0'): ?>
										<span class="label label-sm label-success"> 未借出 </span>
									<?php elseif($one->status == '1'): ?>
										<span class="label label-sm label-danger"> 借用中 </span>
									<?php endif; ?>
								</td>
								<td>
									<div class="btn-group">
										<button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
										動作 <i class="fa fa-angle-down"></i>
										</button>
										<ul class="dropdown-menu pull-right" role="menu">
											<?php if( $one->status == '1' ): ?>
												<li>
													<a class="btn_return" uid="<?php echo e($one->id); ?>" href="javascript:;">
													<i class="fa fa-check"></i>
													歸還 </a>
												</li>
											<?php endif; ?>
											<li>
												<a href="<?php echo e(action('InstEquipmentController@edit',['id'=>$one->id])); ?>">
												<i class="fa fa-pencil"></i>
												編輯 </a>
											</li>
											<li class="divider">
											</li>
											<li>
												<a class="btn_delete_one" utitle="<?php echo e($one->equipment_name); ?>" uid="<?php echo e($one->id); ?>" href="javascript:;">
												<i class="fa fa-trash-o"></i>
												刪除 </a>
											</li>
										</ul>
									</div>
								</td>
							</tr>
							<?php endforeach; ?>
							</tbody>
							</table>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>
		</div>

	<input type="hidden" id="_token" name="_token" value="<?php echo e(csrf_token()); ?>">
	
<div class="modal fade" id="modal_tag" tabindex="-1" role="basic" aria-hidden="true">
	<div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">項目管理</h4>
            </div>
            <div class="modal-body">
            	<form id="form_type" class="form-horizontal form-bordered ">
                    <div class="form-body">
                        <div class="form-group">
                            <label class="control-label col-md-3">放置位置</label>
                            <div class="col-md-9">
                                <input name="placement" class="tagsinput" type="text" value="<?php echo e($placement->tags); ?>" data-role="tagsinput"> 
                            </div>
                        </div>
                        <input type="hidden" id="_token" name="_token" value="<?php echo e(csrf_token()); ?>">
                    </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-9">
                                <button id="btn_type_submit" type="button" class="btn green">
                                    <i class="fa fa-check"></i> 儲存</button>
                                <button type="button" class="btn default" data-dismiss="modal">關閉</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

	<div class="modal" id="modal_edit_return"  role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<form id="edit_form" class="form-horizontal">
			<div class="modal-content">

				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
					<h4 class="modal-title">設備歸還作業</h4>
				</div>
				<div class="modal-body form">
					<!-- BEGIN FORM-->
						<div class="form-body">
							<div id="form_error_alert" class="alert alert-danger display-hide">
								<button class="close" data-close="alert"></button>
								您似乎有部分欄位有錯誤，請查看後再送出。
							</div>
							<div id="form_success_alert" class="alert alert-success display-hide">
								<button class="close" data-close="alert"></button>
								更新成功。
							</div>
							<input name="id" type="hidden"/>
							<input name="equipment" type="hidden"/>
								<div class="form-group">
									<label class="col-md-3 control-label">借出編號</label>
									<div class="col-md-6">
										<input name="id" type="text" class="form-control readOnlyInput" maxlength="25">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">財產編號</label>
									<div class="col-md-6">
										<input name="equipment_no" type="text" class="form-control readOnlyInput" maxlength="25">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">設備名稱</label>
									<div class="col-md-6">
										<input name="equipment_name" type="text" class="form-control readOnlyInput" maxlength="25">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">放置位置</label>
										<div class="col-md-6">
											<input name="placement" type="text" class="form-control readOnlyInput">
										</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">保管人</label>
										<div class="col-md-6">
											<input name="custodian" type="text" class="form-control readOnlyInput">
										</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">經手人</label>
										<div class="col-md-6">
											<input name="beforeby" type="text" class="form-control readOnlyInput">
										</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">借用時間</label>
									<div class="col-md-6">
										<input name="borrowing_time" type="text" class="form-control readOnlyInput" maxlength="25">
									</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">借用人</label>
										<div class="col-md-6">
											<input name="borrower" type="text" class="form-control readOnlyInput">
										</div>
								</div>
								<div class="form-group">
									<label class="col-md-3 control-label">歸還人</label>
										<div class="col-md-6">
											<select id="return" name="return" class="form-control select2me" id="set" data-placeholder="請選擇歸還人">
												<optgroup label="<?php echo app('translator')->get('messages.liver'); ?>">
													<?php foreach($users as $user): ?>
													<option value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?>(<?php echo e($user->username); ?>)</option>
													<?php endforeach; ?>
												</optgroup>
												<optgroup label="<?php echo app('translator')->get('messages.admin'); ?>">
													<?php foreach($admins as $admin): ?>
													<option value="<?php echo e($admin->id); ?>"><?php echo e($admin->name); ?>(<?php echo e($admin->username); ?>)</option>
													<?php endforeach; ?>
				    							</optgroup>
											</select>
										</div>
								</div>
						</div>
					<!-- END FORM-->
				</div>
				<div class="modal-footer">
					<button type="button" class="btn default" data-dismiss="modal">關閉</button>
					<button type="submit" class="btn blue" id="btn_form_edit">歸還</button>
				</div>
			</div>
			</form>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	
<?php $__env->stopSection(); ?>

<?php $__env->startSection('plugins_page_level'); ?>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/media/js/jquery.dataTables.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootbox/bootbox.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootstrap-tagsinput/bootstrap-tagsinput.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/jquery-validation/js/jquery.validate.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootstrap-fileinput/additional-methods.js')); ?>"></script>
<script src="<?php echo e(asset('assets/global/plugins/moment.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/select2/js/select2.min.js')); ?>"></script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('js_page_level'); ?>
<script>
$(document).ready(function(){
	
    $('.readOnlyInput').prop('readonly', true);
	$('.show-popover').popover({
		html:true
	})
	$('.show-tooltip').tooltip();
	$('#table_list').dataTable({
		// Internationalisation. For more info refer to http://datatables.net/manual/i18n
		"language": {
		    "aria": {
		        "sortAscending": ": activate to sort column ascending",
		        "sortDescending": ": activate to sort column descending"
		    },
		    "emptyTable": "此列表目前沒有資料",
		    "info": "顯示第 _START_ 到第 _END_ 則 共有 _TOTAL_ 則",
		    "infoEmpty": "沒有找到符合的資料",
		    "infoFiltered": "(從 _MAX_ 筆資料中過濾)",
		    "lengthMenu": "顯示 _MENU_ 則",
		    "search": "快速篩選:",
		    "zeroRecords": "目前沒有任何資料",
		    "paginate": {
		        "previous":"上一頁",
		        "next": "下一頁",
		        "last": "最後",
		        "first": "最前"
		    }
		},


		"lengthMenu": [
		    [10, 20, 50, -1],
		    [10, 20, 50, "All"] // change per page values here
		],
		// set the initial value
		"pageLength": 10,            
		"pagingType": "bootstrap_full_number",
		"columnDefs": [{  // set default column settings
		    'orderable': false,
		    'targets': [0]
		}, {
		    "searchable": false,
		    "targets": [0]
		}]
	});

	$(document).on('click','.btn_delete_one',function(){
		var id = $(this).attr('uid');
		var title = $(this).attr('utitle');
		bootbox.confirm("<h5>確定刪除"+title+"這個設備?</h5>", function(result) {
	       if(result === true){
	       		$.ajax({
	       			type  : 'DELETE',
	       			url   : "<?php echo e(action('InstEquipmentController@destroy' ,['id'=>''])); ?>/"+id,
	       			dataType : 'json',
	       			data  : {
	       				_token   : $('#_token').val(),
	       				id : id
	       			},
	       			success : function(){
	       				toastr['success']('刪除設備成功');
	       				window.location.reload();
	       			},
	       			error   : function(){
	       				toastr['error']('刪除設備失敗');
	       			}
	       		});
	       }
	    });
	});

	$('#table_list').find('.group-checkable').change(function () {
        var set = jQuery(this).attr("data-set");
        var checked = jQuery(this).is(":checked");
        jQuery(set).each(function () {
            if (checked) {
                $(this).attr("checked", true);
            } else {
                $(this).attr("checked", false);
            }
        });
        jQuery.uniform.update(set);
    });
	
	$(document).on('change','#table_list .checkboxes',function(){
    	var checkedNum = $('.checkboxes:checked').length;
		if(checkedNum>0){
			$('#btn_mass_delete').show();
		}else{
			$('#btn_mass_delete').hide();
		}
	});
	$(document).on('change','.group-checkable',function(){
		var checkedNum = $('.checkboxes:checked').length;
		if(checkedNum>0){
			$('#btn_mass_delete').show();
		}else{
			$('#btn_mass_delete').hide();
		}
	});

	$(document).on('click','#btn_mass_delete',function(){
		var ids = $.map($('input:checkbox:checked'), function(n, i){
		      return n.value;
		}).join(',');
		bootbox.confirm("您確定要刪除所選擇的項目?", function(result) {
			if(result == true){
				$.ajax({
					type : 'DELETE',
					dataType : 'json',
					url  : "<?php echo e(action('InstEquipmentController@destroy' ,['id'=>''])); ?>/"+ids,
					data : {
						_token   : $('#_token').val(),
						id : ids
					},
					success : function(sendback){
						toastr['success']('刪除成功');
						window.location.reload();
					},
					error : function(sendback){
						toastr['error']('刪除失敗，請重新整理後再試');
					}
				});
			}
		});
	});

$(document).on('click','#btn_tag',function(){
	$('#modal_tag').modal('show');
});

$(document).on('click','#btn_type_submit',function(){
	$.ajax({
		type : 'POST',
		dataType: 'json',
		url  : '<?php echo e(action('InstEquipmentController@updateType')); ?>',
		data : $('#form_type').serialize(),
		success : function(sendback){
			toastr['success']('更新成功!');
		},
		error : function(sendback){
			toastr['error']('更新失敗，請重新整理後再嘗試');
		}
	})
});
	
	$('#edit_form').validate({
		errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",  // validate all fields including form hidden input
		rules:{
			return:{
				required:true,
			}
		},
		messages:{
			return:{
				required:'請填入歸還人',
			}
		},

        invalidHandler: function (event, validator) { //display error alert on form submit
            $('#form_success_alert').hide();
            $('#form_error_alert').show();
            $('.modal').scrollTop($('#form_error_alert'));
        },

        highlight: function (element) { // hightlight error inputs
           $(element)
                .closest('.form-group').addClass('has-error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change done by hightlight
            $(element)
                .closest('.form-group').removeClass('has-error'); // set error class to the control group
        },

        success: function (label) {
            label
                .closest('.form-group').removeClass('has-error'); // set success class to the control group
        },

        submitHandler: function (form) {
            $('#form_success_alert').show();
            $('#form_error_alert').hide();
            //確認表單正確，Ajax送出
            $.ajax({
				type: 'POST',
				dataType: 'json',
				url: "<?php echo e(action('InstEquipmentController@returnEquip')); ?>",
				data: {
					_token   : $('#_token').val(),
					id   : $('#edit_form input[name = "id"]').val(),
					equipment   : $('#edit_form input[name = "equipment"]').val(),
					return : $('#return').val()
				},
				success: function(sendback){
					$('#modal_edit_return').modal('hide');
					window.location.reload();
				},
				error   : function ( data )
		        {
		            var errors = data.responseJSON;
		            //以下是目前測試到的可行解，但還是要實際嘗試才知道怎麼使用。
		            $.each(errors, function (key, data) {
					    console.log(key);
					    if( key == 'errors'){
					    	$.each(data, function (index, data) {
					    		$( "input[name='"+index+"']").closest('.form-group').addClass('has-error');
					    		$( "input[name='"+index+"']").closest('.form-control').after( '<span class="help-block help-block-error">'+data+'</span>' )
					    	});
					    }
					});
					$('#form_success_alert').hide();
            		$('#form_error_alert').show();
					$('.modal').scrollTop($('#form_error_alert'));

		        }
			});
        }
	});
	
	$(document).on('click','.btn_return',function(){
		var uid = $(this).attr('uid');
		$.ajax({
			type : 'POST',
			url  : "<?php echo e(action('InstEquipmentController@getEquip')); ?>",
			dataType : 'json',
			data : {
				_token   : $('#_token').val(),
				id : uid
			},
			success: function(sendback){
				$.each(sendback, function (key, data) {
						$( "#edit_form input[name='"+key+"']").val(data);
				});
				$('#modal_edit_return').modal('show');
			},
			error: function(){
				toastr['error']('發生錯誤，請重新整理後再嘗試');
			}
		})
	});
});

</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.manage', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>