<?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/datatables/media/css/buttons.dataTables.min.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')); ?>"/>
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.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;
        }
        .portlet.light .dataTables_wrapper .dt-buttons {
            margin-top: 0px;
        }
        .dataTables_wrapper .dt-buttons {
            float: left;
        }
	</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="#">取得資產設備建立</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-6">
                                        <div class="btn-group">
											<a id="btn_add_assets" href="javascript:;"  class="btn green">
											新增 <i class="fa fa-plus"></i>
											</a>
                                        </div>
                                        <button id="btn_mass_delete" class="btn red" style="display:none;"> 刪除 <i class="fa fa-trash"></i></button>
									</div>
								</div>
									
							</div>
							<table class="table table-striped table-bordered table-hover" id="table_list">

							</table>
						</div>
					</div>
					<!-- END EXAMPLE TABLE PORTLET-->
				</div>
			</div>
		</div>
        <div class="modal fade draggable-modal" id="modal_add_assets" role="basic" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <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>
                    <form id="form_add" class="form-horizontal" enctype="multipart/form-data" action="<?php echo e(route('manage::asset::add_assets')); ?>">
                        <div class="modal-body">
                            <div class="form-body">

                            </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 class="modal fade draggable-modal" id="modal_edit_assets" role="basic" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <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>
                    <form id="form_edit" class="form-horizontal" enctype="multipart/form-data" action="<?php echo e(route('manage::asset::update_assets')); ?>">
                        <input type="hidden" name="id" value="" />
                        <div class="modal-body">
                            <div class="form-body">

                            </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 id="formTemplate" style="display:none;">
        <blockquote>
                                <h4>Step 1. 設備資訊</h4>
                            </blockquote>
            <div class="form-group">
                <label class="col-md-2 control-label">資產編號</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="no" class="form-control" placeholder="請輸入資產編號">   
                </div>
                </div>                       
                <label class="col-md-2 control-label">廠牌型號</label>
                <div class="col-md-4">
                <div class="input-icon">
                     <i class="fa fa-pencil"></i>
                    <input type="text" name="model" class="form-control" placeholder="請輸入廠牌型號">   
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">資產名稱</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="asset_name" class="form-control" placeholder="請輸入資產名稱">   
                </div>
                </div>
                <label class="col-md-2 control-label">分類類別</label>
                <div class="col-md-4">
                    <select name="c_id" class="form-control mySelect2">   
                        <?php foreach($assetCategories as $category): ?>
                            <option value="<?php echo e($category->id); ?>"><?php echo e($category->name); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">規格明細</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <textarea name="specification" class="form-control" placeholder="請輸入規格明細"></textarea>   
                </div>
                </div>
                <label class="col-md-2 control-label">附屬設備</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <textarea name="affiliated_asset" class="form-control" placeholder="請輸入附屬設備"></textarea>   
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">產品序號</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="sn" class="form-control" placeholder="請輸入產品序號">   
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">取得日期</label>
                <div class="col-md-4">
                    <div class="input-group date form_datetime date-picker" data-date-format="yyyy-mm-dd">
                        <input type="text" name="purchase_date" readonly class="form-control"> 
                        <span class="input-group-btn">
                            <button class="btn default date-set" type="button"><i class="fa fa-calendar"></i></button>
                        </span>
                    </div>  
                </div>
                <label class="col-md-2 control-label">取得來源</label>
                <div class="col-md-4">
                    <select name="as_id" class="form-control mySelect2">
                        <?php foreach($acquiredSources as $source): ?>
                            <option value="<?php echo e($source->id); ?>"><?php echo e($source->source); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">取得成本</label>
                <div class="col-md-2">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="costs" class="form-control" placeholder="請輸入成本">   
                </div>
                </div>
                <label class="col-md-2 control-label">耐用年限</label>
                <div class="col-md-2">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="age_limit" class="form-control" placeholder="請輸入年限">   
                </div>
                </div>
                <label class="col-md-2 control-label">現值</label>
                <div class="col-md-2">
                    <input type="text" name="depreciation_price" class="form-control readOnlyInput" value="0">   
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">取得原因</label>
                <div class="col-md-4" style="p">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="purchase_reason" class="form-control" placeholder="請輸入取得原因">   
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">產品現況</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <textarea name="situation" class="form-control" placeholder="請輸入產品現況"></textarea>  
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">保管人</label>
                <div class="col-md-4">
                    <select name="custodian" class="form-control mySelect2"> 
                        <option value="">未選擇</option>
                        <?php foreach($staffs as $staff): ?>
                            <option value="<?php echo e($staff->id); ?>"><?php echo e($staff->employee_no.'  '.$staff->name); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <label class="col-md-2 control-label">姓名</label>
                <div class="col-md-4">
                    <input type="text" name="custodian_name" class="form-control disableInput">  
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">部門群組</label>
                <div class="col-md-2">
                    <input type="text" name="department_no" class="form-control disableInput"> 
                </div>
                <div class="col-md-2">
                    <input type="text" name="department_name" class="form-control disableInput"> 
                </div>
                <label class="col-md-2 control-label">職務名稱</label>
                <div class="col-md-4">
                    <input type="text" name="title" class="form-control disableInput">  
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">存放位置</label>
                <div class="col-md-4">
                    <select name="sp_id" class="form-control mySelect2"> 
                        <?php foreach($storagePlaces as $place): ?>
                            <option value="<?php echo e($place->id); ?>"><?php echo e($place->place); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <blockquote>
                <h4>Step 2. 保固資訊</h4>
            </blockquote>
            <div class="form-group">
                <label class="col-md-2 control-label">銷售/維修廠商</label>
                <div class="col-md-4">
                <div class="input-icon">
                <select name="f_id" class="form-control mySelect2">   
                    <?php foreach($firms as $firm): ?>
                        <option value="<?php echo e($firm->id); ?>"><?php echo e($firm->no.'-'.$firm->name); ?></option>
                    <?php endforeach; ?>
                </select>
                </div>
                </div>
                <label class="col-md-2 control-label">聯絡人</label>
                <div class="col-md-4">
                    <div class="input-icon">
                        <i class="fa fa-user"></i>
                        <input type="text" name="repairer_contact" class="form-control" placeholder="請輸入聯絡人"> 
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">維修電話</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-phone"></i>
                    <input type="text" name="repairer_tel" class="form-control" placeholder="請輸入維修電話"> 
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">保固方式</label>
                <div class="col-md-4">
                <div class="input-icon">
                                        <i class="fa fa-pencil"></i>
                    <input type="text" name="warranty_type" class="form-control" placeholder="請輸入保固方式"> 
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">保固到期日</label>
                <div class="col-md-4">
                <div class="input-group date form_datetime date-picker" data-date-format="yyyy-mm-dd">
                    <input type="text" name="warranty_exp_date" readonly class="form-control"> 
                    <span class="input-group-btn">
                        <button class="btn default date-set" type="button"><i class="fa fa-calendar"></i></button>
                    </span>
                </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">圖片</label>
                <div class="col-md-8 imageList">
                    <div class="fileinput fileinput-new" data-provides="fileinput" >
                        <input type="hidden" name="image_id" value="" />
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 269.97px; height: 150px;">
                            <img src="" name="image_url" alt=""/>
                        </div>
                        <div>
                            <span class="btn default btn-file">
                            <span class="fileinput-new">
                            選擇圖片 </span>
                            <span class="fileinput-exists">
                            更換 </span>
                            <input type="file" name="image">
                            </span>
                            <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput">
                            移除 </a>
                        </div>
                    </div>
                </div>
            </div>
            <input type="hidden" id="_token" name="_token" value="<?php echo e(csrf_token()); ?>">
        </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.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/jquery-form/jquery.form.min.js')); ?>"></script>

    <script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/media/buttons/dataTables.buttons.min.js')); ?>"></script>
    <script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/media/buttons/buttons.print.min.js')); ?>"></script>

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

<?php $__env->startSection('js_page_level'); ?>
    <script>
        $(document).ready(function(){
            callDataTable();
        });
        
        function initAction() {
            $.fn.modal.Constructor.prototype.enforceFocus = function () {};
            $('#modal_add_assets form').find('.form-body').html($('#formTemplate').html());
            $('#modal_edit_assets form').find('.form-body').html($('#formTemplate').html());
            $('#modal_edit_assets [name="custodian"]').addClass('disableInput');
            $(".mySelect2").select2();
            $('.disableInput').prop('disabled', true);
            $('.readOnlyInput').prop('readonly', true);
            $('.show-tooltip').tooltip();
            $('#type').multiSelect();
            $('#source').multiSelect();
            $('.date-picker').datepicker({
                orientation: "left",
                autoclose: true,
                dateFormat: 'yyyy-mm-dd'
            });
            $(document).on('click','#btn_add_assets',function(){
                $('#modal_add_assets').modal('show');
            });
            $(document).on('click', '#btn_mass_delete', function(){
                var ids = [];
                $('.checkboxes:checked').each(function() {
                    ids.push($(this).val());                      
                });   
                deleteRecords(ids);
            });
            $('[name="costs"]').change(function() {
                refreshDepreciationPrice($(this).parents('form'));    
            });
            $('[name="age_limit"]').change(function() {
                refreshDepreciationPrice($(this).parents('form'));    
            });
            $('[name="purchase_date"]').change(function() {
                refreshDepreciationPrice($(this).parents('form'));    
            });
            $('.btn_delete').click(function() {
                var id = $(this).attr('uid');
                deleteRecords(id);
            });
            $('.mySelect2[name="custodian"]').change(function() {
                var form = $(this).parents('form');
                var data = $(this).select2('data');
                if (typeof data[0] == 'undefined') return;
                
                $.ajax({
                    type : 'GET',
                    url  : "<?php echo e(route('manage::asset::get_custodian')); ?>",
                    dataType : 'json',
                    data : {
                        'id' : $(this).val()
                    },
                    success: function(sendback){
                        $.each(sendback, function(key, value) {
                            if (key == 'id') { 
                                key = 'custodian';
                                var obj = $("[name='" + key + "']", form);
                                $(obj).val(value); 
                            }
                        });
                    },
                    error: function(){
                        toastr['error']('發生錯誤，請重新整理後再嘗試');
                    }
                });
            });
            $('.btn_edit').click(function(){
                var uid = $(this).attr('uid');
                $("#form_edit [name='image']").val('');
                $("#form_edit [name='image_id']").val('');
                $("#form_edit [name='image_url']").attr('src', '');
                
                $.ajax({
                    type : 'GET',
                    url  : "<?php echo e(route('manage::asset::get_assets')); ?>",
                    dataType : 'json',
                    data : {
                        'id' : uid
                    },
                    success: function(sendback){
                        $.each(sendback, function(key, value) {
                            var obj = $("#form_edit [name='" + key + "']");
                            if ($(obj).is('img')) {
                                $(obj).attr('src', value);
                            } else if ($(obj).hasClass('mySelect2')) {
                                $(obj).select2("val", value); 
                            } else {
                                $(obj).val(value);     
                            } 
                            if (key == 'no' || key == 'custodian') {
                                if (value != '' && value != '0') {
                                    $('#modal_edit_assets [name="' + key + '"]').prop('disabled', true);    
                                } else {
                                    $('#modal_edit_assets [name="' + key + '"]').prop('disabled', false);
                                }
                            }    
                        });
                        refreshDepreciationPrice('#form_edit');
                        $('#modal_edit_assets').modal('show');
                    },
                    error: function(){
                        toastr['error']('發生錯誤，請重新整理後再嘗試');
                    }
                });
            });
            
            $('#form_add').ajaxForm({
                type: "POST",
                dataType: "json",
                beforeSubmit: function () {
                    $('#form_success_alert').show();
                    $('#form_error_alert').hide();
                    return $("#form_add").valid();
                },
                success: function (returnData) {
                    $('#modal_add_assets').modal('hide');
                    window.location.reload();
                },
                error: function(){
                    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') {
                                    alert(data);    
                                }
                    });
                    $('#form_success_alert').hide();
                    $('#form_error_alert').show();
                    $('.modal').scrollTop($('#form_error_alert'));
                }
            });
            
            $('#form_add').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:{
                    asset_name:{
                        required:true,
                        maxlength:50
                    },
                    specification:{
                        required:true
                    },     
                    model:{
                        required:true,
                        maxlength:50
                    },         
                    affiliated_asset:{
                        required:true
                    },  
                    sn:{
                        required:true,
                        maxlength:50
                    },             
                    c_id:{
                        required:true
                    },              
                    purchase_date:{
                        required:true,
                        date:true
                    },       
                    as_id:{
                        required:true
                    },              
                    age_limit:{
                        required:true,
                        number:true
                    },           
                    costs:{
                        required:true,
                        number:true
                    },               
                    depreciation_price:{
                        required:true,
                        number:true
                    },  
                    purchase_reason:{
                        required:true,
                        maxlength:50
                    }, 
                    situation:{
                        required:true
                    },          
                    custodian:{
                        required:true
                    },           
                    sp_id:{
                        required:true
                    },                  
                    repairer_tel:{
                        required:true,
                        maxlength:15
                    },    
                    repairer_contact:{
                        required:true,
                        maxlength:20
                    }, 
                    warranty_type:{
                        required:true,
                        maxlength:20
                    },   
                    warranty_exp_date:{
                        required:true,
                        date:true
                    }
                },
                messages:{
                    asset_name:{
                        required:'資產名稱不可為空',
                        maxlength:'資產編號長度不能超過50個字元'
                    },
                    specification:{
                        required:'規格明細不可為空'
                    },     
                    model:{
                        required:'廠牌型號不可為空',
                        maxlength:'廠牌型號長度不能超過50個字元'
                    },         
                    affiliated_asset:{
                        required:'附屬設備不可為空'
                    },  
                    sn:{
                        required:'產品序號不可為空',
                        maxlength:'產品序號長度不能超過50個字元'
                    },             
                    c_id:{
                        required:'分類類別未選擇'
                    },              
                    purchase_date:{
                        required:'取得日期未輸入',
                        date:'取得日期必需是日期格式'
                    },       
                    as_id:{
                        required:'取得來源未選擇'
                    },              
                    age_limit:{
                        required:'耐用年限不可為空',
                        number:'耐用年限必需為數字格式'
                    },           
                    costs:{
                        required:'取得成本不可為空',
                        number:'取得成本必需為數字格式'
                    },               
                    depreciation_price:{
                        required:'現值不可為空',
                        number:'現值必需為數字格式',
                    },  
                    purchase_reason:{
                        required:'取得原因不可為空',
                        maxlength:'取得原因長度不能超過50個字元'
                    }, 
                    situation:{
                        required:'產品現況不可為空'
                    },          
                    custodian:{
                        required:'保管人未輸入'
                    },           
                    sp_id:{
                        required:'放置位置未選擇'
                    },                   
                    repairer_tel:{
                        required:'維修電話不可為空',
                        maxlength:'維修電話長度不能超過15個字元'
                    },    
                    repairer_contact:{
                        required:'聯絡人不可為空',
                        maxlength:'聯絡人長度不能超過20個字元'
                    }, 
                    warranty_type:{
                        required:'保固方式不可為空',
                        maxlength:'保固方式長度不能超過20個字元'
                    },   
                    warranty_exp_date:{
                        required:'保固到期日不可為空',
                        date:'保固到期日必需是日期格式'
                    }
                },

                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
                },
            });
            
            $('#form_edit').ajaxForm({
                type: "POST",
                dataType: "json",
                beforeSubmit: function () {
                    $('#form_success_alert').show();
                    $('#form_error_alert').hide();
                    return $("#form_edit").valid();
                },
                success: function (returnData) {
                    $('#modal_edit_assets').modal('hide');
                    window.location.reload();
                },
                error: function(){
                    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').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:{
                    asset_name:{
                        required:true,
                        maxlength:50
                    },
                    specification:{
                        required:true
                    },             
                    affiliated_asset:{
                        required:true
                    },  
                    sn:{
                        required:true,
                        maxlength:50
                    },             
                    c_id:{
                        required:true
                    },              
                    purchase_date:{
                        required:true,
                        date:true
                    },       
                    as_id:{
                        required:true
                    },              
                    age_limit:{
                        required:true,
                        number:true
                    },           
                    costs:{
                        required:true,
                        number:true
                    },               
                    depreciation_price:{
                        required:true,
                        number:true
                    },  
                    purchase_reason:{
                        required:true,
                        maxlength:50
                    }, 
                    situation:{
                        required:true
                    },          
                    custodian:{
                        required:true
                    },           
                    sp_id:{
                        required:true
                    },              
                    repairer:{
                        required:true,
                        maxlength:50
                    },        
                    repairer_tel:{
                        required:true,
                        maxlength:15
                    },    
                    repairer_contact:{
                        required:true,
                        maxlength:20
                    }, 
                    warranty_type:{
                        required:true,
                        maxlength:20
                    },   
                    warranty_exp_date:{
                        required:true,
                        date:true
                    }
                },
                messages:{
                    asset_name:{
                        required:'資產名稱不可為空',
                        maxlength:'資產編號長度不能超過50個字元'
                    },
                    specification:{
                        required:'規格明細不可為空'
                    },            
                    affiliated_asset:{
                        required:'附屬設備不可為空'
                    },  
                    sn:{
                        required:'產品序號不可為空',
                        maxlength:'產品序號長度不能超過50個字元'
                    },             
                    c_id:{
                        required:'分類類別未選擇'
                    },              
                    purchase_date:{
                        required:'取得日期未輸入',
                        date:'取得日期必需是日期格式'
                    },       
                    as_id:{
                        required:'取得來源未選擇'
                    },              
                    age_limit:{
                        required:'耐用年限不可為空',
                        number:'耐用年限必需為數字格式'
                    },           
                    costs:{
                        required:'取得成本不可為空',
                        number:'取得成本必需為數字格式'
                    },               
                    depreciation_price:{
                        required:'現值不可為空',
                        number:'現值必需為數字格式',
                    },  
                    purchase_reason:{
                        required:'取得原因不可為空',
                        maxlength:'取得原因長度不能超過50個字元'
                    }, 
                    situation:{
                        required:'產品現況不可為空'
                    },          
                    custodian:{
                        required:'保管人未輸入'
                    },           
                    sp_id:{
                        required:'放置位置未選擇'
                    },              
                    repairer:{
                        required:'銷售/維修廠商不可為空',
                        maxlength:'銷售/維修廠商長度不能超過50個字元'
                    },        
                    repairer_tel:{
                        required:'維修電話不可為空',
                        maxlength:'維修電話長度不能超過15個字元'
                    },    
                    repairer_contact:{
                        required:'聯絡人不可為空',
                        maxlength:'聯絡人長度不能超過20個字元'
                    }, 
                    warranty_type:{
                        required:'保固方式不可為空',
                        maxlength:'保固方式長度不能超過20個字元'
                    },   
                    warranty_exp_date:{
                        required:'保固到期日不可為空',
                        date:'保固到期日必需是日期格式'
                    }
                },

                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
                }
            });
        };

        var deleteRecords = function(id) {
            bootbox.confirm("您確定要刪除指定的資產設備?", function(result) {
                if(result == true){
                    $.ajax({
                        type : 'DELETE',
                        dataType : 'json',
                        url  : "<?php echo e(route('manage::asset::delete_assets')); ?>",
                        data : {
                            _token : $('#_token').val(),
                            id     : id
                        },
                        success : function(sendback){
                            window.location.reload();
                        },
                        error : function(sendback){
                            toastr['error']('刪除失敗，請重新整理後再試');
                        }
                    })
                }
            }); 
        }
        
        var callDataTable = function () {
            var table = $('#table_list');

            table.on( 'draw.dt', function () {
                initAction();
            });
            
            var oTable = table.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": "最前"
                    }
                },
                "aoColumns":[
                    {"sTitle":"<input type=\"checkbox\" class=\"group-checkable\" data-set=\"#table_list .checkboxes\"/>", "sClass" : "table-checkbox"},
                    {"sTitle":"類別", "sClass": "printColumn"},
                    {"sTitle":"資產編號", "sClass": "printColumn"},
                    {"sTitle":"資產名稱", "sClass": "printColumn"},
                    {"sTitle":"廠牌型號", "sClass": "printColumn"},
                    {"sTitle":"產品序號", "sClass": "printColumn"},
                    {"sTitle":"取得日期", "sClass": "printColumn"},
                    {"sTitle":"取得成本", "sClass": "printColumn"},
                    {"sTitle":"目前狀態", "sClass": "printColumn"},
                    {"sTitle":"動作"},
                ],
                "bStateSave": false, // save datatable state(pagination, sort, etc) in cookie.
                "lengthMenu": [
                    [10, 20, 100, -1],
                    [10, 20, 100, "All"] // change per page values here
                ],
                // set the initial value
                "pageLength": 100,
                "aaData":[<?php echo $assetArray; ?>],
                "dom": 'lBfrtip',
                "buttons": [
                    {
                        extend: 'print',
                        text: '列印',
                        message: '<h4>資產設備列表</h4>',
                        exportOptions: {
                            columns: '.printColumn'
                        }
                    }
                ],
            });

            $('.group-checkable', table).change(function () {
                var set     = jQuery(this).attr("data-set");
                var checked = jQuery(this).is(":checked");
                jQuery(set).each(function () {
                    if (checked) {
                        $('#btn_mass_delete').show();
                        $(this).attr("checked", true);
                    } else {
                        $('#btn_mass_delete').hide();
                        $(this).attr("checked", false);
                    }
                });
                jQuery.uniform.update(set);
            });

            $('.checkboxes', table).change(function(){
                var checkedNum = $('.checkboxes:checked').length;
                if(checkedNum > 0){
                    $('#btn_mass_delete').show();
                }else{
                    $('#btn_mass_delete').hide();
                }
            });
        }   
        
        var refreshDepreciationPrice = function(form)
        {
            var price        = 0; 
            var purchaseDate = $('[name="purchase_date"]', form).val();
            var costs        = $('[name="costs"]', form).val();
            var ageLimit     = $('[name="age_limit"]', form).val();
            var today        = new Date();
            var lastDate;
            
            try {
                purchaseDate = purchaseDate.split("-");
                
                lastDate     = new Date(parseInt(purchaseDate[0]) + parseInt(ageLimit), purchaseDate[1] - 1, purchaseDate[2]);
                purchaseDate = new Date(purchaseDate[0], purchaseDate[1] - 1, purchaseDate[2]);
                
                if (today < lastDate) {
                    price = costs / ((lastDate - purchaseDate) / (24 * 60 * 60 * 1000));
                    price = (lastDate - today) / (24 * 60 * 60 * 1000) * price;
                }
               
                $('[name="depreciation_price"]', form).val(Math.ceil(price));
                
            } catch(e) {
                
            }
        }
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.manage', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>