
<script>
    $(document).ready(function() {

        $("#myTab a").click(function(e) {
            e.preventDefault();
            $(this).tab('show');
        });
        $("#myTab2 a").click(function(e) {
            e.preventDefault();
            $(this).tab('show');
        });
        $("#myTab3 a").click(function(e) {
            e.preventDefault();
            $(this).tab('show');
        });
    });


    //send result
  $('body').on('submit','#send_mail',function(e){
    e.preventDefault();
    loading('start');
    for (instance in CKEDITOR.instances) {
      CKEDITOR.instances[instance].updateElement();
    }
    var form_data = new FormData(this);
    $.ajax({
      url:'{{url("/customer/sendMail")}}',
      data: form_data,
      contentType: false,
      processData: false,
      cache: false,
      type:'POST',
      dataType: 'json',
      success: function(res){
        swal(res.text, res.cls);
        loading('stop');
      },
      error: function(err){
        $(".msg").html(err.statusText);
        loading('stop');
      }
    }); 
  });

  $('body').on('change','#template',function(e){
    e.preventDefault();
    loading('start');
    var id = $("#template").val();
    if (id != "") {
      $.ajax({
          url: '{{url("/send_email/get_template")}}/'+id,
          type: 'get',
          dataType:'json',
      })
      .done(function (res) {
        $("#subject").val(res.subject);
        $("#params").html(res.params);
        CKEDITOR.instances['message'].setData(res.content);
      })
      .fail(function () {
        console.log("error");
      }).always(function(){
        loading("stop");
      });
    }
  });

  /*send sms*/
  //send result
  $('body').on('submit','#send',function(e){
    e.preventDefault();
    loading('start');
    var form_data = new FormData(this);
    $.ajax({
      url:'{{url("/providers/sendSms")}}',
      data: form_data,
      contentType: false,
      processData: false,
      cache: false,
      type:'POST',
      dataType: 'json',
      success: function(res){
        swal(res.text, res.cls);
        loading('stop');
      },
      error: function(err){
        $(".msg").html(err.statusText);
        loading('stop');
      }
    }); 
  });

  /*send notification*/
  //send notification
  $('body').on('submit','#sendNotify',function(e){
    e.preventDefault();
    loading('start');
    var form_data = new FormData(this);
    $.ajax({
      url:'{{url("/providers/sendPushNotify")}}',
      data: form_data,
      contentType: false,
      processData: false,
      cache: false,
      type:'POST',
      dataType: 'json',
      success: function(res){
        swal(res.text, res.cls);
        loading('stop');
      },
      error: function(err){
        $(".msg").html(err.statusText);
        loading('stop');
      }
    }); 
  });

  ///////////////// Edit time script //////////////////////////

    $(document).ready(function (e) {
        $('.edit-vendor-time').click(function () {
            timing_id = $(this).data('id');
            try {

                    loading();
                    $.ajax({
                        url: '{{url("/vendor-timing/edit")}}/'+timing_id,
                        type: 'get',
                    })
                    .done(function(response) {
                        // console.log(response);
                        $('#from_time').val(response.from_time);
                        $('#to_time').val(response.to_time);
                        var checked = (response.status == 1) ? true : false ;
                        console.log(checked);
                        $('#timing_status').prop('checked',checked);
                        $('#timingModal').modal('show');
                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        loading("stop");
                    });

            } catch (error) {
                console.log(error);
            }
        })
    });
    //////////////////////////// Update timing ////////////////////////
    $(document).ready(function () {
        $('#edit-timing-form').submit(function(e){
            e.preventDefault();
            try {
                    var fdata = new FormData(this);
                    fdata.append('id',timing_id);
                    loading();
                    $.ajax({
                        url: '{{url("/vendor-timing/update")}}/',
                        type: 'post',
                        data : fdata,
                        contentType: false,
                        processData: false,
                        cache: false,
                    })
                    .done(function(response) {
                        // console.log(response);
                        $('#from_time').val(response.from_time);
                        $('#to_time').val(response.to_time);

                        $('#timing_status').val(response.status);
                        swal(response.text,response.cls);
                        $('#timingModal').modal('hide');
                        // $('.from_time_'+timing_id).val(fdata.get('from_time'));
                        // $('.to_time_'+timing_id).val(fdata.get('to_time'));
                        // console.log(fdata.get('from_time'));
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);

                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        loading("stop");
                    });

            } catch (error) {
                console.log(error);
            }
        })
    });
    ///////////////////////////// Delete timing //////////////////////////////////
    $(document).ready(function () {
        $('.delete-vendor-time').click(function(){
            var id = $(this).data('id');
            if(!confirm('Are you sure')){
                return false;
            }

            try {

                loading();
                    $.ajax({
                        url: '{{url("/vendor-timing/delete")}}/'+id,
                        type: 'get',
                    })
                    .done(function(response) {
                        // console.log(response);
                        loading("stop");
                        swal(response.text.response.cls);

                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        loading("stop");
                    });

            } catch (error) {
                console.log(error);
            }
        });
    });
    /////////////////////// View Rating //////////////////////////////////
    $(document).ready(function () {
        $('.view-ratings').click(function(e){
            e.preventDefault();
            var id = $(this).data('id');
            try{
                    loading();
                    $.ajax({
                        url: '{{url("/review/get")}}/'+id,
                        type: 'get',
                    })
                    .done(function(response) {
                        // console.log(response.rating);
                        loading("stop");

                        var stars = "";
                        for(var i = 1; i <= 5; i++){
                            var rating = (i <= response.rating) ? 'checked1' : "";
                                stars += '<span class="fa fa-star '+rating+'"></span>';
                        }
                        console.log(stars);
                        $('.user-stars').html(stars);
                        $('.user-rating-number').html(response.rating);
                        $('.user-rating-status').text(response.status);
                        $('.user-rating-comment').text(response.comment);
                            $('#ratingModal').modal('show');

                    //    swal(response.text.response.cls);

                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        loading("stop");
                    });
            } catch (error) {
                console.log(error);
            }
        });
    });
    //////////////////////// Change rating status /////////////////////////////////////
    $(document).ready(function () {
        $('.change-rating-status').change(function(){
            var id = $(this).data('id');
            try{
                loading();
                $.ajax({
                    url: '{{url("/review/change-status")}}/'+id,
                    type: 'get',
                })
                .done(function(response) {
                    // console.log(response.rating);
                    loading("stop");

                    swal(response.text,response.cls);

                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    loading("stop");
                });
            } catch (error) {
                console.log(error);
            }
        })
    });
    ///////////////// Provider item delete //////////////////////////////////

    $('body').on('click','.item-delete',function(e){
        if(!confirm('Are you sure')){
            return;
        }
        e.preventDefault();
                let  item_id = $(this).data('id');
                var self = $(this);
        if(!item_id == 0){
            try {

                 loading();
                    $.ajax({
                        url: '{{url("/items/delete")}}/'+item_id,
                        type: 'get',
                    })
                    .done(function(response) {
                        // console.log(response);
                        loading("stop");
                        self.parent().parent().remove();
                        swal(response.text,'warning');

                    })
                    .fail(function() {
                        console.log("error");
                    })
                    .always(function() {
                        loading("stop");
                    });

            } catch (error) {
                loading("stop");
                console.log(error);
            }
        }
    });
    ////////////////////// Delete Item Category ////////////////////

    $('body').on('click','.delete-item-category',function(e){
        e.preventDefault();
        if(!confirm('Are you sure?')){
            return
        }
        var id = $(this).data('value');
        var self = $(this);
        try {

            loading();
            $.ajax({
                url: '{{url("/item-category/delete")}}/'+id,
                type: 'get',
            })
            .done(function(response) {
                // console.log(response);
                loading("stop");
                self.parent().parent().remove();
                swal(response.text,'warning');
                $('.'+id).remove();

            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                loading("stop");
            });
        } catch (error) {
            console.log(error);
             loading("stop");
        }
    });
    ///////////////////////////// delete room //////////////////////////
    $('body').on('click','.delete-room',function(e){
        e.preventDefault();
        if(!confirm('Are you sure?')){
            return
        }
        var id = $(this).data('value');
        var self = $(this);
        try {

            loading();
            $.ajax({
                url: '{{url("/rooms/delete")}}/'+id,
                type: 'get',
            })
            .done(function(response) {
                // console.log(response);
                loading("stop");
                self.parent().parent().remove();
                swal(response.text,'warning');
                $('.room_'+id).remove();

            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                loading("stop");
            });
        } catch (error) {
            console.log(error);
             loading("stop");
        }
    })

    ///////////////////////////// delete table //////////////////////////
    $('body').on('click','.delete-table',function(e){
        e.preventDefault();
        if(!confirm('Are you sure?')){
            return
        }
        var id = $(this).data('value');
        var self = $(this);
        try {

            loading();
            $.ajax({
                url: '{{url("/tables/delete")}}/'+id,
                type: 'get',
            })
            .done(function(response) {
                // console.log(response);
                loading("stop");
                self.parent().parent().remove();
                swal(response.text,'warning');
                $('.table_'+id).remove();

            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                loading("stop");
            });
        } catch (error) {
            console.log(error);
             loading("stop");
        }
    })
    ///////////////////////////// delete slider //////////////////////////
    $('body').on('click','.delete-slider',function(e){
        e.preventDefault();
        if(!confirm('Are you sure?')){
            return
        }
        var id = $(this).data('id');
        var self = $(this);
        try {

            loading();
            $.ajax({
                url: '{{url("/vendor-slider/delete")}}/'+id,
                type: 'get',
            })
            .done(function(response) {
                // console.log(response);
                loading("stop");
                self.parent().parent().remove();
                swal(response.text,'warning');
                $('.table_'+id).remove();

            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                loading("stop");
            });
        } catch (error) {
            console.log(error);
             loading("stop");
        }
    })
   /////////////////////////////////Order list////////////////////////////////////

    var table = "";
    var filter_search = null;
    $(function () {

        table = $('.orders-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url : "{{ url('providers/get-orders-list') }}",
                type: 'GET',
                data: function (d) {
                    d.filter_search = filter_search;
                    d.vendor_id = '{{$vendor_id}}';
                    // console.log(d);
                }
            },
            columns: [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'order_id', name: 'order_id'},
                {data: 'date_time', name: 'date_time'},
                {data: 'status', name: 'status', orderable: false, searchable: false},
                {data: 'action', name: 'action', orderable: false, searchable: false},
            ],
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]]
        });

    });

    $("#search_new").on("keyup search input paste cut", function() {
        table.search(this.value).draw();
    });

    $('body').on('click','.category',function (e) {
        filter_search = $(this).data('value');
        table.draw(true);
    });


    $('body').on('click','.clear',function (e) {
       filter_search = "";
        table.draw(true);
    });
///////////////////////////////////////////////////////////////////////

    $(document).ready(function () {

        items_table = "";
        item_filter_search = null;
        $(function () {

            items_table = $('.items-table').DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    url : "{{ url('providers/get-items-list') }}",
                    type: 'GET',
                    data: function (d) {
                        d.item_filter_search = item_filter_search;
                        d.items_vendor_id = '{{$vendor_id}}';
                        // console.log(d);
                    }
                },
                columns: [
                    {data: 'id', name: 'id'},
                    {data: 'name', name: 'name'},
                    {data: 'status', name: 'status'},
                    {data: 'price', name: 'price'},

                    // {data: 'actions', name: 'actions'},

                    {data: 'actions', name: 'actions', orderable: false, searchable: false},
                ],
                "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]]
            });

        });

        $("#search_new").on("keyup search input paste cut", function() {
            items_table.search(this.value).draw();
        });

    });


    $('body').on('click','.items_category',function (e) {
        item_filter_search = $(this).data('id');
        // alert(item_filter_search);
        items_table.draw(true);
    });

    $('body').on('click','.clear_item_category',function (e) {
        item_filter_search = null;
        items_table.draw(true);
        console.log(item_filter_search);
    });

    /////////////////////////////////////////////////////////////////////////////

    google.charts.load('current', {
        'packages': ['bar']
    });
    google.charts.setOnLoadCallback(drawStuff);

    function drawStuff() {
        // console.log(@json($earning_graph));
        var record = @json($earning_graph);
        var data = new google.visualization.arrayToDataTable(record);

        var options = {
            title:  'Earning Graph',

            legend: {
                position: 'none'
            },
            chart: {
                title: ' ',
                subtitle: ' '
            },
            bars: 'vertical',
            axes: {
                x: {
                    0: {
                        side: '',
                        label: ''
                    } // Top x-axis.
                }
            },
            bar: {
                groupWidth: "90%"
            }
        };

        var chart = new google.charts.Bar(document.getElementById('top_div'));
        chart.draw(data, options);

    };

</script>
