<?php
namespace app\admin\controller;

use think\App;
use think\Db;

class ConveyPlanRecord extends Auth
{

    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->table = 'convey_plan_record';
    }
    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;

            $title = input('real_name','','trim');
            if($title){
                 $user = Db::name('user')
                     ->alias('u')
                     ->join('user_org o','o.user_id=u.id')
                     ->where('u.del',0)
                     ->where('u.enable',1)
                     ->where('u.real_name','like','%'.$title.'%')
                     ->where('o.org_id',$this->orgId)
                     ->column('u.id');
                 if(empty($user)){
                     $map[] = ['user_id','=',0];
                 }else{
                     $map[] = ['user_id','in',$user];

                 }
            }

            $addr = input('addr','','trim');
            if($addr){
                $map[] = ['addr_id','=',$addr];

            }
            $s = input('start','','trim');
            if($s){
                $ss = date('Ymd',strtotime($s));
                $map[] = ['create_yyyymmdd','>=',$ss];
            }
            $e = input('end','','trim');
            if($e){
                $ee = date('Ymd',strtotime($e));
                $map[] = ['create_yyyymmdd','<=',$ee];
            }

            $map[] = ['org_id','=',$this->orgId];
            $map[] = ['type','=',0];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();

            foreach ($lists as $k=>$v){
                $lists[$k]['real_name'] = Db::name('user')
                    ->where('id',$v['user_id'])
                    ->value('real_name');
                $lists[$k]['address_title'] = Db::name('address')
                    ->where('id',$v['addr_id'])
                    ->value('title');
            }
            //数据返回
            $totalCount = db($this->table)->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{

            $address = (new \app\common\model\Address())->getListByType(2,$this->orgId);
            $this->assign('address',$address);
            return $this->fetch();
        }
    }


    //excel导出
    public function export() {
        $meta_title = '循环签到记录';
        if (request()->isGet()) {
            $sortRow = input('sidx','id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = $sortRow.' '.$sort;

            $title = input('real_name','','trim');
            if($title){
                $user = Db::name('user')
                    ->alias('u')
                    ->join('user_org o','o.user_id=u.id')
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->where('u.real_name','like','%'.$title.'%')
                    ->where('o.org_id',$this->orgId)
                    ->column('u.id');
                if(empty($user)){
                    $map[] = ['user_id','=',0];
                }else{
                    $map[] = ['user_id','in',$user];

                }
            }

            $addr = input('addr','','trim');
            if($addr){
                $map[] = ['addr_id','=',$addr];

            }
            $s = input('start','','trim');
            if($s){
                $ss = date('Ymd',strtotime($s));
                $map[] = ['create_yyyymmdd','>=',$ss];
            }
            $e = input('end','','trim');
            if($e){
                $ee = date('Ymd',strtotime($e));
                $map[] = ['create_yyyymmdd','<=',$ee];
            }

            $map[] = ['org_id','=',$this->orgId];
            $map= empty($map) ? true: $map;
            //数据查询
            $lists = db($this->table)->where($map)->order($order)->select();

            foreach ($lists as $k=>$v){
                $lists[$k]['real_name'] = Db::name('user')
                    ->where('id',$v['user_id'])
                    ->value('real_name');
                $lists[$k]['address_title'] = Db::name('address')
                    ->where('id',$v['addr_id'])
                    ->value('title');
            }
            include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
            //实例化PHPExcel类
            $objPHPExcel = new \PHPExcel();
            //激活当前的sheet表
            $objPHPExcel->setActiveSheetIndex(0);
            //设置表格头(即excel表格的第一行)
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'id')
                ->setCellValue('B1', '签到人')
                ->setCellValue('C1', '位置')
                ->setCellValue('D1', '时间');
            // 设置表格头水平居中
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置列水平居中
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置单元格宽度
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(50);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
            //循环刚取出来的数组,将数据逐一添加到excel表格。
            for ($i = 0; $i < count($lists); $i++) {
                $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['id']);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['real_name']);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['address_title']);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['create_time']);
            }
            //设置保存的Excel表格名称
            $filename = $meta_title.'_' . date('YmdHis', time()) . '.xls';
            //设置当前激活的sheet表格名称
            $objPHPExcel->getActiveSheet()->setTitle($meta_title);
            //设置浏览器窗口下载表格
            ob_end_clean();
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header('Content-Disposition:inline;filename="' . $filename);
            //生成excel文件
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            //下载文件在浏览器窗口
            return $objWriter->save('php://output');
        }

    }



}