<?php

namespace Home\Controller;

use Think\Controller;

class MonitoringReportingController extends ComController
{
    //监察上报
    public function addMonitoringReporting()
    {
        $Data = I('');
        if (!isset($Data['supervision_title']) || !isset($Data['site_id']) || !isset($Data['supervision_content'])) {
            $res['msg'] = '操作失败，缺少重要参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

//        print_r($_FILES);
//        exit();


        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }


        $name_data = M()->query("select personnel_name from personnel where personnel_phone='" . $_SESSION['phone'] . "' limit 0,1");
        if (!empty($name_data[0]['personnel_name'])) {
            $data['creator'] = $name_data[0]['personnel_name'];
        } else {
            $data['creator'] = $_SESSION['phone'];
        }
        $data['supervision_title'] = $Data['supervision_title'];
        $data['site_id'] = $Data['site_id'];
        $data['supervision_content'] = $Data['supervision_content'];
        $data['scene_pictures'] = $Data['img_url'];
        $data['create_time'] = date('Y-m-d H:i:s');

        $R = M("supervision_report")->add($data);
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }
    public function setUpload(){
        //图片上传
//        print_r($_FILES);
        $tmpFilePath = $_FILES['file']['tmp_name'];
        if ($_FILES["file"]["error"] == 1) {
            $res['msg'] = '图片上传失败，图片大小超过限制4M';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (empty($tmpFilePath)) {
            $res['msg'] = '图片上传失败';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $FileCode = date('YmdHis') . rand(1000, 9999);
        $FileName = $_FILES['file']['name'];
        $FileType = strtolower(substr(strrchr($FileName, '.'), 1));
        $BasefName = $FileCode . '.' . $FileType;
//        $newFilePath = 'D:/images/' . $BasefName;//本地路径
//        $newFilePath = 'D:/PHP_www/USKYOF/image/' . $BasefName;//本地路径
        $newFilePath = '/var/www/html/USKYOF/image/' . $BasefName;//线上路径
        $newFilePath1 = '/USKYOF/image/' . $BasefName;
//        print_r($newFilePath);

        if (!move_uploaded_file($tmpFilePath, $newFilePath)) {
            $res['msg'] = '图片上传失败,上传路径错误';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $res['msg'] = '上传成功';
        $res['flag'] = true;
        $res['img_url'] = $newFilePath1;
        echo json_encode($res);

    }

    public function getMonitoringReportingList()
    {
        $Data = I('');
        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!empty($Data['id'])) {
            $where = " and b.id = " . $Data['id'];
        }
        if(!empty($Data['currentPage'])&&!empty($Data['size'])){
            $start_number = ($Data['currentPage']-1)*$Data['size'];
            $limit = " limit ".$start_number.",".$Data['size'];
        }
        $R1 = M()->query("select count(*) as sl from personnel as a join supervision_report as b on a.site_id=b.site_id LEFT JOIN site as s on s.id=b.site_id where a.personnel_phone='" . $_SESSION['phone'] . "' " . $where . " ");
        $R = M()->query("select b.*,s.site_name from personnel as a join supervision_report as b on a.site_id=b.site_id LEFT JOIN site as s on s.id=b.site_id where a.personnel_phone='" . $_SESSION['phone'] . "' " . $where . " order by b.id desc".$limit);
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $R;
        $res['total'] = $R1[0]['sl'];
        $res['currentPage'] = $Data['currentPage'];
        $res['size'] = $Data['size'];
        echo json_encode($res);
    }
}