{% extends 'obj_form.html' %}
{% load i18n %}
{% load tj_filters %}

{% block css %}
    {{ block.super }}    
   
{% endblock %}



{% block js %}
    {{ block.super }}
   
{% endblock %}

 
{% block subtitle-1 %}
    {% trans '库存管理' %}
{% endblock %}
{% block subtitle-2 %}
    {% trans '包装材料入库' %}
{% endblock %}   


    


{% block form_content %}
    {{ block.super }}
    <script type="text/javascript">  
      $(document).ready(function() {
          //编辑 回显查询采购记录
          var purchase_id = $('#id_packaging_purchas').val()*1
          var instore_id = $('#id_instore').val()
          var before_instore_num = $('#id_instore_num').val()?$('#id_instore_num').val()*1:0 //编辑前入库数量
          console.log('instore_id',instore_id)
          if(purchase_id){
            queryPackagingPurchaseById({purchase_id,instore_id})
          }

          function validateForm() {
            var purchase_num = $('#qty').val()*1;
            var instore_num = $('#id_instore_num').val()*1;
            var inventory_num = $('#packaging_inventory').val()*1;
            var yet_instore_num = $('#yet_instore_num').val()*1;
            var max_num = purchase_num - yet_instore_num; //可入库数量 最大
            var max_min = (before_instore_num - inventory_num)>0 ? (before_instore_num - inventory_num) : 0 ; //可入库数量 最小
            if(purchase_num && instore_num){
                if(instore_num > max_num){
                  toastr.error('入库数量不能大于' + max_num); 
                  $('#id_instore_num').val("")
                  return false;
                }
            }
            if(purchase_id && instore_num < max_min){//编辑修改
              toastr.error('入库数量不能小于'+ max_min);
              $('#id_instore_num').val("")
              return false;
            }
            return true;
          }

          // 监听code字段的变化
          $('#id_packaging_purchas').on('change', function() {
              var purchase_id = $(this).val()*1;
              queryPackagingPurchaseById({purchase_id,instore_id})
          });
          
          function queryPackagingPurchaseById(data={}){
            $.ajax({
                  url: '/mes/query/packaging/purchase/',
                  method: 'GET',
                  data: data,
                  success: function(response) {
                      // 在页面上显示相关数据
                      const {
                        dt,
                        qty, yet_instore_num, packaging_inventory,
                        packaging_id,packaging_code, packaging_name, packaging_specification,
                        vendor_name
                      }=response
                      $('#dt').val(dt);
                      $('#id_packaging').val(packaging_id);
                      $('#packaging_code').val(packaging_code);
                      $('#packaging_name').val(packaging_name);
                      $('#packaging_specification').val(packaging_specification);
                      $('#vendor_name').val(vendor_name);
                      $('#qty').val(qty);
                      $('#yet_instore_num').val(yet_instore_num);
                      $('#packaging_inventory').val(packaging_inventory);
                  },
                  error: function(xhr, status, error) {
                      console.error('AJAX请求失败:', error);
                  }
            });
          }
          
          // 表单提交时的数据校验
          $('.btn-save').on('click', function(event) {
              if (!validateForm()) {
                  event.preventDefault(); // 阻止表单提交
              }else{
                $.SubmitForm('#obj_form form','{{request.path}}');
              }
          });
      });
    </script>
    <input type='hidden' type="number" value="{{instore_id}}" name="id_instore" id="id_instore" />
    <input type='hidden' type="number" value="" name="packaging" id="id_packaging" />
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '入库编码:' %} </label>
        <div class="col-sm-10">{{ form.code }}</div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '入库时间:' %} </label>
      <div class="col-sm-10">{{ form.dt }}</div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '采购编号:' %} </label>
      <div class="col-sm-10">{{ form.packaging_purchas }}</div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '采购时间:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='dt' id='dt' />
      </div>
    </div> 
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '包材编码:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='packaging_code' id='packaging_code' />
      </div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '包材名称:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='packaging_name' id='packaging_name' />
      </div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '规格:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='packaging_specification' id='packaging_specification' />
      </div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '供应商:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='vendor_name' id='vendor_name' />
      </div>
    </div>
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '采购数量:' %} </label>
      <div class="col-sm-10">
        <input type="text" disabled name='qty' id='qty' />
      </div>
    </div>
    <div class="form-group ">
      <label class="col-sm-1 control-label ">{% trans '采购单已入数量:' %} </label>
      <div class="col-sm-5">
        <input type="text" disabled name='yet_instore_num' id='yet_instore_num' />
      </div>
      <label class="col-sm-1 control-label ">{% trans '库数量:' %} </label>
      <div class="col-sm-5">
        <input type="text" disabled name='packaging_inventory' id='packaging_inventory' />
      </div>
    </div>

    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '入库数量:' %} </label>
      <div class="col-sm-10">{{ form.instore_num }}</div>
    </div>  
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '备注说明:' %} </label>
        <div class="col-sm-10">{{ form.remark }}</div>
    </div>   
    <div>
        <a class="btn btn-primary btn-save" type="button" style="float:right;">{% trans '保存' %}</a>
    </div>

{% endblock %}
