<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加项目</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">

                    <div class="layui-col-md12">
                        <div class="layui-col-sm6">
                            <input class="layui-input" id="repair-keyword" autocomplete="off"  placeholder="请输入合同号搜索"/>
                        </div>
                        <div class="layui-col-sm6">
                            <button type="button" class="layui-btn" data-type="onSearchPurchase">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                             <label  id="id_amount" style="margin-left: 10px;"></label>
                            <label  id="id_order_apply_amount" style="margin-left: 10px;"></label>
                        </div>
                        <div style="clear:both;height: 5px;"></div>
                    </div>

                    <div class="layui-col-md12">
                        <label class="layui-form-label" style="width: 100px;">本次申请金额：</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm8">
                                <input id="id_apply_amount" type="number" lay-verify="required" autocomplete="off" name="apply_amount"  class="layui-input"  style="width: 200px;">
                            </div>
                        </div>
                        <div style="clear:both;height: 5px;"></div>
                    </div>
                    <div class="layui-col-md12">
                        <label class="layui-form-label" style="width: 100px;">申请备注：</label>
                        <div class="layui-input-block">
                            <div class="layui-col-sm8">
                                <input id="id_notes" type="text" autocomplete="off" name="notes"  class="layui-input"  style="width: 90%;">
                            </div>
                        </div>
                        <div style="clear:both;height: 5px;"></div>
                    </div>

                    <div class="layui-col-md12">
                        <table id="dataTable" lay-filter="dataTable" class="layui-hide"></table>
                    </div>
                    <button class="layui-btn" lay-submit id="btn_search_product_ok" style="display: none;">确定</button>

                </div>
            </div>
        </div>
    </div>
</div>

  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>
  var _order_id = null;
  var _amount = 0;
  var _order_apply_amount = 0;
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
      var $ = layui.$
      ,table = layui.table
      ,admin = layui.admin;

      var id = layui.view.getParameterByName('id');

      $('#btn_search_product_ok').on('click', function () {

          var apply_amount = $('#id_apply_amount').val();
          var notes = $('#id_notes').val();

          if (!apply_amount) {
              layer.msg('请输入本次申请金额！', {icon: 5});
              return false;
          }

          if (apply_amount <= 0) {
              layer.msg('本次申请金额必须大于0！', {icon: 5});
              return false;
          }

          var amount = parseFloat(_amount);
          _order_apply_amount = parseFloat(_order_apply_amount);
          apply_amount = parseFloat(apply_amount);

         // if ((_order_apply_amount + apply_amount) - amount > 0.00001 ) {
          //    layer.msg('该合同申请金额大于合同总金额！', {icon: 5});
         //     return false;
         // }


          if (!_order_id) {
              layer.msg('请输入单号查询合同！', {icon: 5});
              return false;
          }
          var save_data = {'order_id': _order_id, 'amount': _amount, 'apply_amount': apply_amount, 'notes': notes};
          admin.req({
            url: '/purchase/purchase_payment/save/?id='+id
            ,data: JSON.stringify(save_data)
            ,type: 'post'
            , done: function (res) {
                parent.layui.onSubmitChild();
            }
            });
      });
      parent.layui.submitChild = function () {
          $("#btn_search_product_ok").click();
      };

		var layTableId = "dataTable";
		var tableIns = table.render({
			elem: '#dataTable',
			id: layTableId,
			data: [],
			page: false,
			limit: 1000,
			loading: true,
			even: true, //不开启隔行背景
			cols: [[
				{title: '序号', type: 'numbers'},
                {field: 'name', title: '名称', width: 150},
				{field: 'model', title: '代码', width: 150},
                {field: 'order_no', title: '合同号', minWidth: 150},
                {field: 'supplier_name', title: '供应商', minWidth: 150},
                {field: 'amount', title: '金额', minWidth: 150}
			]]
            ,height:'full-104'
            ,done:function () {
                    layui.index.removeNoPermButtons();
                }
		});

		//定义事件集合
		var active = {
            onSearchPurchase: function(){
              var $ = layui.$;
              var value = $('#repair-keyword').val();
              if (!value) {
                  layer.msg('请输入采购合同单号');
                  return false;
              }
              var admin = layui.admin;

              admin.req({
                  url: '/purchase/select_purchase_order/?no=' + value,
                  done: function (res) {
                      _order_id = res.data.order_id;
                      _amount = res.data.amount;
                      _order_apply_amount = res.data.apply_amount;
                      $('#id_amount').html('总金额:' + res.data.amount);
                      $('#id_order_apply_amount').html('已申请金额:' + res.data.apply_amount);
                      var data = res.data.data;
                      var oldData = [];
                      for (var i in data) {
                        var item = data[i];
                        var newRow = {
                          name: item.name,
                          model: item.model,
                          order_no: item.order_no,
                          supplier_name: item.supplier_name,
                          amount: item.amount,
                          id: item.id
                        };
				        oldData.push(newRow);
                      }
                      tableIns.reload({
                          data : oldData
                      });
                  }
              });
            },
		}

		//激活事件
		var activeByType = function (type, arg) {
			if(arguments.length === 2){
				active[type] ? active[type].call(this, arg) : '';
			}else{
				active[type] ? active[type].call(this) : '';
			}
		}

		//注册按钮事件
		$('.layui-btn[data-type]').on('click', function () {
			var type = $(this).data('type');
			activeByType(type);
		});

      if (id) {
        admin.req({
          url: '/purchase/purchase_payment/detail/?id='+id,
          done: function(res){
            var data = res.data.data;
            _order_id = res.data.order_id;
            var oldData = [];
            for (var i in data) {
                var item = data[i];
                var newRow = {
                    name: item.name,
                    model: item.model,
                    order_no: item.order_no,
                    supplier_name: item.supplier_name,
                    amount: item.amount,
                    id: item.purchase_detail_id
                }
                oldData.push(newRow);
            }
            tableIns.reload({
                data: oldData
            });
          }
        });
    }
 });

  </script>
</body>
</html>