<?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-datepicker/css/bootstrap-datepicker3.min.css')); ?>" />
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css')); ?>" />
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/bootstrap-select/bootstrap-select.min.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')); ?>"/>
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/jquery-multi-select/css/multi-select.css')); ?>"/>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('page-content-wrapper'); ?>
	<style type="text/css">
		.th_display_none { display: none;}
        .disableInput {
            background-color: #ffee88 !important;
        }
        .readOnlyInput {
            background-color: #ffee88 !important;
        }
	</style>
	<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="index.html">首頁</a>
						<i class="fa fa-angle-right"></i>
					</li>
					<li>
						<a href="#">BA管理</a>
						<i class="fa fa-angle-right"></i>
					</li>
					<li>
						<a href="#">標籤參數管理</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-3">
                                        <div class="btn-group">
											<button id="btn_gov_config" class="btn green" <?php echo e(count($govConfig) > 0 ? "disabled='true'" : ""); ?>  >
											設定市府API
											</button>
                                        </div>
									</div>
								</div>									
							</div>
                            
                            <div class="modal-header">
                                <h4 class="modal-title">市府API</h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-body">
                                    <table class="table table-striped table-bordered table-hover" id="table_gov">
                                        <thead>
                                            <tr>
                                                <th>
                                                    市府名稱
                                                </th>
                                                <th>
                                                    URL
                                                </th>
                                                <th>
                                                    動作
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach($govConfig as $gov): ?>
                                            <tr class="odd gradeX">
                                                <td>
                                                    <?php echo e($gov['name']); ?>

                                                </td>
                                                <td>
                                                    <?php echo e($gov['value']); ?>

                                                </td>
                                                <td>
                                                    <div class="btn-group">
                                                        <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown" aria-expand ed="false">
                                                        <?php echo app('translator')->get('messages.action'); ?> <i class="fa fa-angle-down"></i>
                                                        </button>
                                                        <ul class="dropdown-menu pull-right" role="menu">
                                                            <li>
                                                                <a class="btn_edit" uid="<?php echo e($gov->id); ?>" href="javascript:;">
                                                                <i class="fa fa-pencil"></i>
                                                                <?php echo app('translator')->get('messages.edit'); ?> </a>
                                                            </li>
                                                            <li class="divider">
                                                            </li>
            
                                                            <li>
                                                                <a class="btn_del" uid="<?php echo e($gov->id); ?>" href="javascript:;">
                                                                <i class="fa fa-trash-o"></i>
                                                                <?php echo app('translator')->get('messages.del'); ?> </a>
                                                            </li>
                                                        </ul>
                                                    </div>
                                                </td>
                                            </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                    <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                                </div>
                            </div>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>

			<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-9">
                                        <div class="input-group">
                                            <span class="input-group-addon">API伺服器</span>
                                            <input type="text" class="form-control" value="<?php echo e(isset($serverConfig->value) ? $serverConfig->value : '未設定'); ?>" readonly>
                                            <div class="input-group-btn">
                                                <button tyep="button" id="btn_config" class="btn blue">設定API伺服器</button>
                                            </div>
                                        </div> 
									</div>
									<div class="col-md-3">
                                        <div class="btn-group">
											<a id="btn_catch_config" href="javascript:;" class="btn green">
											重新向伺服器請求設定 <i class="fa fa-refresh"></i>
											</a>
                                        </div>
									</div>
								</div>	
							</div>
                            <div class="modal-header">
                                <h4 class="modal-title">修改偵測器參數資料</h4>
                            </div>
                            <form id="form_edit" action="<?php echo e(route('manage::ba::add_sensor')); ?>" class="form-horizontal">
                                <input type="hidden" name="id" value="" />
                                <div class="modal-body">
                                    <div class="form-body">
                                        <?php foreach($paras as $para): ?>
                                            <div class="form-group">
                                                <label class="col-md-2 control-label para-no">參數<?php echo e($para->para_no); ?></label>
                                                <div class="col-md-3">
                                                    <input type="text" name="text[<?php echo e($para->para_no); ?>]" class="form-control para-text" value="<?php echo e($para->text); ?>">   
                                                </div>
                                                <div class="col-md-7">
                                                    <label class="radio-inline">
                                                        <input type="radio" name="represent[<?php echo e($para->para_no); ?>]" class="form-control represent" value="none" <?php echo e(($para->represent == 0) ? 'checked' : null); ?>>
                                                        無
                                                    </label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="represent[<?php echo e($para->para_no); ?>]" class="form-control represent" value="tag" <?php echo e(($para->represent == 1) ? 'checked' : null); ?>>
                                                        為標籤來源
                                                    </label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="represent[<?php echo e($para->para_no); ?>]" class="form-control represent" value="value" <?php echo e(($para->represent == 2) ? 'checked' : null); ?>>
                                                        為數據來源
                                                    </label>
                                                    <label class="radio-inline">
                                                        <input type="radio" name="represent[<?php echo e($para->para_no); ?>]" class="form-control represent" value="trigger_time" <?php echo e(($para->represent == 3) ? 'checked' : null); ?>>
                                                        為時間來源
                                                    </label>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                        <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" class="btn green btn_query_submit" value="add">修改</button>
                                </div>
                            </form>                            
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>
		</div>
        <div class="modal fade draggable-modal" id="modal_api_config" 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">設定API伺服器</h4>
                    </div>
                    <form id="form_config" action="" class="form-horizontal">
                        <input type="hidden" name="id" value="" />
                        <div class="modal-body">
                            <div class="form-body">
                                <div class="form-group">
                                    <label class="col-md-3 control-label">伺服器URL</label>
                                    <div class="col-md-9">
                                        <input type="text" name="value" class="form-control">   
                                    </div>
                                </div>                                
                                <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn dark btn-outline" data-dismiss="modal">關閉</button>
                            <button type="submit" class="btn green btn_query_submit" value="add">設定</button>
                        </div>
                    </form>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>

        <?php /* 市府API */ ?>
        <div class="modal fade draggable-modal" id="modal_gov_config" 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">設定市府API</h4>
                    </div>
                    <form id="form_config_gov" action="" class="form-horizontal">
                        <input type="hidden" name="id" value="" />
                        <div class="modal-body">
                            <div class="form-body">
                                <div class="form-group">
                                    <label class="col-md-3 control-label">伺服器URL</label>
                                    <div class="col-md-9">
                                        <input type="text" name="value" class="form-control">   
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-3 control-label">市府</label>
                                    <div class="col-md-9">
                                        <input type="text" name="government" class="form-control">   
                                    </div>
                                </div>
                                <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn dark btn-outline" data-dismiss="modal">關閉</button>
                            <button type="submit" class="btn green btn_query_submit" value="add">設定</button>
                        </div>
                    </form>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('plugins_page_level'); ?>
    <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/jquery-validation/js/additional-methods.min.js')); ?>"></script>
    <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/moment.min.js')); ?>"></script>
    <script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.js')); ?>"></script>
    <script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.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/jquery-multi-select/js/jquery.multi-select.js')); ?>"></script>
    <script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/select2/js/select2.full.js')); ?>"></script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('js_page_level'); ?>
    <script>
        $(document).ready(function(){
            
            $.fn.modal.Constructor.prototype.enforceFocus = function () {};
            $('.represent').click(function() {
                if ($(this).val() == 'none') return;
                var radio = $('.represent[value="' + $(this).val() + '"]:checked').not(this);
                radio.each(function() {
                    $(this).attr('checked', false);
                    $(this).parents('span').removeClass('checked');

                    $('[name="' + $(this).attr('name') + '"][value="none"]').attr('checked', true);
                    $('[name="' + $(this).attr('name') + '"][value="none"]').parents('span').addClass('checked');
                });
            })
            $('#btn_config').click(function(){
                $.ajax({
                    type : 'GET',
                    url  : "<?php echo e(route('manage::ba::get_sensor_api_config')); ?>",
                    dataType : 'json',
                    data : {
                        'type' : 'para_config_server'
                    },
                    success: function(sendback){
                        if (typeof sendback.value == 'undefined') {
                            $('#form_config [name="id"]').val(''); 
                            $('#form_config [name="value"]').val('');    
                        } else {
                            $('#form_config [name="id"]').val(sendback.id); 
                            $('#form_config [name="value"]').val(sendback.value); 
                        }
                        
                        $('#modal_api_config').modal('show');
                    },
                    error: function(){
                        toastr['error']('發生錯誤，請重新整理後再嘗試');
                    }
                });
            });
            $('#btn_catch_config').click(function() {
                bootbox.confirm("您確定要向伺服器重新請求設定嗎?", function(result) {
                    if(result == true){
                        $.ajax({
                            type : 'POST',
                            dataType : 'json',
                            url  : "<?php echo e(route('manage::ba::catch_para_config')); ?>",
                            data : {
                                _token : '<?php echo e(csrf_token()); ?>'
                            },
                            success : function(sendback){
                                window.location.reload();
                            },
                            error : function(sendback){
                                if (typeof sendback.responseJSON.message != 'undefined') {
                                    toastr['error'](sendback.responseJSON.message);
                                } else {
                                    toastr['error']('請求失敗，請重新整理後再試');    
                                }
                            }
                        })
                    }
                });
            });

            $('#form_config').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:{
                    value:{
                        required:true,
                        maxlength: 255
                    }
                },
                messages:{
                    value:{
                        required: '伺服器URL不可為空',
                        maxlength: '伺服器URL不可以超過255個字元'
                    }
                },

                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();
                    
                    var data = $("#form_config").serialize();
                    
                    //確認表單正確，Ajax送出
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: "<?php echo e(route('manage::ba::update_sensor_api_config')); ?>",
                        data: data,
                        success: function(sendback){
                            $('#modal_api_config').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>' )
                                    });
                                } else if (key == 'message') {
                                    toastr['error'](data);    
                                }
                            });
                            $('#form_success_alert').hide();
                            $('#form_error_alert').show();
                            $('.modal').scrollTop($('#form_error_alert'));
                        }
                    });
                }
            });
            
            $('#form_edit').submit(function(){
                // 檢查內容
                var errors   = '';
                var paraText = $('.para-text');
                paraText.each(function() {
                    if ($(this).val().trim().length == 0) {
                        errors += $(this).parents('.form-group').find('.para-no').text() + "的文字敘述不能空白<br />";  
                    }    
                });
                
                if ($('.represent[value="tag"]:checked').length == 0) {
                    errors += "需設定一個參數作為標籤來源<br />"; 
                }
                if ($('.represent[value="value"]:checked').length == 0) {
                    errors += "需設定一個參數作為數據來源<br />"; 
                }
                
                if (errors.length > 0) {
                    toastr['error'](errors);   
                    return false;
                }
                
                var data = $("#form_edit").serialize();
                
                //確認表單正確，Ajax送出
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: "<?php echo e(route('manage::ba::update_para')); ?>",
                    data: data,
                    success: function(sendback){
                        window.location.reload();
                    },
                    error   : function ( data )
                    {
                        var errors = data.responseJSON;
                        //以下是目前測試到的可行解，但還是要實際嘗試才知道怎麼使用。
                        $.each(errors, function (key, data) {
                            console.log(key);
                            if (key == 'message') {
                                toastr['error'](data);    
                            }
                        });
                        $('#form_success_alert').hide();
                        $('#form_error_alert').show();
                    }
                });
                
                return false;
            });
        });

        // 市府URL設定
        $('#btn_gov_config').click(function(){
            $('#modal_gov_config').modal('show');
        })

        $('#form_config_gov').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:{
                value:{
                    required:true,
                    maxlength: 255
                }
            },
            messages:{
                value:{
                    required: '伺服器URL不可為空',
                    maxlength: '伺服器URL不可以超過255個字元'
                }
            },

            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();
                
                var data = $("#form_config_gov").serialize();
                
                //確認表單正確，Ajax送出
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: "<?php echo e(route('manage::ba::update_sensor_api_config')); ?>",
                    data: data,
                    success: function(sendback){
                        $('#modal_gov_config').modal('hide');
                        window.location.reload();
                    },
                    error   : function ( data )
                    {                            
                    }
                });
            }
        });

        $(".btn_edit").click(function(){
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url : "<?php echo e(route('manage::ba::get_sensor_api_config')); ?>",
                data : {
                    id : $(this).attr('uid'),
                    type : 'government'
                },
                success: function(sendback){
                    if (typeof sendback.value == 'undefined') {
                        $('#form_config_gov [name="id"]').val(''); 
                        $('#form_config_gov [name="value"]').val('');
                        $('#form_config_gov [name="government"]').val(''); 
                    } else {
                        $('#form_config_gov [name="id"]').val(sendback.id); 
                        $('#form_config_gov [name="value"]').val(sendback.value);
                        $('#form_config_gov [name="government"]').val(sendback.name); 
                    }
                    
                    $('#modal_gov_config').modal('show');
                },
                error: function(sendback){

                }
            })
        });

        $(".btn_del").click(function(){
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url : "<?php echo e(route('manage::ba::delete_sensor_api_config')); ?>",
                data : {
                    id : $(this).attr('uid'),
                    type : 'government',
                    _token : '<?php echo e(csrf_token()); ?>'
                },
                success: function(sendback){
                    window.location.reload();
                },
                error: function(sendback){

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