@extends('layouts.master')
  @section('page_title')
      @if(isset($page_title))
          {{ $page_title }}
      @endif
  @endsection
  @section('css')
  <link href="{!! url('public/assets/vendors/tabulator/css/bootstrap/tabulator_bootstrap.css') !!}" type="text/css" rel="stylesheet">
  @endsection
@section('content')
@include('shortcodes.partials.create')
@include('client.partials.edit-shortcodes')

  <div class="">
      <div class="page-title">
        <div class="title_left">
          <div class="title_left">
            <ol class="breadcrumb">
              <li><a href="{!! url('dashboard') !!}">Dashboard</a></li>
              <!-- <li><a href="{!! url('clients') !!}">Clients</a></li> -->
              <li class="active">SMS Short Codes </li>
            </ol>
          </div>
        </div>

        <div class="title_right">


        </div>
      </div>

      <div class="clearfix"></div>
      <div class="row">
        @include('commons.notifications')
        <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
          <div class="x_title">
            <h2> Client Short Codes </h2>
            <div class="pull-right">
                <button class="btn btn-primary btn-sm" id="createSmsShortCodeBtn"><i class="fa fa-plus-circle"></i> Add Short Code</button>
            </div>
            <div class="clearfix"></div>
          </div>

          <div class="x_content">

                <div role="tabpanel" class="tab-pane fade active in" id="tab_sms" aria-labelledby="sms-tab">
                  <div class="pull-left">
                    <button id="smscodes-download-xlsx" class="btn btn-success btn-sm"><i class="fa fa-file-excel-o"></i> Download XLSX</button>
                    <button id="smscodes-download-pdf" class="btn btn-danger btn-sm"><i class="fa fa-file-pdf-o"></i> Download PDF</button>
                    </div>
                    <div class="pull-right">
                      <form method="GET" action="{!! url('shortcodes') !!}">
                        <input type="hidden" name="code_type" value="sms">
                        <div class="col-md-5 col-sm-5 col-xs-12 form-group">
                          <div style="margin-top:1px; margin-right:-90px;" class="top_search">
                          </div>
                        </div>
                        <div class="col-md-12 form-group pull-left top_search" style="margin-top: -2px;">
                          <div class="input-group bg-danger">
                            <input type="text" name="keyword" class="form-control" id="keywordField" placeholder="Keyword here...">
                            <span class="input-group-btn">
                              <!-- <button type="submit" class="btn btn-primary" style="color: #fff;" type="button">Go!</button> -->
                            </span>
                          </div>
                        </div>
                      </form>
                    </div>
                  <div id="smsShortCodeTable"></div>
                </div>
                <div role="tabpanel" class="tab-pane fade" id="tab_ussd" aria-labelledby="ussd-tab">
                  <div id="ussdShortCodeTable"></div>
                </div>
                <div role="tabpanel" class="tab-pane fade" id="tab_voice" aria-labelledby="voice-tab">
                  <div id="voiceShortCodeTable"></div>
                </div>

            </div>
          </div>
        </div>
      </div>
    </div>
</div>

@endsection

@section('javascript')
<script src="{!! url('public/assets/vendors/tabulator/js/tabulator.js') !!}"></script>
<script type="text/javascript" src="{!! url('public/assets/vendors/tabulator/js/xlsx.full.min.js') !!}"></script>
<script type="text/javascript" src="{!! url('public/assets/vendors/tabulator/js/jspdf.min.js') !!}"></script>
<script type="text/javascript" src="{!! url('public/assets/vendors/tabulator/js/jspdf.plugin.autotable.js') !!}"></script>
<script type="text/javascript" src="{!! url('public/assets/js/shortcode.js') !!}"></script>

<script type="text/javascript">
  $(document).ready(function(){
    $('#shortCodeEditForm').submit(function(evt){
      evt.preventDefault();
      var formData = new FormData($(this)[0]);
      $.ajax({
          type: "POST",
          url: base_url + '/clients/shortcode_update',
          data : formData,
          processData: false,
          contentType: false,
          async: false,
          success: function (data){
            if (data.code === 1) {
              $.alert({
                  title: 'Alert!',
                  content: 'Short Code Details successfully updated',
              });
              setTimeout(function(){
                location.reload();
              }, 8000);
            }
            else if (data.code > 1) {
              $.alert({
                  title: 'Alert!',
                  content: data.msg,
              });
            }
            else {
              $.alert({
                  title: 'Alert!',
                  content: 'Your request could not be handled. Try again !',
              });

            }
          }
      });
    });
    
    $(document).on('click', '.linkButton', function(){
      var theVal = $(this).siblings('.hiddenInput').val();
      console.log('heere at the wall' + theVal );
      $.ajax({
        type: "GET",
        url: base_url + '/clients/get_shortcode/' + theVal,
        processData: false,
        contentType: false,
        async: false,
        success: function (data){
          if (data.code === 1) {
            console.log(data);
            $('#shortCodeIDEdit').val(theVal);
            $('#nameEdit').val(data.result.name);
            $('#shortCodeClientIdEdit').val(data.result.client_id);
            $('#shortCodeTypeEdit').val(data.result.code_type);
            $('#shortCodeEdit').val(data.result.shortcode);
            $('#tollFreeEdit').val(data.result.toll_free).change();
            $('#monthlyFeeEdit').val(data.result.monthly_fee);
            $('#launchDateEdit').val(data.result.launch_date);
            $('#expiryDateEdit').val(data.result.expiry_date);
            $('#codeStatusEdit').val(data.result.status).change();
            $('#network').val(data.result.network).change();
            $('#remarksEdit').val(data.result.remarks);
            $('#shortCodeEditModal').modal('show');
          }
          else if (data.code > 1) {
            $.alert({
                title: 'Alert!',
                content: data.msg,
            });
          }
          else {
            $.alert({
                title: 'Alert!',
                content: 'Your request could not be handled. Try again !',
            });

          }
        }
      });

    });
    function getShortCodeDetails(id){
      alert('heere at the wall from that side ' + id);
    }
    function link(cell, formatterParams){
      var url = cell.getValue();
      var rowID = cell.getData().id
      //use rowID to fetch details from DB and populate form on the modal 
      // return "<a href='"+ base_url + "/smsshortcodes/"+rowID+"/edit' class='btn btn-link' >"+url+"</a>";



      //return "<input type='button' class='btn btn-link linkButton' value='" + url + "' name='" + url + "'/>";
      return "<input type='hidden' class='hiddenInput' value='" + rowID + "' name='" + url + "'/><button type='button' class='btn btn-link linkButton' >"+url+"</button>";
      // return "<a href='' class='btn btn-link'>"+url+"</a>";
    }
    function cellDesign (cell, formatterParams){
      var value = cell.getValue();
      return "<span style='color:#54B4D3; font-weight:bold;'>" + value + "</span>";
    }
    function statusDesign (cell, formatterParams){
      var value = cell.getValue();
      if(value === 'LIVE'){
            return "<span style='color:#3FB449; font-weight:bold;'>" + value + "</span>";
      }
      else{
            return "<span style='color:#E4A11B;'>" + value + "</span>";
      }
    }
    var table = new Tabulator("#smsShortCodeTable", {
        ajaxURL: "smsshortcodes/all",
        paginationSize: 15,
        layout: "fitColumns",
        pagination: "remote",
        selectable: false,
        printAsHtml: true,
        ajaxLoaderLoading: $('#logo_spinner').html(),
        columns: [
            // {
            //     title: "Friendly Name",
            //     field: "name",
            //     sorter: "string",
            //     formatter:link,
            // },
            {
                title: "Short Code",
                field: "shortcode",
                sorter: "string",
            },
            {   title:"Network",
                field:"network",
                sorter:"string",
            },
            {   title:"Client",
                field:"clientName",
                sorter:"string",
            },
            {   title:"Monthly Fee",
                field:"monthly_fee",
                sorter:"string",
            },
            {
                title: "Status",
                field: "status",
                sorter: "string",
                formatter: statusDesign,
            },
            {
                title: "Launch Date",
                field: "launch_date",
                sorter: "string",
            },
            {
                title: "Expirty Date",
                field: "expiry_date",
                sorter: "string",
            },
            {
                title: "Last Modified By",
                field: "modifiedBy",
                sorter: "string",
            }
        ],
        // $('#clients-download-xlsx').click(function(){
        //   table.download("xlsx", "client-list.xlsx", {sheetName:"Sheet 1"});
        // });

        //rowClick:function(e, row){
          //var shortCodeID = row.getData().id;
          //alert("heere at the wall, ID is " + shortCodeID );
          //getShortCodeDetails(shortCodeID);
          // $('#userEditModal').modal('show');
        //},
        // cellClick:function(e, cell){
          //e - the click event object
          //cell - cell component
          // var theHead = cell.getCell();
          // var shortCodeId = cell.getData();
          // console.log(cell);
          // console.log(shortCodeId.id);
          // console.log(theHead);
        // },
    });
    document.getElementById("smscodes-download-xlsx").addEventListener("click", function(){
      table.download("xlsx", "sms-shortcode-list.xlsx", {sheetName:"Sheet 1"});
    });
        //trigger download of data.pdf file
    document.getElementById("smscodes-download-pdf").addEventListener("click", function(){
      table.download("pdf", "sms-shortcode-list.pdf", {
          orientation:"portrait", //set page orientation to portrait
          title:"Click Mobile - SMS Short Codes", //add title to report
      });
    });

    $('#keywordField').on('keyup', function(){
      var keyword = $(this).val();
      table.setData("smsshortcodes/all?keyword=" + keyword + "&code_type=sms");

    });

});

</script>
@endsection
