<script type="text/javascript" >
  $(document).ready(function() {
    counter();
    $('#allchk').click(function(event) {
      if (this.checked) {
        $('.chk').each(function() {
            this.checked=true;
        });
      } else {
        $('.chk').each(function(){
            this.checked=false;
        });
      }
      counter();
    });
  });

  function counter() {
    var uid = new Array();
    $('input[name="chk[]"]').each(function(){
      if($(this).is(':checked')) {
        uid.push($(this).val());
      }
    });
    $("#counter").html(uid.length);
  }

  //send result
  $('body').on('submit','#send_mail',function(e){
    e.preventDefault();
    loading('start');
    for (instance in CKEDITOR.instances) {
      CKEDITOR.instances[instance].updateElement();
    }
    var uid = new Array();
    $('input[name="chk[]"]').each(function(){
      if($(this).is(':checked')) {
        uid.push($(this).val());
      }
    });
    var form_data = new FormData(this);
    form_data.append('uid',uid);
    $.ajax({
      url:'{{url("/send_email/send")}}',
      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");
      });
    }
  });

  /*function update_temp(sno){
    var temp_name = "price list";
    var msg_temp = $("#msg").val();
    $.ajax({
      url:'{{url("/send_email/update")}}',
      data:{_token: '{!! csrf_token() !!}',temp_name:temp_name,msg_temp:msg_temp,sno:sno},
      type:'POST',
      success: function(res){
        $("#status").html(res);
      },
      error: function(err){
        $("#status").html(err.statusText);
      }
    });
  }*/

  function is_default(id) {
    loading();
      //var cid = $(this).val();
      $.ajax({
          url: '{{url("/email_hosts/is_default")}}',
          type: 'post',
          dataType:'json',
          data: {_token: '{!! csrf_token() !!}', id: id}
      })
      .done(function (res) {
        swal(res.text,res.cls);
      })
      .fail(function () {
          console.log("error");

      }).always(function(){
        loading("stop");
      });
  }

  function state_switch(id) {
    loading();
      //var cid = $(this).val();
      $.ajax({
          url: '{{url("/email_hosts/status")}}',
          type: 'post',
          dataType:'json',
          data: {_token: '{!! csrf_token() !!}', id: id}
      })
      .done(function (res) {
        swal(res.text,res.cls);
      })
      .fail(function () {
          console.log("error");

      }).always(function(){
        loading("stop");
      });
  }

  function destroy(id) {
    bootbox.confirm("{{trans('admin.Are_you_sure_to_delete')}}", function(result){
        loading();
        $.ajax({
            url: '{{url("/email_hosts/destroy")}}',
            type: 'post',
            dataType:'json',
            data: {_token: '{!! csrf_token() !!}', id: id},
        })
        .done(function(res) {
            swal(res.text,res.cls);
            $("#"+id).hide(1000);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            loading("stop");
        }); 
    })
  }
</script>
