$(function() {
  $('#printBtn').click(function() {
    $('#main').jqprint();
  });

  $(".deleteable").click(function(event) {
    event.preventDefault();
    var url = $(this).attr("href");
    $("#dialog-modal").html("These items will be permanently deleted and cannot be recovered. Are you sure?").dialog({
      height: 140,
      modal: true,
      buttons: {
        "Delete": function() {
          $(this).dialog("close");
          document.location.href = url;
        },
        Cancel: function() {
          $(this).dialog("close");
        }
      }
    });
  });

  $("input").keydown(function(event) {
    if(event.keyCode == '13')
      event.preventDefault();
  });

  $(".modify-form [type=submit]").click(function(event) {
    event.preventDefault();
    return $("#dialog-modal").html("Are you sure to modify with new data?").dialog({
      height: 140,
      modal: true,
      buttons: {
        "Modify": function() {
          $(this).dialog("close");
          $(".modify-form").submit();
        },
        Cancel: function() {
          $(this).dialog("close");
        }
      }
    });
  });

  $( ".date-field" ).datepicker({ dateFormat: 'yy-mm-dd' });

  var dates = $( "#start_date, #end_date" ).datepicker({
    defaultDate: "-2m",
    changeMonth: true,
    numberOfMonths: 3,
    dateFormat: 'yy-mm-dd',
    onSelect: function( selectedDate ) {
      var option = this.id == "start_date" ? "minDate" : "maxDate",
        instance = $( this ).data( "datepicker" ),
        date = $.datepicker.parseDate(
          instance.settings.dateFormat ||
          $.datepicker._defaults.dateFormat,
          selectedDate, instance.settings );
      dates.not( this ).datepicker( "option", option, date );
    }
  });

  if($("#dialog-modal").html() != "")
  {
    $( "#dialog-modal" ).dialog({
      height: 140,
      modal: true
    });
  }

  $("input#Product_information").click(function() {
    $("input.Product_information_items").each(function() {
      this.checked = $("input#Product_information").get(0).checked;
    });
  });

  $("input#Supplier_information").click(function() {
    $("input.Supplier_information_items").each(function() {
      this.checked = $("input#Supplier_information").get(0).checked;
    });
  });

  $("input#Fair_data").click(function() {
    $("input.Fair_data_items").each(function() {
      this.checked = $("input#Fair_data").get(0).checked;
    });
  });

  $("input#Price_information").click(function() {
    $("input#Latest_price").get(0).checked = this.checked;
    $("input.Latest_price_items").each(function() {
      this.checked = $("input#Latest_price").get(0).checked;
    });

    $("input#Last_order_price").get(0).checked = this.checked;
    $("input.Last_order_price_items").each(function() {
      this.checked = $("input#Last_order_price").get(0).checked;
    });
  });

  $("input#Latest_price").click(function() {
    $("input.Latest_price_items").each(function() {
      this.checked = $("input#Latest_price").get(0).checked;
    });
  });

  $("input#Last_order_price").click(function() {
    $("input.Last_order_price_items").each(function() {
      this.checked = $("input#Last_order_price").get(0).checked;
    });
  });

  $("input#Email_inquery").click(function() {
    $("input.Email_inquery_items").each(function() {
      this.checked = $("input#Email_inquery").get(0).checked;
    });
  });

  $("input#RMA").click(function() {
    $("input.RMA_items").each(function() {
      this.checked = $("input#RMA").get(0).checked;
    });
  });

  $("input#Mercadoliver_venda").click(function() {
    $("input.Mercadoliver_venda_items").each(function() {
      this.checked = $("input#Mercadoliver_venda").get(0).checked;
    });
  });

  $("input#Batch").click(function() {
    $("input.Batch_items").each(function() {
      this.checked = $("input#Batch").get(0).checked;
    });
  });

  $("input#Data_Export").click(function() {
    $("input.Data_Export_items").each(function() {
      this.checked = $("input#Data_Export").get(0).checked;
    });
  });

  $("input#Search").click(function() {
    $("input.Search_items").each(function() {
      this.checked = $("input#Search").get(0).checked;
    });
  });

  $("input#Data_confirm").click(function() {
    $("input.Data_confirm_items").each(function() {
      this.checked = $("input#Data_confirm").get(0).checked;
    });
  });

  $("input#Category_management").click(function() {
    $("input.Category_management_items").each(function() {
      this.checked = $("input#Category_management").get(0).checked;
    });
  });

  $("input#User_management").click(function() {
    $("input.User_management_items").each(function() {
      this.checked = $("input#User_management").get(0).checked;
    });
  });

  $("#checkall").click(function() {
    $("input:checkbox").each(function() {
      if(this.id != "checkall")
        this.checked = $("#checkall").get(0).checked;
    });
  });

  $(document).ready(function() {
    $(".validate-form").validate({
      errorClass: "validate-error",
      rules: {
        order_qty: {
          min: 0.00001
        },
        new_price: {
          min: 0.00001
        },
        order_price: {
          min: 0.00001
        }
      }
    });
  });

  $("#fetch").click(function() {
    var powerpackModelNo = $("select[name='powerpack_model_no'] option:selected").val();

    $.ajax({
      url: 'ajax/get_product_information_by_powerpack_model_no.php?powerpack_model_no=' + powerpackModelNo,
      success: function(data) {
        if(data != "no data") {
          var obj = jQuery.parseJSON(data);

          $(".fetch_delivery_term").val(obj.delivery_term);
          $(".fetch_supplier_id > option[value='" + obj.supplier_id + "']").attr("selected", true);
          $(".fetch_main_feature").val(obj.main_feature);
          $(".fetch_cbm").val(obj.cbm);
          $(".fetch_gw").val(obj.gw);
          $(".fetch_pcs").val(obj.pcs);
          $(".fetch_category_id").val(obj.category_id);
          $(".fetch_category_path").val(obj.category_path);

          $("#upload_picture").hide();
          $("#fetch_picture").show();
          $("#fetch_picture input[name='fetch_picture']").val("yes");
          $("#fetch_picture img").attr("src", obj.picture);

        }
      }
    });
  });

  $("#cancel_fetch_picture").click(function(event) {
    event.preventDefault();

    $("#upload_picture").show();
    $("#fetch_picture").hide();
    $("#fetch_picture input[name='fetch_picture']").val("no");
    $("#fetch_picture img").attr("src", "");

  });

  $("#fetch_auth").click(function() {
    var userId = $("#copy_user option:selected").val();

    $.ajax({
      url: 'ajax/get_authorities_by_user.php?uid=' + userId,
      success: function(data) {
        if(data != "no data") {
          var obj = jQuery.parseJSON(data);

          $("input[name='product_information_list']").get(0).checked = obj.product_information_list;
          $("input[name='product_information_add']").get(0).checked = obj.product_information_add;
          $("input[name='product_information_delete']").get(0).checked = obj.product_information_delete;
          $("input[name='product_information_modify']").get(0).checked = obj.product_information_modify;
          $("input[name='product_information_import']").get(0).checked = obj.product_information_import;
          $("input[name='supplier_list']").get(0).checked = obj.supplier_list;
          $("input[name='supplier_add']").get(0).checked = obj.supplier_add;
          $("input[name='supplier_delete']").get(0).checked = obj.supplier_delete;
          $("input[name='supplier_modify']").get(0).checked = obj.supplier_modify;
          $("input[name='supplier_import']").get(0).checked = obj.supplier_import;
          $("input[name='fair_data_list']").get(0).checked = obj.fair_data_list;
          $("input[name='fair_data_add']").get(0).checked = obj.fair_data_add;
          $("input[name='fair_data_delete']").get(0).checked = obj.fair_data_delete;
          $("input[name='fair_data_modify']").get(0).checked = obj.fair_data_modify;
          $("input[name='fair_data_import']").get(0).checked = obj.fair_data_import;
          $("input[name='latest_price_list']").get(0).checked = obj.latest_price_list;
          $("input[name='latest_price_add']").get(0).checked = obj.latest_price_add;
          $("input[name='latest_price_delete']").get(0).checked = obj.latest_price_delete;
          $("input[name='latest_price_modify']").get(0).checked = obj.latest_price_modify;
          $("input[name='latest_price_import']").get(0).checked = obj.latest_price_import;
          $("input[name='latest_price_export']").get(0).checked = obj.latest_price_export;
          $("input[name='last_order_price_list']").get(0).checked = obj.last_order_price_list;
          $("input[name='last_order_price_add']").get(0).checked = obj.last_order_price_add;
          $("input[name='last_order_price_delete']").get(0).checked = obj.last_order_price_delete;
          $("input[name='last_order_price_modify']").get(0).checked = obj.last_order_price_modify;
          $("input[name='last_order_price_import']").get(0).checked = obj.last_order_price_import;
          $("input[name='last_order_price_export']").get(0).checked = obj.last_order_price_export;
          $("input[name='email_inquery_list']").get(0).checked = obj.email_inquery_list;
          $("input[name='email_inquery_add']").get(0).checked = obj.email_inquery_add;
          $("input[name='email_inquery_delete']").get(0).checked = obj.email_inquery_delete;
          $("input[name='email_inquery_modify']").get(0).checked = obj.email_inquery_modify;
          $("input[name='email_inquery_import']").get(0).checked = obj.email_inquery_import;
          $("input[name='email_inquery_export']").get(0).checked = obj.email_inquery_export;
          $("input[name='rma_list']").get(0).checked = obj.rma_list;
          $("input[name='rma_add']").get(0).checked = obj.rma_add;
          $("input[name='rma_delete']").get(0).checked = obj.rma_delete;
          $("input[name='rma_modify']").get(0).checked = obj.rma_modify;
          $("input[name='rma_import']").get(0).checked = obj.rma_import;
          $("input[name='rma_export']").get(0).checked = obj.rma_export;
          $("input[name='mercadoliver_venda_list']").get(0).checked = obj.mercadoliver_venda_list;
          $("input[name='mercadoliver_venda_add']").get(0).checked = obj.mercadoliver_venda_add;
          $("input[name='mercadoliver_venda_delete']").get(0).checked = obj.mercadoliver_venda_delete;
          $("input[name='mercadoliver_venda_modify']").get(0).checked = obj.mercadoliver_venda_modify;
          $("input[name='mercadoliver_venda_import']").get(0).checked = obj.mercadoliver_venda_import;
          $("input[name='mercadoliver_venda_export']").get(0).checked = obj.mercadoliver_venda_export;
          $("input[name='batch_export']").get(0).checked = obj.batch_export;
          $("input[name='batch_import']").get(0).checked = obj.batch_import;
          $("input[name='export_repeat_order_price']").get(0).checked = obj.export_repeat_order_price;
          $("input[name='export_new_product_price']").get(0).checked = obj.export_new_product_price;
          $("input[name='export_search_general']").get(0).checked = obj.export_search_general;
          $("input[name='export_search_advanced']").get(0).checked = obj.export_search_advanced;
          $("input[name='search_repeat_order_price']").get(0).checked = obj.search_repeat_order_price;
          $("input[name='search_new_product_price']").get(0).checked = obj.search_new_product_price;
          $("input[name='search_general']").get(0).checked = obj.search_general;
          $("input[name='search_advanced']").get(0).checked = obj.search_advanced;
          $("input[name='confirm_fair_data']").get(0).checked = obj.confirm_fair_data;
          $("input[name='confirm_latest_price']").get(0).checked = obj.confirm_latest_price;
          $("input[name='confirm_last_order_price']").get(0).checked = obj.confirm_last_order_price;
          $("input[name='user_list']").get(0).checked = obj.user_list;
          $("input[name='user_add']").get(0).checked = obj.user_add;
          $("input[name='user_delete']").get(0).checked = obj.user_delete;
          $("input[name='user_modify']").get(0).checked = obj.user_modify;
          $("input[name='permission_control']").get(0).checked = obj.permission_control;
          $("input[name='category_list']").get(0).checked = obj.category_list;
          $("input[name='category_add']").get(0).checked = obj.category_add;
          $("input[name='category_delete']").get(0).checked = obj.category_delete;
          $("input[name='category_modify']").get(0).checked = obj.category_modify;
        }
      }
    });
  });

  $(".uncheck_confirm").click(function() {
    if(!$(this).get(0).checked) {
      var checkbox = $(this).get(0);
      return $("#dialog-modal").html("Are you sure to UN-check this record?").dialog({
        height: 140,
        modal: true,
        buttons: {
          "Sure": function() {
            checkbox.checked = false;
            $(this).dialog("close");
          },
          Cancel: function() {
            checkbox.checked = true;
            $(this).dialog("close");
          }
        }
      });
    }
  });

  $("a.show_in_dialog").click(function(event) {
    event.preventDefault();

    var target = $(this).attr("href");
    var dialog_title = $(this).attr("dialog_title");

    $("#data-dialog").load(target).dialog({
      title: dialog_title,
      position: 'center',
      width: '800px',
      height: '800px'
    });
  });

  $("img.list_thumb").click(function(event) {
    var pic = '<center><img src="' + $(this).attr("ori") + '" /></center>';
    $("#data-dialog").html(pic).dialog({
      position: 'top',
      width: '800px',
      height: '800px'
    });
  });

  var allow_submit = false;
  $("#add_product_information_form").submit(function(event) {

    if(allow_submit) {
      allow_submit = false;
      return;
    }

    if($("#powerpack_model_no").val() == '')
      return;

    event.preventDefault();

    var form = $(this);
    var powerpackModelNo = $("#powerpack_model_no").val();

    $.ajax({
      url: 'ajax/get_product_information_by_powerpack_model_no.php?powerpack_model_no=' + powerpackModelNo,
      success: function(data) {
        if(data != "no data") {
          var obj = jQuery.parseJSON(data);

          $("#data-dialog").html('powerpack power no: ' + powerpackModelNo + ' is already exist! save this record anyway? ').dialog({
            buttons: {
              'Save it!': function() {
                allow_submit = true;
                form.submit();
                $("#data-dialog").html('').dialog('close');
              },
              'Cancel': function() {
                $("#powerpack_model_no").val('');
                $("#data-dialog").html('').dialog('close');
              }
            }
          });
        } else {
          allow_submit = true;
          form.submit();
        }
      }
    });
  });

  $("#add_supplier_form").submit(function(event) {

    if(allow_submit) {
      allow_submit = false;
      return;
    }

    if($("#sup_name").val() == '')
      return;

    event.preventDefault();

    var form = $(this);
    var supplier = $("#sup_name").val();

    $.ajax({
      url: 'ajax/get_supplier_name.php?term=' + supplier,
      success: function(data) {
        if(data != "no data") {
          var obj = jQuery.parseJSON(data);

          $("#data-dialog").html('supplier: ' + supplier + ' is already exist! save this record anyway? ').dialog({
            buttons: {
              'Save it!': function() {
                allow_submit = true;
                form.submit();
                $("#data-dialog").html('');
                $("#data-dialog").dialog('close');
              },
              'Cancel': function() {
                $("#sup_name").val('');
                $("#data-dialog").html('');
                $("#data-dialog").dialog('close');
              }
            }
          });
        } else {
          allow_submit = true;
          form.submit();
        }
      }
    });
  });
});

