<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
    <div class="card">
      <div class="body">
        <div class="p-l-10">
         <%= simple_form_for([:inventory, @inventory_definition]) do |f| %>
         <%= f.error_notification %>

         <div class="form-inputs">
          <div class="row">
            <% arr = MenuItem.active.order("name desc").pluck(:name, :item_code) %>
            <% Product.order("name desc").pluck(:name, :item_code).each do |p| %>
            <% arr.push(p) %>
            <% end %>
            <div class="col-md-4 ">
              <div class="form-group">
                <label class="control-label"><abbr title="required">*</abbr>Select Item</label>
                <select class="form-control item_code_place" id="item">
                  <% if !@inventory_definition.item_code.nil? %>
                  <% menuiteminstance = MenuItemInstance.find_by_item_instance_code(@inventory_definition.item_code) %>
                  <% if menuiteminstance.nil? %>
                  <% code = @inventory_definition.item_code %>
                  <% else %>
                  <% code = menuiteminstance.menu_item.item_code %>
                  <% end %>
                  <% end %>
                  <option value=""></option>
                  <% arr.each do |a| %>
                  <% if a[1] == code %>
                  <option value="<%= a[1] %>" selected><%= a[0] %></option>
                  <% else %>
                  <option value="<%= a[1] %>"><%= a[0] %></option>
                  <% end %>
                  <% end %>
                </select>
              </div>
            </div>

            <% sample = [] %>
            <% if @inventory_definition.item_code.nil? %>
            <% if !MenuItemInstance.find_by_item_instance_code(@inventory_definition.item_code).nil? %>
            <% sample = MenuItemInstance.where("item_instance_code=?", @inventory_definition.item_code).pluck(:item_instance_name, :item_instance_code) %>
            <% else %>
            <% sample = Product.where("item_code=?", @inventory_definition.item_code).pluck(:name, :item_code) %>
            <% end %>
            <% end %>
            <div class="col-md-3"><%= f.input :item_code, collection: sample %></div>
          </div>

          <%= f.input :min_order_level %>
          <%= f.input :max_stock_level %>
        </div>

        <div class="form-actions">
          <%= f.submit t("views.btn.submit"), class: 'btn bg-blue waves-effect' %>
        </div>

        <% end %>
      </div>
      </div>
    </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
  <div class="card">
    <div class="body">
     <h5><i class="material-icons md-18">view_headline <%= t("views.right_panel.header.page_detail") %></i> </h5>
     <p>
      1) <%= t("views.right_panel.detail.select_item") %> - <%= t("views.right_panel.detail.select_txt") %> <%= t("views.right_panel.detail.item_txt") %> <br>
      2) <%= t("views.right_panel.detail.item_code") %> - <%= t("views.right_panel.detail.write_txt") %> <%= t("views.right_panel.detail.item_code_txt2") %> <br>
      3) <%= t("views.right_panel.detail.min_order") %> <%= t("views.right_panel.detail.level") %> - <%= t("views.right_panel.detail.write_txt") %> <%= t("views.right_panel.detail.min_order_level_txt") %> <br>
      4) <%= t("views.right_panel.detail.max_stock") %> <%= t("views.right_panel.detail.level") %> - <%= t("views.right_panel.detail.write_txt") %><%= t("views.right_panel.detail.max_stock_level_txt") %><br>
    </p>
    <h5><i class="material-icons md-18">list <%= t("views.right_panel.header.button_lists") %></i> </h5>
    <p>
      1) <%= t("views.btn.submit") %> - <%= t("views.right_panel.detail.create_btn_txt") %> <%= t("views.right_panel.detail.inventory") %> <br>
    </p>

    <h5><i class="material-icons md-18">list <%= t("views.right_panel.header.link_lists") %></i> </h5>
    <p>
      1) <%= t("views.right_panel.button.home") %> - <%= t("views.right_panel.detail.home_txt") %> <br>
      2) <%= t("views.right_panel.button.back") %> - <%= t("views.right_panel.detail.back_txt") %> <%= t("views.right_panel.detail.home_txt") %> <br>
    </p>
  </div>
</div>  
</div>
</div>


<script>

  $(document).ready(function () {
        // $(".item_code_place").select2({
        //     placeholder: 'Select Item'
        // });
        // $("select#inventory_definition_item_code").select2();

        $(".item_code_place").on('change', function (event) {
          var ajax_url = "<%= settings_find_item_instance_path	 %>";
          var item_code = this.value;
          $.ajax({
            type: "GET",
            url: ajax_url,
            data: 'item_code=' + item_code,
            success: function (result) {
              $("#inventory_definition_item_code").empty();
              var itemlist;
              for (var i = 0; i < result.length; i++) {
                itemlist = "<option value ='" + result[i][1] + "'>" + result[i][0] + "</option>";
                $("select#inventory_definition_item_code").append(itemlist);
              }
//                    $("select#inventory_definition_item_code").append(itemlist);
                    // $("select#inventory_definition_item_code").select2();
                  }
                });
        });

      });

    </script>