<?php include_once(mvc::$cfg['PATH_TPL'].'public/headBase.php');?>
<script type="text/javascript" src="<?=mvc::$cfg['SELF_URL']?>static/js/xm-select.js"></script>
<div class="layui-card">
	<div class="layui-card-body">
		<?php if(!$Params['type']){?>
		<div>
			<form class="layui-form" id="search">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">月份区间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="betime" id="betime" class="layui-input" value="<?=$Params['betime'];?>" placeholder=" ~ ">
                        </div>
                    </div>
                    <!-- 品牌 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">品牌</label>
                        <div class="layui-input-inline">
                            <div id="brand" style="width: 150px;"></div>
                        </div>
                    </div>
				
                    <!-- 统计类型 -->
                    <div class="layui-inline" >
                        <label class="layui-form-label">统计类型</label>
                        <div class="layui-input-inline" style="width: auto;">
                            <?php foreach ($groupTypeArr as $key => $val): ?>
                                <input type="checkbox" name="group[]" lay-filter="group_fields" value="<?= $key ?>" title="<?= $val ?>" <?php if (in_array($key,$group)) echo 'checked'; ?>>
                            <?php endforeach; ?>
                        </div>
                    </div>
					<div class="layui-inline">
                        <button class="layui-btn act-btn" type="submit">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
			</form>
		</div>
		<?php } ?>
		<div style="margin-bottom: 10px;">
			<button class="layui-btn layui-btn-xs" id="btn_heapline">对比折线图</button>
			<button class="layui-btn layui-btn-xs" id="btn_export">导出</button>
		</div>
		<div style="overflow-x: auto;">
            <div style="display: inline-block; min-width: 100%;" class="layui-form">
				<table class="layui-table YpTable" style="width: auto; white-space: nowrap;">
				<thead class="Filter Sortable">
						<tr>
							<?php if ($Params['level']=='year') { ?>
								<th><input type="checkbox" lay-filter="lay-selectall" value="year_id" lay-skin="primary"/></th>
							<?php } else if ($Params['level']=='series') { ?>
								<th>
									<input type="checkbox" lay-filter="lay-selectall" value="series_id" lay-skin="primary"/>
								</th>
							<?php }else if ($Params['level']=='brand') { ?>
								<th>
									<input type="checkbox" lay-filter="lay-selectall" value="brand_id" lay-skin="primary"/>
								</th>
							<?php }else if ($Params['level']=='city') { ?>
								<th>
									<input type="checkbox" lay-filter="lay-selectall" value="city_id" lay-skin="primary"/>
								</th>
							<?php }else if ($Params['level']=='province') { ?>
								<th>
									<input type="checkbox" lay-filter="lay-selectall" value="province_id" lay-skin="primary"/>
								</th>
							<?php }?>
							
		
							<?php if ($showCols['brand']): ?>
							<th  class="Sort" width="60px"><span>品牌</span></th>
							<?php endif; ?>
							<!-- 车系列 -->
							<?php if ($showCols['mod1']): ?>
								<th class="Sort" width="60px"><span>车系</span></th>
							<?php endif; ?>

							<!-- 车代列 -->
							<?php if ($showCols['mod2']): ?>
								<th class="Sort" width="60px"><span>车代</span></th>
								<th class="Sort" width="90px">年份</th>
							<?php endif; ?>

							<!-- 省份列 -->
							<?php if ($showCols['province']): ?>
								<th class="Sort" width="60px"><span>省份</span></th>
							<?php endif; ?>

							<!-- 城市列 -->
							<?php if ($showCols['city']): ?>
								<th class="Sort" width="60px"><span>城市</span></th>
							<?php endif; ?>
							
							<th sort="float" class="Sort" width="60px"><span>排名</span></th>
							<th width="60px"><span>操作</span></th>
							<th sort="float" class="Sort" width="60px"><span>占比</span></th>
						
							<th sort="float" class="Sort" width="60px">总热度</th>	
							<?php foreach ($months as $month): ?>
								<th sort="float" class="Sort" width="60px"><span><?= $month ?></span></th>
							<?php endforeach; ?>
						</tr>
					</thead>
				<tbody>
					<?php if(!empty($tjArr)){ ?>
						<?php foreach ($tjArr as $row): ?>
							<tr>
								<?php if ($Params['level']=='year') { ?>
									<td><input lay-skin="primary" type="checkbox" name="year_id" value="<?php echo $row['mod2_id']; ?>"></td>
								<?php } else if ($Params['level']=='series') { ?>
									<td><input lay-skin="primary" type="checkbox" name="series_id" value="<?php echo $row['mod1_id']; ?>"></td>
								<?php }else if ($Params['level']=='brand') { ?>
									<td><input lay-skin="primary" type="checkbox" name="brand_id" value="<?php echo $row['brand_id']; ?>"></td>
								<?php }else if ($Params['level']=='city') { ?>
									<td><input lay-skin="primary" type="checkbox" name="city_id" value="<?php echo $row['city_id']; ?>"></td>
								<?php }else if ($Params['level']=='province') { ?>
									<td><input lay-skin="primary" type="checkbox" name="province_id" value="<?php echo $row['province_id']; ?>"></td>
								<?php }?>
								
								<?php if ($showCols['brand']): ?>
								<td><?= $row['brand_name'] ?></td>
								<?php endif; ?>
								<?php if ($showCols['mod1']): ?>
									<td><?= $row['mod1_name'] ?></td>
								<?php endif; ?>

								<?php if ($showCols['mod2']): ?>
									<td><?= $row['mod2_name'] ?></td>
									<td><?= $row['years'] ?></td>
								<?php endif; ?>

								<?php if ($showCols['province']): ?>
									<td><?= $row['province'] ?></td>
								<?php endif; ?>

								<?php if ($showCols['city']): ?>
									<td><?= $row['city'] ?></td>
								<?php endif; ?>	

                                <td><?= $row['rank']?></td>
								<td>
								<button
								id="single_line_statistic"
								data-id="<?php switch ($Params['level']) {
                                case 'brand':
                                    echo $row['brand_id'];
                                    break;
                                case 'series':
                                    echo $row['mod1_id'];
                                    break;
                                case 'year':
                                    echo $row['mod2_id'];
                                    break;
								case 'city':
									echo $row['city_id'];
									break;
								case 'province':
									echo $row['province_id'];
									break;
                            } ?>"
								class="layui-btn layui-btn-xs">折线图</button>
								</td>
								<td><?= $row['percent']>0? $row['percent'].'%':'' ?></td>
					
								<td><?=$row['total']?></td>
								<?php foreach ($months as $month): ?>
									<td><?= $row[$month]>0?$row[$month]:'' ?></td>
								<?php endforeach; ?>
							</tr>
						<?php endforeach; ?>
					<?php }?>
				</tbody>
				</table>	
			</div>
		</div>
		<div><?=$Page?></div>
	</div>

</div>

<?php include_once(mvc::$cfg['PATH_TPL'].'public/footBase.php');?>

<script>
	var brandList = <?=json_encode($brandList)?>;
	$(document).ready(function (){
		<?php if(!$Params['type']){?>
		layui.use(['table', 'laydate','form'], function(){
			var table = layui.table,laydate = layui.laydate,form = layui.form;
			laydate.render({
				elem: '#betime'
				,range: '~'
				,type:'month'
				,min:'<?=$limitYear['minYear']?>'
				,max:'<?=$limitYear['maxYear']?>'
			});
			form.on('checkbox(lay-selectall)', function(data){
			$('[name="'+data.value+'"').prop('checked', data.elem.checked);
				form.render('checkbox');
			});
		});
		var  xmSelectObj = xmSelect.render({
			el: '#brand',
			name: 'brand',
			size: 'mini',
			tips:'请选择或搜索',
			searchTips:'请选择或搜索',
			autoRow: true,
			filterable: true,
			style:{
				transfer: true
			},
			data:brandList,
			on:function(data){

				var arr = data.arr;
				var id = [];
				for (var key in arr) {
					id.push(arr[key].value);
				}
		
			},
		});
		<?php }else{?>
			layui.use(['table', 'laydate','form'], function(){
			var table = layui.table,laydate = layui.laydate,form = layui.form;
			
			form.on('checkbox(lay-selectall)', function(data){
				$('[name="'+data.value+'"').prop('checked', data.elem.checked);
				form.render('checkbox');
			});
		});
		<?php }?>
		 // 点击表格单元格打开折线统计图弹窗
		$(document).on('click', '#btn_heapline', function() {
            var level = '<?=$Params['level']?>';
			var betime = '<?=$Params['betime']?>';
            // 获取表格选中的ID
            var selectedIds = [];
            var idParam = '';
           	if (level =='year') {
				
                $('.layui-table').find('input[name="year_id"]:checked').each(function() {
                    selectedIds.push($(this).val());
                });
                idParam = 'ids=' + selectedIds.join(',');
            } else if (level =='series') {
		
                $('.layui-table').find('input[name="series_id"]:checked').each(function() {
                    selectedIds.push($(this).val());
                });
                idParam = 'ids=' + selectedIds.join(',');
            } else if(level == 'brand'){
				$('.layui-table').find('input[name="brand_id"]:checked').each(function() {
                    selectedIds.push($(this).val());
                });
                idParam = 'ids=' + selectedIds.join(',');
				idParam += '&<?=http_build_query($Params)?>';
			} else if(level == 'city'){
				$('.layui-table').find('input[name="city_id"]:checked').each(function() {
                    selectedIds.push($(this).val());
                });
                idParam = 'ids=' + selectedIds.join(',');
				idParam += '&<?=http_build_query($Params)?>';
			}else if(level == 'province'){
				$('.layui-table').find('input[name="province_id"]:checked').each(function() {
                    selectedIds.push($(this).val());
                });
                idParam = 'ids=' + selectedIds.join(',');
				idParam += '&<?=http_build_query($Params)?>';
			}	
            
            // 验证选中ID数量
            if (selectedIds.length < 1) {
                layer.msg('请至少选择一个项目！', {icon: 2});
                return;
            }
            // 打开弹窗，引入折线图页面
            var url = SELF_URI + 'charts/stackedLineChart?' + "level=" + level + "&betime=" + betime;
            if (idParam) {
                url += '&' + idParam;
            }
            // 标识需要调用的函数
            url += '&func=getBrandChartsData';
            layer.open({
                type: 2,
                title: '折线统计图',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '75%'],
                content: url
            });
        });

		//单一数据折线统计图
        $(document).on('click', '#single_line_statistic', function() {
		
            let level = "<?php echo $Params['level']; ?>";
            let betime = "<?php echo $Params['betime']; ?>";
            let ids = $(this).data('id');
            let url = SELF_URI + 'charts/stackedLineChart?' + "level=" + level + "&betime=" + betime + "&ids=" + ids;
			url += '&<?=http_build_query($Params)?>';
            // 标识需要调用的函数
            url += '&func=getBrandChartsData';
            layer.open({
                type: 2,
                title: '折线统计图',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '70%'],
				zIndex: layer.zIndex,
                content: url
            });
        })

		$("#btn_export").click(function(){
			//当前链接
			window.open(window.location.href+"&isExport=1");
		})
	})
</script>

