@php
    $action_link = [];
    foreach($this->option['action_link'] as $k => $v) {
        if(!$v) continue;
        $v['url'] = str_replace("{controller}", $this->option['controller_url'], $v['url']);
        $action_link[$k] = $v;
        if(isset($action_link[$k]['display_in']) == false)
            $action_link[$k]['display_in'] = '';
    }
@endphp

<div id="{{$this->option['name']}}" class="ttable">
    <ul class="subsubsub">

    </ul><!--subsubsub-->

    <div class="row captiontable">
        <div class="col-sm-7 col-md-8">
            @php
                $dataHrefOption = '{';
                foreach($action_link as $k => $v) {
                    if($v['display_in'] == 'option') {
                        $dataHrefOption .= '"'.$v['title'].'":"'.$v['url'].'",';
                    }
                }
                if($dataHrefOption != '{')
                    $dataHrefOption = substr($dataHrefOption, 0, strlen($dataHrefOption) - 1);
                $dataHrefOption .= '}';
            @endphp

            
            @if($dataHrefOption != '{}')
            <div class="group-action">
                <select id="bulk-action-selector-top" class="fleft" data-ajax=".checkboxb.checked" data-href='{{$dataHrefOption}}' data-confirm="Bạn có chắc muốn <b>{value}</b> {item} {{$this->option['title']}}?" data-success-type="option" data-ajax-value="ischeck">
                    <option value="-1" selected="selected">- Hành động -</option>
                    
                    @foreach($action_link as $k => $v)
                        @if($v['display_in'] == 'option')
                            <option value="{{$v['title']}}" {{$k == 'deletes' ? 'data-success-removes="true"' : ''}}>{{$v['text']}}</option>
                        @endif
                    @endforeach
                </select>
                <input type="button" class="button fleft" data-target="#bulk-action-selector-top" value="Áp dụng">
            </div>
            @endif

            <?php
 if($this->option['filter_control']) {
 $filter_control = $this->option['filter_control'];
 foreach($filter_control as $v => $f) {
 if($f['type'] == 'column') {
 $cl = $f['column'];
 $colIndex = array_search($cl, array_keys($this->fields)) + 1;
 $dataFilter = '{"type":"column","column":"select","filtertype":"="}';
 $optionData = [];
 if($f['data'] == 'table-column') {
 foreach($this->data as $k => $vvv) {
 if(isset($vvv[$cl])) {
 $vv = $vvv[$cl];
 if(!array_key_exists($vv, $optionData)) $optionData[$vv] = [ 'text' => $vv, 'count' => 1 ];
 else $optionData[$vv]['count']++;
 }
 }
 }
 else {
 foreach($f['data'] as $kkk => $ddd) {
 $optionData[$kkk] = [ 'text' => $ddd ];
 }
 }
 $optionText = '';
 foreach($optionData as $k => $d) {
 $optionText .= '<option value="'.$k.'" data-id="'.$k.'" data-column="'.$colIndex.'">'.$d['text'] . (isset($d['count']) ? (' ('.$d['count'].')') : '').'</option>';
 }
 }
 else if($f['type'] == 'attr') {
 $dataFilter = '{"type":"attr","attr_name":"'.$f['column'].'"}';
 }
 else {
 continue;
 }
 if(isset($f['groupName'])) $groupName = ' data-group-filter="'.$f['groupName'].'" ';
 else $groupName = '';
 ?>

                    <div class="group-action">
                        <select style="width:120px" <?php echo $groupName ?> id="cfilter-by-<?php echo $v ?>" data-filter='<?php echo $dataFilter ?>'>
                            <option selected="selected" value="-1" data-id="-1"><?php echo $f['option_text'] ?></option>
                            <?php echo $optionText ?>
                        </select>
                        <input type="button" class="button fleft" data-target="#cfilter-by-<?php echo $v ?>" value="Lọc">
                    </div>

            <?php  }
 }
 ?>

           

        </div> <!--col left-->

        <div class="col-sm-5 col-md-4 captionright">
            <div class="gsearchtable">
                <input type="button" class="button fright" data-target="#filter-by-search" value="Tìm kiếm" />
                <div class="searchicon">
                    <?php
 $searchColumn = '';
 if($this->option['search_column'] != null) {
 foreach($this->option['search_column'] as $k => $v) {
 $index = 1;
 foreach($this->fields as $key => $value) {
 if($key == $v) {
 break;
 }
 $index++;
 }
 $searchColumn .= $index.',';
 }
 }
 else {
 $index = 1;
 foreach($this->fields as $key => $value) {
 if($this->option['hide_column'] != null && in_array($key, $this->option['hide_column'])) continue;
 if($value->type == \FIELD_DATA_TYPE::STRING || $value->type == \FIELD_DATA_TYPE::TEXT) {
 $searchColumn .= $index.',';
 }
 $index++;
 }
 }
 if($searchColumn != '') {
 $searchColumn = substr($searchColumn, 0, strlen($searchColumn) - 1);
 }
 ?>
                    <input type="text" id="filter-by-search" placeholder="Nhập nội dung cần tìm..." class="searchtable fright"
                        data-filter='{"type":"column","column":"all","fiter_column":[<?php echo $searchColumn ?>]}' />
                    <i title="Xóa nội dung tìm kiếm.">&times;</i>
                </div>
                <div class="clearfloat"></div>
            </div>
            <div class="clearfloat"></div>
        </div> <!--col search-->
    </div> <!--captiontable-->

    <table style="min-width:{{$this->option['min_width']}}">
        <thead>
            <tr>
                <th width="35px">
                    <span class="ascheckbox checkall center" data-target=".checkboxb"></span>
                </th>
                @foreach($this->fields as $key => $value)
                    <?php  if($this->option['hide_column'] != null && in_array($key, $this->option['hide_column'])) continue;
 $clName = false;
 if($this->option['header_text_column'] && in_array($key, $this->option['header_text_column'])) {
 $clName = $this->option['header_text_column'][$key];
 }
 else {
 $clName = $value->displayName;
 }
 ?>
                    <th <?php echo (empty($this->option['column_width'][$key]) == false ? ' width="'.$this->option['column_width'][$key].'" ' : '');
 echo ($this->option['sort_column'] != null && ($this->option['sort_column'] == "*" || in_array($key, $this->option['sort_column']))) ? 'class="tsort"' : '';
 ?> data-column-id="{{$key}}">{{$clName}}</th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @foreach($this->data as $key => $value)
                <tr>
                    <td>
                        <span class="ascheckbox center checkboxb" data-value="{{$value['id']}}"></span>
                    </td>
                    @php $index = 0; @endphp
                    @foreach($this->fields as $k => $v)
                        <?php
 if($this->option['hide_column'] != null && in_array($k, $this->option['hide_column'])) continue;
 if(!empty($this->option['custom_value_column'][$k])) {
 $column_value = call_user_func($this->option['custom_value_column'][$k], $value);
 }
 else {
 if(!isset($value[$k])) {
 $value[$k] = "";
 }
 $column_value = $value[$k];
 if($v->type == \FIELD_DATA_TYPE::STRING || $v->type == \FIELD_DATA_TYPE::TEXT) {
 $column_value = $column_value;
 }
 else if($v->type == \FIELD_DATA_TYPE::DATETIME) {
 if($v->format != null) {
 $column_value =date($v->format, strtotime($column_value));
 }
 else {
 $column_value =date('d/m/Y H:i', strtotime($column_value));
 }
 }
 else if($v->type == \FIELD_DATA_TYPE::DATE) {
 if($v->format != null) {
 $column_value =date($v->format, strtotime($column_value));
 }
 else {
 $column_value =date('d/m/Y', strtotime($column_value));
 }
 }
 else if($v->type == \FIELD_DATA_TYPE::TIME) {
 if($v->format != null) {
 $column_value =date($v->format, strtotime($column_value));
 }
 else {
 $column_value =date('H:i', strtotime($column_value));
 }
 }
 if($this->option['column_link'] != null && isset($this->option['column_link'][$k])) $column_value = '<a href="' . str_replace("{id}", $value['id'], $this->option['column_link'][$k]) . '">'.$column_value.'</a>';
 }
 ?>
                        @if($k != 'id')
                            @if(++$index == $this->option['action_link_column'])
                                <td>
                                    <?php
 switch($v->type) {
 case \FIELD_DATA_TYPE::BOOLEAN: echo '<span class="ascheckbox checkboxblock ' . ($value[$k] == 1 ? 'checked' : '') . '" data-background="none" data-ajax="true" data-href="/user/block" data-value="4" data-name="test"><i></i></span>';
 break;
 case \FIELD_DATA_TYPE::IMAGE: case \FIELD_DATA_TYPE::IMAGE_LIST: echo '<span><img src="'.asset($column_value).'" style="height: 50px" onerror="this.parentNode.removeChild(this)" /></span>';
 break;
 case \FIELD_DATA_TYPE::PRICE: if((int)$column_value == 0) {
 echo '<span>Liên hệ</span>';
 }
 else {
 echo '<span>' . number_format((int)$column_value) . '</span>';
 }
 break;
 default: echo '<span>' . $column_value . '</span>';
 break;
 }
 ?>
                                    <div class="row-action">
                                        @foreach($action_link as $kk => $item)
                                            @if($item['display_in'] == 'table-row')
                                                <span class="{{isset($item['class']) ? $item['class'] : ''}}">
                                                    @if($kk == 'delete')
                                                        <a href="#" class="event" data-ajax="true" data-href="{{$item['url']}}" data-value="{{$value['id']}}" data-name="{{isset($value['name']) ? $value['name'] : 'này'}}" data-success-remove="true" data-confirm="Bạn có chắc muốn <b>xóa</b> {{$this->option['title']}} <b>{name}</b>?" title="{{isset($item['title']) ? $item['title'] : ''}}">{{$item['text']}}</a>
                                                    @else
                                                        <a href="{{str_replace('{id}', $value['id'], $item['url'])}}" title="{{isset($item['title']) ? $item['title'] : ''}}">{{$item['text']}}</a>
                                                    @endif
                                                    <small>| </small>
                                                </span>
                                            @endif
                                        @endforeach
                                    </div>
                                </td>
                            @else
                                <?php
 switch($v->type) {
 case \FIELD_DATA_TYPE::BOOLEAN: if($this->option['checkbox_action'] && array_key_exists($k, $this->option['checkbox_action'])) {
 $in = $this->option['checkbox_action'][$k];
 $vId = $value['id'];
 $name = false;
 if(isset($value['name'])) {
 $name = $value['name'];
 }
 $confirm = false;
 if(isset($in['confirm'])) {
 $confirm = ' data-confirm="'.$in['confirm'].'"';
 }
 echo '<td><span class="ascheckbox checkboxblock ' . ($value[$k] == 1 ? 'checked' : '') . '" data-background="none" data-ajax="true" data-href="'. $in['url'] .'" data-value="'.$vId.'" data-name="'.$name.'"'.$confirm.'><i></i></span></td>';
 }
 else {
 echo '<td><span class="ascheckbox checkboxblock ' . ($value[$k] == 1 ? 'checked' : '') . ' disabled" style="border: 1px solid #b4b9be"><i></i></span></td>';
 }
 break;
 case \FIELD_DATA_TYPE::IMAGE: case \FIELD_DATA_TYPE::IMAGE_LIST: echo '<td><img src="'.asset($column_value).'" style="height: 50px" onerror="this.parentNode.removeChild(this)" /></td>';
 break;
 case \FIELD_DATA_TYPE::PRICE: if((int)$column_value == 0) {
 echo '<td>Liên hệ</td>';
 }
 else {
 echo '<td>' . number_format((int)$column_value) . '</td>';
 }
 break;
 default: echo '<td>' . $column_value . '</td>';
 break;
 }
 ?>
                            @endif
                        @endif
                    @endforeach
                </tr>
            @endforeach
        </tbody>
    </table>

</div> <!--ttable-->
 <!--confirmdialog-->
 <div class="modal fade" id="confirmbox" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">

                <h4 class="modal-title">Thông Báo</h4>
            </div>
            <div class="modal-body">
                <p id="confirmMessage">Any confirmation message?</p>
            </div>
            <div class="modal-footer">
                <button class="btn" id="confirmFalse">Cancel</button>
                <button class="btn btn-primary" id="confirmTrue">OK</button>
            </div>
        </div>
    </div>
</div>
<!--/confirmdialog-->
<div class="modal fade" id="alterdialog" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Thông Báo</h4>
            </div>
            <div class="modal-body">
                <p>Any confirmation message?</p>
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>
<div id="ajaxloader" style="display:none;position: fixed; top: 20px; left: 45%; z-index: 9999; box-shadow: 0px 0px 3px #1ABB9C ; border-radius: 3px; background-color: #1ABB9C ; width: 100px; padding: 10px 0px; opacity: 0.8; color: white; text-align: center">
    <img src="{{asset('/images/loader/ajax-loader.gif')}}" />
    <div style="height: 5px"></div>
    Đang Xử Lý
</div>


<script>
    window.addEventListener('load', function(){
        $(document).ready(function () {
            new TTable($("#{{$this->option['name']}}"), {
                "showcount": function (obj, sumitem) {
                    //obj.find(".subsubsub li:eq(0) .count").html('(' + sumitem + ')');
                }
            });
        });
    });
</script>