<?php

namespace app\admin\controller;
use think\Db;

class DailyRecord extends Auth {
    public function index() {
        if (request()->isAjax()) {
            //分页参数
            $length = input('rows', 10, 'intval');   //每页条数
            $page = input('page', 1, 'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx', 'sort', 'trim');      //排序列
            $sort = input('sord', 'asc', 'trim');        //排序方式
            $order = $sortRow . ' ' . $sort . ' ,id desc';
            $title = input('title', '', 'trim');
            if ($title) {
                $map[] = ['d.title', 'like', '%' . $title . '%'];
            }
            $map[] = ['dr.org_id', '=', $this->orgId];
            $map = empty($map) ? true : $map;
            //数据查询
            $lists = Db::name('daily_record')
                ->alias('dr')
                ->join('daily d', 'd.id=dr.daily_id')
                ->field('dr.*,d.title,d.address_id,d.content as daily_details')
                ->where($map)
                ->limit($start, $length)
                ->order($order)
                ->select();
            foreach ($lists as $k => $v) {
                $lists[$k]['title'] = '';
                if($v['address_id'] >0){
                    $lists[$k]['title'] = Db::name('address')
                        ->where('id',$v['address_id'])
                        ->value('title');
                }
                $lists[$k]['user_name'] = Db::name('user')
                    ->where('id', $v['user_id'])
                    ->value('real_name');
                $status = 0;
                if($v['order_id'] > 0 ){
                    $order =  Db::name('orders')
                        ->where('id',$v['order_id'])
                        ->where('quality_type',0)
                        ->whereIn('order_mode',[5,6])->where('del',0)->find();

                    if($order){
                        $status = 2;
                    }else{
                        $status = 1;
                    }
                }
                $lists[$k]['zg_type'] = $status;
            }
            //数据返回
            $totalCount = Db::name('daily_record')
                ->alias('dr')
                ->field('dr.*,d.title,d.content as daily_details')
                ->join('daily d', 'd.id=dr.daily_id')
                ->where($map)->count();
            $totalPage = ceil($totalCount / $length);
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }
        else {
            $this->assign('meta_title', '检查记录列表');
            return $this->fetch();
        }
    }
    public function details($id) {
        if (!$id) {
            $this->error('参数错误');
        }
        $ret = Db::name('daily_record')->where('id', $id)->find();
        $daily = Db::name('daily')->field('title,content,address_id')
            ->where('id', $ret['daily_id'])->find();
        $daily['title'] = '';
        if($daily['address_id'] >0){
            $daily['title'] = Db::name('address')
                ->where('id',$daily['address_id'])
                ->value('title');
        }
        $ret['daily_name'] = $daily['title'];
        $ret['images'] = $ret['images']?explode(',',$ret['images']):[];
        $ret['daily_details'] = $daily['content'];
        $ret['check_json'] = json_decode($ret['check_json'], true);
        $ret['user_name'] = Db::name('user')
            ->where('id', $ret['user_id'])->value('real_name');
        $status = 0;
        if($ret['order_id'] > 0 ){
            $order =  Db::name('orders')
                ->where('id',$ret['order_id'])
                ->where('quality_type',0)
                ->whereIn('order_mode',[5,6])->where('del',0)->find();

            if($order){
                $status = 2;
            }else{
                $status = 1;
            }
        }
        $ret['zg_type'] = $status;
        $this->assign('info', $ret);
        $this->assign('meta_title', '日常工作记录详情');
        return $this->fetch();
    }
    //excel导出
    public function export() {
        $meta_title = '日常工作记录';
        if (request()->isPost()) {
            $start = input('start');
            $end = input('end');
            $where[] = ['create_yyyymmdd', '>=', $start];
            $where[] = ['create_yyyymmdd', '<=', $end];
            $where[] = ['org_id', '=', $this->orgId];
            $ret = Db::name('daily_record')->where($where)->select();
            foreach ($ret as $k => $v) {
                $daily = Db::name('daily')
                    ->field('title,content,address_id')->where('id', $v['daily_id'])->find();
                $daily['title'] = '';
                if($daily['address_id'] >0){
                    $daily['title'] = Db::name('address')
                        ->where('id',$daily['address_id'])
                        ->value('title');
                }
                $ret[$k]['user_name'] = Db::name('user')->where('id', $v['user_id'])->value('real_name');
                $ret[$k]['daily_name'] = $daily['title'];
                $ret[$k]['daily_details'] = $daily['content'];
            }
            include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
            //实例化PHPExcel类
            $objPHPExcel = new \PHPExcel();
            //激活当前的sheet表
            $objPHPExcel->setActiveSheetIndex(0);
            //设置表格头(即excel表格的第一行)
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '编号')
                ->setCellValue('B1', '任务地点名称')
                ->setCellValue('C1', '任务详情')
                ->setCellValue('D1', '工作人员')
                ->setCellValue('E1', '时间')
                ->setCellValue('F1', '内容')
                ->setCellValue('G1', '工作表单');
            // 设置表格头水平居中
            $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(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
            $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(50);
            //循环刚取出来的数组,将数据逐一添加到excel表格。
            for ($i = 0; $i < count($ret); $i++) {
                $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['id']);//ID
                $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['daily_name']);//日常工作名称
                $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['daily_details']);//日常工作详情
                $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['user_name']);//工作人员
                $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $ret[$i]['create_time']);//时间
                $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $ret[$i]['content']);//content
                $result = json_decode($ret[$i]['check_json'], true);
                $str = array();
                foreach ($result as $kk => $vv) {
                    $ss = $vv['title'] . ":";
                    if ($vv['result'] == 1) {
                        $ss .= '☑';
                    }
                    else {
                        $ss .= '□';
                    }
                    $str[] = $ss;
                }
                $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), implode(' ; ', $str));//工作项
            }
            //设置保存的Excel表格名称
            $filename = '日常工作记录_' . date('YmdHis', time()) . '.xls';
            //设置当前激活的sheet表格名称
            $objPHPExcel->getActiveSheet()->setTitle('日常工作记录');
            //设置浏览器窗口下载表格
            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');
        }
        $this->assign('meta_title', $meta_title);
        return $this->fetch();
    }
}