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

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



{% block js %}
    {{ block.super }}
    $('.btn-save').click(function(){
        $.SubmitForm('#obj_form form','{{request.path}}');
    });
   
{% endblock %}

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


    


{% block form_content %}
    {{ block.super }}
    <script type="text/javascript">
      $(document).ready(function() {
        
          //初始化
          var id = $('#id_packaging').val();
          var edit_receive_num = $('#id_receive_num').val()*1;
          if(id) {
              packagingInventoreByid(id)
          }
          //库存数量校验
          $('#id_receive_num').on('input', function(){
              var inventory_num = $('#inventory_num').val()*1;
              var receive_num = $(this).val()*1;
              
              if(receive_num>inventory_num){
                toastr.error('领取数量不能大于库存数量！'); 
                var receive_num = $(this).val('');
              }
          })

          //根据包材id 查询库存、包材信息
          $('#id_packaging').on('change', function() {
              var id = $(this).val();
              packagingInventoreByid(id)
          });
          
          //查询库存、包材信息
          function packagingInventoreByid(id){
              $.ajax({
                    url: '/mes/packaging/inventory/query/', 
                    method: 'GET',
                    data: { id: id },
                    success: function(response) {
                        // 在页面上显示相关数据
                        console.log('packagingPurchasRecord',response)
                        let {
                          dt,
                          inventory_num,
                          packaging_code, packaging_name, packaging_specification,
                          vendor_name
                        }=response
                        //之前领取的数量要加回库存 才是真实的可编辑领取数量
                        if(edit_receive_num) inventory_num += edit_receive_num

                        $('#dt').val(dt);
                        $('#packaging_code').val(packaging_code);
                        $('#packaging_name').val(packaging_name);
                        $('#packaging_specification').val(packaging_specification);
                        $('#inventory_num').val(inventory_num);
                    },
                    error: function(xhr, status, error) {
                        console.error('AJAX请求失败:', error);
                    }
              });
          }
      });
    </script>
    
    <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 }}</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_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='inventory_num' id='inventory_num' />
      </div>
    </div>
    
    <div class="form-group ">
      <label class="col-sm-2 control-label ">{% trans '领用数量:' %} </label>
      <div class="col-sm-10">{{ form.receive_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="submit" style="float:right;">{% trans '保存' %}</a>
    </div>

{% endblock %}
