<style type="text/css">
  .canedit{
    color: red;
    font-size: 5px;
  }
</style>
<link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/css/bootstrap-datepicker.standalone.min.css">
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>

<div class="content_wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".addadvertiser">添加渠道商</button>
                                    <button type="button" class="btn btn-success" data-toggle="modal" data-target=".adddailydata">添加每日广告数据</button>
                                    <a href="<?php echo base_url('#advertiser/adverList');?>" class="btn btn-default" role="button">渠道商列表</a>
                                </div>
                            </div>
                        </div>
                    </section>
                    
                    <!-- <section style="margin-top: 10px;">
                      <div class="container-fluid">
                        <div class="row">
                          <div class="col-sm-12">
                            <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
                          </div>
                        </div>
                      </div>
                    </section> -->
                </aside>
            </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <aside>
              <header><i class="fa fa-fw fa-file"></i>查询</header>
              <section>
                <div class="container-fluid">
                  <form class="form-inline" role="form" method="POST" action="#advertiser/index" name="adinfo">
                      <div class="form-group">
                              <label>开始时间</label>
                              <input type="text" class="form-control datetimepicker" value="<?php echo $stime;?>" placeholder="开始时间" name="ad_start_time" />
                      </div>
                      <span class="gap"></span>
                      <div class="form-group">
                              <label>结束时间</label>
                              <input type="text" class="form-control datetimepicker" value="<?php echo $etime;?>" placeholder="结束时间" name="ad_end_time" />
                      </div>
                      <span class="gap"></span>
                      <div class="form-group">
                          <label>渠道商</label>
                          <span class="gap"></span>
                          <?php foreach($adList as $adinfo){?>
                            <label class="checkbox-inline">
                              <input <?php if(in_array($adinfo['id'], $click_adList) ){ echo "checked";}?> type="checkbox" name="adname[]" value="<?php echo $adinfo['id'];?>"><?php echo $adinfo['ad_name'];?>
                            </label>
                          <?php } ?>
                      </div>
                      <span class="gap"></span>
                      <button type="submit" class="btn btn-primary">查询</button>
                  </form>
                </div>
              </section>
            </aside>
          </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="table-responsive">
                          <table class="table table-striped table-bordered table-hover">
                            <thead>
                              <tr>
                                <th>渠道</th>
                                <th>平台</th>
                                <th>安装量</th>
                                <th>收入</th>
                                <th>花费</th>
                                <th>当日ROI</th>
                                <th>7日ROI</th>
                                <th>CPI</th>
                                <th>渠道留存</th>
                                <th>留存</th>
                                <th>占比(安装/当日新增)</th>
                                <th>操作</th>
                              </tr>
                            </thead>
                            <tbody>
                              <?php 
                                foreach ($dailyReport as $day => $list) {
                                  $sublen = count($list);
                                  $i = 1;
                                  $adZrLc = $adInstall = $adMoney = $adIncome = $adInstallLc = $income7day = $cost7day = 0;
                                  foreach ($list as $val) {
                                    $adInstall += $val['ad_num'];
                                    $adMoney += $val['ad_money'];
                                    $adIncome += $val['ad_income'];
                                    $adInstallLc += $val['ad_install_lc'];
                                    $income7day += $val['income7day'];
                                    $cost7day += $val['cost7day'];
                                    if ($val['ad_zr_lc']) {
                                      $adZrLc = $val['ad_zr_lc'];
                                    }
                              ?>
                              <tr colid='<?php echo $val['id'];?>'>
                                <td><?php echo $val['ad_name'];?></td>
                                <td><?php echo $platform[$val['ad_platform']];?></td>
                                <td class="changeinput" colname='ad_num'><?php echo $val['ad_num'];?></td>
                                <td class="changeinput" colname='ad_income'><?php echo $val['ad_income'];?></td>
                                <td class="changeinput" colname='ad_money'><?php echo $val['ad_money'];?></td>
                                <td><?php echo $val['ad_money'] == 0 ? 0 :round(($val['ad_income']-$val['ad_money'])/$val['ad_money'], 4)*100;?>%</td>
                                <td><?php echo $val['roi7day']*100;?>%</td>
                                <td><?php echo $val['ad_num'] == 0 ? 0 :round($val['ad_money']/$val['ad_num'], 4);?></td>
                                <td class="changeinput" colname='ad_install_lc'><?php echo $val['ad_install_lc'];?></td>
                                <td class="changeinput" colname='ad_zr_lc'><?php echo $val['ad_zr_lc'];?></td>
                                <td>
                                  <?php
                                    $newp = $newplayerList[$day];
                                    if ($newp == 0) {
                                      echo 0;
                                    }else{
                                       $zb = round($val['ad_num']/$newp, 4);
                                       echo $zb*100;
                                    }
                                  ?>                                    
                                  %
                                </td>
                                <td>
                                  <a href="<?php echo base_url('#advertiser/editad/'.$val["id"]);?>"  class="btn btn-primary">编辑</a>

                                  <button adid="<?php echo $val['id'];?>" type="button" class="btn btn-danger delad">删除</button></td>
                                
                              </tr>
                              
                              <?php $i++; } ?>
                              <tr>
                                <td colspan="2" style="text-align: center;"><b><?php echo date('Y-m-d', strtotime($day));?>号汇总:</b></td>
                                <td><?php echo $adInstall;?></td>
                                <td><?php echo $adIncome;?></td>
                                <td><?php echo $adMoney;?></td>
                                <td>
                                  <?php
                                    if ($adMoney == 0) {
                                      echo '0%';
                                    }else{
                                      echo round( ($adIncome - $adMoney)/ $adMoney, 4)*100;
                                    }
                                  ?>%
                                </td>
                                <td>
                                  <?php
                                    if ($cost7day == 0) {
                                      echo 0;
                                    }else{
                                      echo round( ($income7day - $cost7day)/$cost7day, 4 )*100;
                                    }
                                  ?>
                                %</td>
                                <td><?php echo $adInstall == 0 ? 0 : round($adMoney/$adInstall, 4);?></td>
                                <td><?php echo $adInstallLc;?></td>
                                <td><?php echo $adZrLc;?></td>
                                <td colspan="2"><?php echo $newp == 0 ? 0 : round($adInstall/$newp, 4)*100;?>%</td>
                              </tr>
                              <?php } ?>
                              <tr>
                                <td><b>统计:</b></td>
                                <td><b>累计广告安装:</b></td>
                                <td><?php echo $totalAdInstall;?></td>
                                <td><b>累计广告花费:</b></td>
                                <td colspan="8"><?php echo $totalAdMoney;?></td>
                              </tr>
                            </tbody>
                          </table> 
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<div class="modal fade addadvertiser" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">添加渠道商</h4>
      </div>
    <form action="<?php echo base_url('advertiser/addadver');?>" method="post" class="form-horizontal" data-sendingText="提交中">
      <div class="modal-body">
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">渠道商名称:</label>
            <div class="col-sm-9">
                <label class="input-inline">
                    <input type="text" class="form-control" name="adname">
                </label>
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary J_ajaxSubmitBtn">提交</button>
      </div>
    </form>
    </div>
  </div>
</div>

<div class="modal fade adddailydata" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">添加渠道商数据</h4>
      </div>
    <form action="<?php echo base_url('advertiser/addInfo');?>" method="post" class="form-horizontal" data-sendingText="提交中">
      <div class="modal-body">
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">渠道商名称:</label>
            <div class="col-sm-9">
                <label class="select-inline">
                    <select class="form-control" name="ad_id">
                    <?php foreach($adList as $adinfo){?>
                      <option value="<?php echo $adinfo['id'];?>"><?php echo $adinfo['ad_name'];?></option>
                    <?php } ?>
                    </select>
                </label>
            </div>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">安装量:</label>
            <div class="col-sm-9">
                <label class="input-inline">
                    <input type="text" class="form-control" name="ad_num">
                </label>
            </div>
          </div>
<!--          <div class="form-group">-->
<!--            <label for="message-text" class="col-sm-3 control-label">收入:</label>-->
<!--            <div class="col-sm-9">-->
<!--                <label class="input-inline">-->
<!--                    <input type="text" class="form-control" name="ad_income">-->
<!--                </label>-->
<!--            </div>-->
<!--          </div>-->
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">花费:</label>
            <div class="col-sm-9">
                <label class="input-inline">
                    <input type="text" class="form-control" name="ad_money">
                </label>
            </div>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">渠道留存:</label>
            <div class="col-sm-9">
                <label class="input-inline">
                    <input type="text" class="form-control" name="ad_install_lc">
                </label>
            </div>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">投放平台:</label>
            <div class="col-sm-9">
                <label class="input-inline">
                    <select class="form-control" name="ad_platform">
                    <?php foreach($platform as $k => $v){?>
                      <option value="<?php echo $k;?>"><?php echo $v;?></option>
                    <?php } ?>
                    </select>
                </label>
            </div>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-sm-3 control-label">时间:</label>
            <div class="col-sm-5">
                <input type="text" class="form-control datetimepicker" value="" placeholder="记录时间" name="ad_time" />
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary J_ajaxSubmitBtn">提交</button>
      </div>
    </form>
    </div>
  </div>
</div>
<script>
    //选择日期
    $('.datetimepicker').datepicker({
        language:'zh-CN',
        format:'yyyy-mm-dd',
        zIndexOffset:906  //控制z-index
    });

    //变input框
    $('.changeinput').dblclick(function(){
      var colId = $(this).parent().attr('colid');
      var colName = $(this).attr('colname');

      var td = $(this);
      var tbVal = td.text();
      var txt = $("<input type='text'>").val(tbVal);
      txt.blur(function(){
          var newText = $(this).val();
          $.post(
            "<?php echo base_url('advertiser/ajaxEdit');?>",
            {adid:colId, colname:colName, val:newText}
          );
          // 移除文本框,显示新值
          $(this).remove();
          td.text(newText);
      });
      td.text("");
      td.append(txt);

    });

    //删除操作
    $('.delad').click(function(){
      if (confirm('确定删除吗?') == true) {
       var adId = $(this).attr('adid');
       $.post(
            "<?php echo base_url('advertiser/delAd');?>",
            {adid:adId},
          );
       alert('删除成功');
       $(this).parent().parent().remove();
       //window.location.reload();
      }
    });
</script>