<?php

namespace Home\Controller;

use Think\Controller;

class BlackoutPlanController extends ComController
{
    //停电计划
    public function addBlackoutPlan()
    {
        $Data = I('');
        if (!isset($Data['site_id']) || !isset($Data['plan_type']) || !isset($Data['start_time']) || !isset($Data['end_time'])) {
            $res['msg'] = '操作失败，缺少重要参数';
            $res['flag'] = false;
            echo json_encode($res);
            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['contacts'] = $name_data[0]['personnel_name'];
            $Data['creator'] = $name_data[0]['personnel_name'];
        } else {
            $Data['contacts'] = $_SESSION['phone'];
            $Data['creator'] = $_SESSION['phone'];
        }

        $Data['create_time'] = date('Y-m-d H:i:s');
        $Data['enable'] = 1;
        $Data['phone'] = $_SESSION['phone'];

        $R = M("wc_blackout_plan")->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 updateBlackoutPlan()
    {
        $Data = I('');
        if (!isset($Data['id'])) {
            $res['msg'] = '操作失败，缺少重要参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['id'] = $Data['id'];
        if (!empty($Data['site_id'])) {
            $data['site_id'] = $Data['site_id'];
        }
        if (!empty($Data['plan_type'])) {
            $data['plan_type'] = $Data['plan_type'];
        }
        if (!empty($Data['start_time'])) {
            $data['start_time'] = $Data['start_time'];
        }
        if (!empty($Data['end_time'])) {
            $data['end_time'] = $Data['end_time'];
        }
        if (!empty($Data['contacts'])) {
            $data['contacts'] = $Data['contacts'];
        }
        if (!empty($Data['phone'])) {
            $data['phone'] = $Data['phone'];
        }
        $R = M("wc_blackout_plan")->where($where)->save($data);
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    public function delBlackoutPlan()
    {
        $Data = I('');
        if (!isset($Data['id'])) {
            $res['msg'] = '操作失败，缺少重要参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $where['id'] = $Data['id'];
        $data['enable'] = 0;
        $R = M("wc_blackout_plan")->where($where)->save($data);
        if ($R) {
            $res['msg'] = '操作成功';
            $res['flag'] = true;
            echo json_encode($res);
        } else {
            $res['msg'] = '操作失败';
            $res['flag'] = false;
            echo json_encode($res);
        }
    }

    public function getBlackoutPlanList()
    {
        $Data = I('');
        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        if (!isset($Data['status_value'])) {
            $res['msg'] = '操作失败，缺少重要参数';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }

        $where = '';
        if (!empty($Data['site_name'])) {
            $where .= " and b.site_name like '%" . $Data['site_name'] . "%'";
        }

        if ($Data['status_value'] == 1) {
            $where .= " and a.start_time >'" . date('Y-m-d H:i:s') . "'";
        } else if ($Data['status_value'] == 2) {
            $where .= " and a.start_time <'" . date('Y-m-d H:i:s') . "'";
            $where .= " and a.end_time >'" . date('Y-m-d H:i:s') . "'";
        } else if ($Data['status_value'] == 3) {
            $where .= " and a.end_time <'" . date('Y-m-d H:i:s') . "'";
        }

        if (!empty($Data['id'])) {
            $where .= " and a.id = " . $Data['id'];
        }
//        print_r("select a.id,a.site_id,a.plan_type,a.start_time,a.end_time,a.contacts,a.phone,a.enable,a.creator,a.create_time,b.site_name from wc_blackout_plan as a join site as b on  a.site_id=b.id where a.enable=1 " . $where . " order by a.id desc");
        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 wc_blackout_plan as a join site as b on  a.site_id=b.id join personnel as c on b.id=c.site_id where c.personnel_phone='".$_SESSION['phone']."' and a.enable=1 " . $where . " ");

        $R = M()->query("select a.id,a.site_id,a.plan_type,a.start_time,a.end_time,a.contacts,a.phone,a.enable,a.creator,a.create_time,b.site_name from wc_blackout_plan as a join site as b on  a.site_id=b.id join personnel as c on b.id=c.site_id where c.personnel_phone='".$_SESSION['phone']."' and a.enable=1 " . $where . " order by a.id desc".$limit);
        foreach ($R as $key => $value) {
            if (strtotime(date('Y-m-d H:i:s')) > strtotime($R[$key]['start_time'])) {
                if (strtotime(date('Y-m-d H:i:s')) < strtotime($R[$key]['end_time'])) {
                    $R[$key]['status'] = '执行中';
                } else {
                    $R[$key]['status'] = '已执行';
                }
            } else {
                $R[$key]['status'] = '未执行';
            }
        }
        $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);
    }

    public function getSiteBox()
    {
        if (!isset($_SESSION['phone'])) {
            $res['msg'] = '请重新进入公众号';
            $res['flag'] = false;
            echo json_encode($res);
            exit();
        }
        $R = M()->query("select b.id,b.site_name from personnel as a join site as b on a.site_id=b.id where b.enable=1 and a.personnel_phone='" . $_SESSION['phone'] . "' order by b.id desc");
        $res['msg'] = '操作成功';
        $res['flag'] = true;
        $res['data'] = $R;
        echo json_encode($res);
    }
}