text: jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
    return this.flatten().reduce( function ( a, b ) {
        if ( typeof a === 'string' ) {
            a = a.replace(/[^\d.-]/g, '') * 1;
        }
        if ( typeof b === 'string' ) {
            b = b.replace(/[^\d.-]/g, '') * 1;
        }
  
        return a + b;
    }, 0 );
  } );
  
  var admDeals = {
      iniMainTable: function(){
    
        var columns = [
          {
            data:null,
            orderable:false,
            render: function(data, type, row){
              html = `<input name="dcID[]" type="checkbox" id="dcID[]" value="${data.id}" />`
              return html
            }
          },
          {
            data: 'id',
            orderable : false
          },
          {
            data: 'username',
            orderable : false
          },
          {
            data: 'realname',
            orderable : false
          },
          {
            data: 'code',
            orderable : false
          },
          {
            data: 'name',
            orderable : false
          },
          {
            data: 'sgsl',
            orderable : false
          },
          {
            data: 'sgrq',
            orderable : false
          },
          {
            data: 'cjje',
            orderable : false
          },
          {
            data: 'cjsl',
            orderable : false
          },
          {
            data: null,
            orderable : false,
            render: function(data, type, row){
              if(data.state == 0) html = '待抽签'
              else if(data.state == 1) html = '已中签'
              else html = '未中签'
              return html;
            }
          },
          {
            data: null,
            orderable : false,
            render: function(data, type, row){
                url = window.location.origin + '/agent/report/xingu_log/' + data.id + '/edit';
                if(data.state == 0)
                  button_html = `<a href="` + url + `" style="color: gray"><button class="btn btn-sm btn-icon btn-pure btn-default on-default m-r-5 button-edit"
                              data-toggle="tooltip" data-original-title="Edit">
                              <i class="icon-pencil" aria-hidden="true"></i></button></a>`;
                else
                  button_html = '';
                return button_html;
              }
          }
        ];
    
        $('#xingu_log-table').DataTable({
          responsive: true,
          processing: true,
          serverSide: true,
          stateSave: true,
          columns: columns,        
          "language": {
            "lengthMenu": "显示 _MENU_ 行",
            "emptyTable": "没有数据。",
            "search": '搜索',
            'csv': 'CSV 导出',
            "info": "显示_START_ - _END_ / _TOTAL_件",
            "infoEmpty":      "显示0条目中的0到0",
            "infoFiltered":   "(从_MAX_个条目中筛选出)",
            "zeroRecords": "没有匹配的数据。",
            "loadingRecords": "载入中...",
            "processing":     "请稍候...",
            "paginate": {
                "first": '首页',
                'last': '尾页',
                "next": "下页",
                "previous": "上页"
            },
            searchPanes: {
                count: '{total} found',
            }
          },        
          ajax: {
            url: window.location.origin + '/ajax/agent/report/xingu_log',
            type: 'GET',
            data: function (d) {
              d.shengou_date = $('#shengou_date').val()
              d.search = $('input[type="search"]').val()
            },
            headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              },
            error:function(err){
              console.log(err);
            }
          },
        });
      },
      reloadTable: function(){
         $('#xingu_log-table').DataTable().ajax.reload();
      },
  
      deleteRow: function(element) {
        $('#xingu_log-table').DataTable()
          .row( element.parents('tr') )
          .remove()
          .draw();
      }
    };
  
    $('div[class="input-group date mb-3"]').datepicker({
        language: 'zh-CN',
        autoclose: true,
        todayHighlight: true,
        showTodayButton: true,
        format: 'yyyy-mm-dd'
      })
    
    function prevent(e){    
      e.preventDefault();
    }
    
    var validate_form = $("#xingu_log-form").validate({
  		rules: {
  			code: { required: true, rangelength:[6,6], digits:true },
  			name: { required:true },
  			price: {required:true, number: true},
        state:{ required: true},
        cjje:{ required: true, number: true },
        cjsl: { required: true, digits: true }
  		},
  		messages: {
  			code: { required: "[<b>股票代码</b>]:请输入股票代码.", rangelength: '[<b>股票代码</b>]:长度在6位.', digits:'[<b>股票代码</b>]:必须输入整数.'},
        name: { required: '请输入股票名称' },
        price: { required: "[<b>申购价格</b>]:不能为空.", number: "[<b>申购价格</b>]:必须为正数或小数."},
        state: { required: '[<b>状态</b>]:不能为空.'},
        cjje: { required: '[<b>成交金额</b>]:不能为空.', number: '[<b>成交金额</b>]:必须为正数或小数.' },
        cjsl: { required: '[<b>成交数量</b>]:不能为空.', number: '[<b>成交数量</b>]:必须为正数' }
  		},
  		submitHandler: function(form) {
        console.log('form submitted')
        // form.submit();
        
        return true;
  		}
  	});
    
    jQuery(function(){
      // var current_date = new Date();
      // current_date = moment(current_date).format('YYYY-MM-DD');
      // $('#shengou_date').val(current_date);
  
      admDeals.iniMainTable();
      
  
      $('#date_search').on('click', function() {
          console.log('clicked');
          admDeals.reloadTable();
      });
        
    });