<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!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">
  <meta name="decorator" content="base"/>
  <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css"/>
  <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css"/>
  <style>
    .layui-table-tool-temp {
      padding-right: 0
    }
  </style>
</head>
<body>

<div class="layui-fluid">
  <form class="layui-form layui-card" id="searchForm">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <sfTags:pagePermission authFlag="KCGL_KCGL_CK_T" html='<<a href="${ctx}/material/stockList"><li class="layui-this">库存管理</li></a>'/>
        <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset">
          <i class="layui-icon fs-12">&#xe9aa;</i>
          重置
        </button>
        <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" id="searchBtn" lay-submit type="button"
                lay-filter="LAY-user-back-search">
          <i class="layui-icon fs-12">&#xe615;</i>
          查询
        </button>
      </ul>
    </div>
    <div class="layui-card-body layuiadmin-card-header-auto">
      <input type="hidden" name="showZeroStock"/>
      <div class="layui-form-item">
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">物料代码：</label>
          <div class="layui-input-block">
            <input type="text" name="code" class="layui-input">
          </div>
        </div>
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">物料类型：</label>
          <div class="layui-input-block">
            <select name="materialTypeId" id="materialTypeId">
              <option value="">请选择</option>
              <c:forEach items="${types}" var="ty">
                <option value="${ty.columns.id}">${ty.columns.name}</option>
              </c:forEach>
            </select>
          </div>
        </div>

        <c:if test="${userType eq '1' || userType eq '4'}">
          <div class="layui-col-lg2 layui-col-sm3">
            <label class="layui-form-label">省区：</label>
            <div class="layui-input-block">
              <select name="province" lay-search lay-filter="province"  id="province">
                <option value="">请选择</option>
                <c:forEach items="${province}" var="trader">
                  <option value="${trader.columns.ProvinceName}"  title="${trader.columns.ProvinceID}">${trader.columns.ProvinceName}</option>
                </c:forEach>
              </select>
            </div>
          </div>
          <div class="layui-col-lg2 layui-col-sm3">
            <label class="layui-form-label">代理商：</label>
            <div class="layui-input-block">
              <div id="belong"></div>
            </div>
          </div>
        </c:if>
        <c:if test="${userType ne '1' && userType ne '4'}">
          <input type="hidden" name="ascriptionId" value="${ascriptionId}"/>
        </c:if>
<%--        <div class="layui-col-lg2 layui-col-sm3">--%>
<%--          <label class="layui-form-label">外部编码：</label>--%>
<%--          <div class="layui-input-block">--%>
<%--            <input type="text" name="qq_code" class="layui-input">--%>
<%--          </div>--%>
<%--        </div>--%>
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">仓库：</label>
          <div class="layui-input-block">
            <select name="warehouseIds" id="warehouseId" lay-search>
              <option value="">请选择</option>
              <c:forEach items="${wares}" var="warehouse">
                <option value="${warehouse.columns.id}">${warehouse.columns.name}</option>
              </c:forEach>
            </select>
          </div>
        </div>
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">可用库存：</label>
          <div class="layui-input-block">
            <select name="availableStock" lay-search>
              <option value="">请选择</option>
              <option value="1">有库存</option>
              <option value="2">无库存</option>
            </select>
          </div>
        </div>
      </div>
    </div>
  </form>
  <div class="layui-card">
    <div class="layui-card-body">
      <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
    </div>
  </div>
</div>
<script type="text/html" id="toolbarDemo">
  <sfTags:pagePermission authFlag='KCGL_DLSKCGL_ADD_B' html='<button class="layui-btn layui-btn-sm" lay-event="addStock"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
  <button class="layui-btn layui-btn-sm"  id="exportExcel"  onclick="exportExcel()"  ><i class="layui-icon layui-icon-export"></i>导出</button>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script>
  layui.config({
    base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'tableExt', 'jquery', 'layer', 'laypage', 'laydate', 'form'], function () {
    $ = layui.$;
    tableExt = layui.tableExt;
    layer = layui.layer;
    laypage = layui.laypage;
    laydate = layui.laydate;
    form = layui.form;
    table = tableExt.getLayTable();

    //开始日期
    var insStart = laydate.render({
      elem: '#repairTime'
      , type: 'datetime'
      , done: function (value, date) {
        //更新结束日期的最小日期
        insEnd.config.min = lay.extend({}, date, {
          month: date.month - 1
        });
        //自动弹出结束日期的选择器
        insEnd.config.elem[0].focus();
      }
    });
    //结束日期
    var insEnd = laydate.render({
      elem: '#endTime'
      , type: 'datetime'
      , done: function (value, date) {
        //更新开始日期的最大日期
        insStart.config.max = lay.extend({}, date, {
          month: date.month - 1
        });
      }
    });

    <c:if test="${userType eq '1' || userType eq '4'}">
    xmSelect.render({
      el: '#belong',
      name: 'ascriptionId',
      size: 'mini',
      filterable: true,
      model: {
        label: {
          type: 'block',
          block: {
            //最大显示数量, 0:不限制
            showCount: 1,
            //是否显示删除图标
            showIcon: true,
          }
        }
      },
      toolbar: {
        show: true
      },
      height: '300px',
      data: [
        // {name: '供应链', value: 'a'},
        <c:forEach items="${traderList}" var="trader">
        {name: '${trader.columns.name}', value: '${trader.columns.id}'},
        </c:forEach>
<%--        <c:forEach items="${supplyList}" var="supply">--%>
<%--        {name: '${supply.columns.name}', value: '${supply.columns.id}'},--%>
<%--        </c:forEach>--%>
      ]
    });
    </c:if>

    // form.on('checkbox(zero)', function (d) {
    //   var showZeroStock = d.elem.checked ? "1" : "";
    //   $('input[name=showZeroStock]').val(showZeroStock);
    //   renderTable();
    //   $('input[name="zero"]').prop('checked', d.elem.checked);
    //   form.render();
    // });

    renderTable();

    function renderTable() {
      tableExt.render({
        elem: '#test'
        , height: 'full-180'
        , toolbar: '#toolbarDemo'
        , defaultToolbar: false
        , limits: [20, 50, 100]
        , limit: 20
        , page: true //开启分页
        ,totalRow: true
        , url: '${ctx}/material/ajaxForStockList'
        , where: _serializeJson($("#searchForm"))
        , cols: [[
          {type: 'numbers', fixed: 'left', title: '序号',rowspan:2,totalRowText: '合计：'}
          <c:if test="${userType eq '1' || userType eq '4'}">
          , {field: 'ascription_name', title: '代理商', minWidth: 280, sort: false, align: 'center',rowspan:2, exportable: true}
          </c:if>
          , {field: 'code', title: '物料代码', minWidth: 150, sort: false, align: 'center',rowspan:2, exportable: true}
          , {field: 'qq_code', title: '外部编码', sort: false, align: 'center', exportable: true,rowspan:2, minWidth: 180}
          , {field: 'materialName', title: '物料名称', minWidth: 150, sort: false, align: 'center',rowspan:2, exportable: true}
          , {field: 'typeName', title: '物料类型', sort: false, align: 'center', exportable: true,rowspan:2, minWidth: 100}
          , {field: 'material_specs', title: '物料规格', sort: false, align: 'center', exportable: true,rowspan:2, minWidth: 300,
            templet: function (d) {
              if (d.typeName == '组件') {
                return d.material_specs+"Wp";
              } else {
                return d.material_specs;
              }
            },expFormat:function(d){
              if (d.typeName == '组件') {
                return d.material_specs+"Wp";
              } else {
                return d.material_specs;
              }
            }
          }
          , {field: 'unitName', title: '库存单位', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true}
          , {field: 'material_brand', title: '供应商名称', sort: false, align: 'center',rowspan:2, exportable: true, minWidth: 100}
          , {field: 'warehouseName', title: '仓库名称', minWidth: 280, sort: false, align: 'center',rowspan:2, exportable: true}
          , {field: 'yfhTotalCapacity', title: '累计已发货', sort: false, align: 'center',colspan:3, minWidth: 100, exportable: true}
          , {field: 'yazTotalCapacity', title: '累计已安装', sort: false, align: 'center',colspan:3, minWidth: 100, exportable: true}
          , {field: 'yfdTotalCapacity', title: '累计已发电', sort: false, align: 'center',colspan:3, minWidth: 100, exportable: true}
          , {field: 'ytfTotalCapacity', title: '累计已投放', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true,totalRow:true}
          , {field: 'azwfdTotalCapacity', title: '安装未发电', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true,totalRow:true}
          , {field: 'azwtfTotalCapacity', title: '安装未投放', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true,totalRow:true}
          , {field: 'yk_th_amount', title: '累计退货', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true,totalRow:true
            ,templet:function(d){
              var htm = "";
              if(d.yk_th_amount != null) {
                htm += d.yk_th_amount;
              } else {
                htm += '0';
              }
              htm += '<a onclick="ykBzjClicked(\'' + d.id + '\', \'' + (d.yk_th_amount != null ? d.yk_th_amount : '0') + '\', \'4\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
              return htm;
            }
          }
          , {field: 'yk_md_amount', title: '累计买断', sort: false, align: 'center',rowspan:2, minWidth: 100, exportable: true,totalRow:true
            ,templet:function(d){
              var htm = "";
              if(d.yk_md_amount != null) {
                htm += d.yk_md_amount;
              } else {
                htm += '0';
              }
              htm += '<a onclick="ykBzjClicked(\'' + d.id + '\', \'' + (d.yk_md_amount != null ? d.yk_md_amount : '0') + '\', \'5\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
              return htm;
            }
          }
          , {title: '可用总库存',minWidth:'130', sort: false, align: 'center',exportable:true,colspan:2,totalRow:true}
          , {field: 'waitDeliverNum',title: '待发货',minWidth:'130', sort: false, align: 'center',exportable:true,rowspan:2,totalRow:true}
          , {field: 'waitSignCount',title: '待签收',minWidth:'130', sort: false, align: 'center',exportable:true,rowspan:2,totalRow:true}
          , {title: '在库30天以内',minWidth:'130', sort: false, align: 'center',exportable:true,colspan:2,totalRow:true}
          , {title: '在库30(天)-60天',minWidth:'130', sort: false, align: 'center',exportable:true,colspan:2,totalRow:true}
          , {title: '在库60(天)-90天',minWidth:'130', sort: false, align: 'center',exportable:true,colspan:2,totalRow:true}
          , {title: '在库90天及以上',minWidth:'130', sort: false, align: 'center',exportable:true,colspan:2,totalRow:true}

        ],
          [
            {field: 'yfhTotalCapacity', title: '户用',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true},
            {field: 'yk_yfh_amount', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true,totalRow:true
              ,templet:function(d){
                var htm = "";
                if(d.yk_yfh_amount != null) {
                  htm += d.yk_yfh_amount;
                } else {
                  htm += '0';
                }
                htm += '<a onclick="ykBzjClicked(\'' + d.id + '\', \'' + (d.yk_yfh_amount != null ? d.yk_yfh_amount : '0') + '\', \'1\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                return htm;
              }
            },
            {field: 'total1', title: '合计', minWidth: '130', sort: false, align: 'center', exportable: true,totalRow:true
              // , templet: function (d) {
              //   var sum = d.yfhTotalCapacity + d.yk_yfh_amount;
              //   var htm = "";
              //   htm += sum
              //   return htm;
              // }
            },
            {field: 'yazTotalCapacity', title: '户用',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true},
            {field: 'yk_yaz_amount', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true,totalRow:true
              ,templet:function(d){
                var htm = "";
                if(d.yk_yaz_amount != null) {
                  htm += d.yk_yaz_amount;
                } else {
                  htm += '0';
                }
                htm += '<a onclick="ykBzjClicked(\'' + d.id + '\', \'' + (d.yk_yaz_amount != null ? d.yk_yaz_amount : '0') + '\', \'2\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                return htm;
              }
            },
            {field: 'total2', title: '合计',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true
              // , templet: function (d) {
              //   var sum = d.yazTotalCapacity + d.yk_yaz_amount;
              //   var htm = "";
              //   htm += sum
              //   return htm;
              // }
            },
            {field: 'yfdTotalCapacity', title: '户用',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true},
            {field: 'yk_yfd_amount', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true,totalRow:true
              ,templet:function(d){
                var htm = "";
                if(d.yk_yfd_amount != null) {
                  htm += d.yk_yfd_amount;
                } else {
                  htm += '0';
                }
                htm += '<a onclick="ykBzjClicked(\'' + d.id + '\', \'' + (d.yk_yfd_amount != null ? d.yk_yfd_amount : '0') + '\', \'3\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                return htm;
              }
            },
            {field: 'total3', title: '合计',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true
              // , templet: function (d) {
              //   var sum = d.yfdTotalCapacity + d.yk_yfd_amount;
              //   var htm = "";
              //   htm += sum
              //   return htm;
              // }
            },

            {field: 'warning', title: '数量',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true,
              templet: function(d){
                return "<a lay-href=\"${ctx}/materialBatch/materialBathList?id="+d.id+"\" lay-text=\"库存批次\" style='font-size:13px;' class=\"layui-table-link layui-icon nav \">"+d.warning+"</a>&nbsp;&nbsp;";
              }
            }
            ,{field: 'totalPrice', title: '金额（元）',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true
            // ,templet: function (d) {
            //   return d.price1 + d.price2 + d.price3 + d.price4;
            // }
          }
            ,{field: 'num1', title: '数量',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true,
            templet: function(d){
              return "<a lay-href=\"${ctx}/materialBatch/materialBathList?inStockTime=1&id="+d.id+"\" lay-text=\"库存批次\" style='font-size:13px;' class=\"layui-table-link layui-icon nav \">"+d.num1+"</a>&nbsp;&nbsp;";
            }
          }
            ,{field: 'price1', title: '金额（元）',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true}
            ,{field: 'num2', title: '数量',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true,
            templet: function(d){
              return "<a lay-href=\"${ctx}/materialBatch/materialBathList?inStockTime=2&id="+d.id+"\" lay-text=\"库存批次\" style='font-size:13px;' class=\"layui-table-link layui-icon nav \">"+d.num2+"</a>&nbsp;&nbsp;";
            }
          }
            ,{field: 'price2', title: '金额（元）',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true}
            ,{field: 'num3', title: '数量',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true,
            templet: function(d){
              return "<a lay-href=\"${ctx}/materialBatch/materialBathList?inStockTime=3&id="+d.id+"\" lay-text=\"库存批次\" style='font-size:13px;' class=\"layui-table-link layui-icon nav \">"+d.num3+"</a>&nbsp;&nbsp;";
            }}
            ,{field: 'price3', title: '金额（元）',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true}
            ,{field: 'num4', title: '数量',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true,
            templet: function(d){
              return "<a lay-href=\"${ctx}/materialBatch/materialBathList?inStockTime=4&id="+d.id+"\" lay-text=\"库存批次\" style='font-size:13px;' class=\"layui-table-link layui-icon nav \">"+d.num4+"</a>&nbsp;&nbsp;";
            }}
            ,{field: 'price4', title: '金额（元）',minWidth:'130', sort: false, align: 'center', exportable:true,totalRow:true}
          ]
        ]
        , exportOpt: {                           //导出选项配置
          ele: '#exportBtn',                   //导出的Dom元素
          headerTitle: '库存管理',             //excel表格的标题
          pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
          maxAmount: 6000,                    //当pageData为false生效
          fileName: '库存管理',
          where: function () {                 //当pageData为false时生效，过滤的条件
            return _serializeJson($("#searchForm"));
          }
        }
      });
    }


    //头工具栏事件
    table.on('toolbar(test)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'addStock':{
          layer.open({
            title:'新增代理商库存',
            type: 2,
            area: ['100%', '100%'],
            content: '${ctx}/material/addStockPage'
          })
          break;
        }
      }
    })


    //监听行单击事件
    table.on('row(test)', function (obj) {
      var data = obj.data;
      var oldData = table.cache["test"];
      var checkCell = obj.tr.find(".layui-form-checkbox");
      var CHECKED;

    });
    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
      var field = data.field;
      //执行重载
      table.reload('test', {
        where: field,
        page: {
          curr: 1
        }
      });
    });

  });

  function ykBzjClicked(id,jq,type) {
    var index =  layer.prompt({title: '修改数据',value:jq}, function(value, index, elem){
      if(!/^(\-)?\d+(\.\d{1,2})?$/.test(value)) {
        layer.msg("请输入数字，最多保留两位小数！");
        return elem.focus();
      }
      $.ajax({
        type:"POST",
        url:"${ctx}/material/editAmount",
        data:{
          id:id,
          type:type,
          amount:value
        },
        success:function(result){
          if(result.code =='200'){
            layer.msg("保存成功",{icon:1,time:1000},function(){
              layer.closeAll()
              $('#searchBtn').click()
            })
          }else{
            layer.msg("修改失败！")
          }

        }
      })
    });
  }
  function exportExcel(){
    window.open('${ctx}/material/exportExcel?'+$('#searchForm').serialize());
  }

  function isBlank(val) {
    if (val == null || val == '' || val == undefined) {
      return true;
    }
    return false;
  }
</script>
</body>
</html>

