<?php

/**
 *    会议相关
 *    请求地址如：http://oa.test/api.php?m=openmeet&a=test&openkey=fee5efd3a93ca5c6a85b679cde60faa2
 */
class openmeetClassAction extends openapiAction
{

    public function testAction() {
        $str = $this->postdata;
        return json_encode($str);
    }

    // 获取会议信息
    public function getMeetInfoAction() {
        $rawArr = $this->getpostarr();

        if (!isset($rawArr['device_id'])) {
            $this->showreturn([], "参数错误", 201);
        }

        $rawData = m('meeting_room')->getone(" mac = '{$rawArr['device_id']}'");

        if (!isset($rawData['room_name'])) {
            $this->showreturn([], "未找到会议室", 202);
        }

        $sqlStr = /** @lang text */
            "SELECT 1 as type, id, title, start_time, end_time, null as compere, meet_state  FROM `xinhu_meeting` where start_time REGEXP '".date("Y-m-d")."' and meeting_room = '".$rawData['room_name']."'
             union all
             SELECT 2 as type, id, title, start_time, end_time, compere, meet_state  FROM `xinhu_meeting_key` where start_time REGEXP '".date("Y-m-d")."' and meeting_room = '".$rawData['room_name']."'";

        $meetData = $this->db->getall($sqlStr);

        if (count($meetData) == 0) {
            $this->showreturn(["meeting_list"=>[],"basic_information"=>[],"meetingTopic"=>[],"participants"=>[]], "暂无会议", 203);
        }


        $data = [];

        for ($i = 0; $i < count($meetData); $i++) {
            $meeting = $meetData[$i];
            $data['meeting_list'][$i] = [
                "meeting_name"=>$meeting['title'],
                "meeting_time"=>date("H:i", strtotime($meeting['start_time'])).' ~ '.date("H:i", strtotime($meeting['end_time'])),
                "meeting_moderator"=>$meeting['compere'],
                "meeting_state"=>$meeting['meet_state'],
                "meeting_state_text"=>m("meeting")->hyarra[$meeting['meet_state']],
            ];

            // 时间到了自动结束一下
            if ($meeting['end_time'] < date("Y-m-d H:i:s")) {
                if ($meeting['type'] == 1) {
                    m("meeting")->update(["meet_state"=>2], "id=".$meeting['id']);
                } else {
                    m("meeting_key")->update(["meet_state"=>2], "id=".$meeting['id']);
                }
            }
        }

        $nowMeetingSql = /** @lang text */
            "SELECT 1 as type, id, title, start_time, end_time, null as compere, meet_state, meeting_room  FROM `xinhu_meeting` where start_time < now() and end_time > now() and meeting_room = '".$rawData['room_name']."'
             union all
             SELECT 2 as type, id, title, start_time, end_time, compere, meet_state, meeting_room  FROM `xinhu_meeting_key` where start_time < now() and end_time > now() and meeting_room = '".$rawData['room_name']."'";
        $nowMeetingData = $this->db->getall($nowMeetingSql);

//        return $nowMeetingSql;

        if (count($nowMeetingData) > 0) {
            $nowMeet = $nowMeetingData[0];
            $data['$basic_information'] = [
                "meeting_room_name" => $nowMeet['meeting_room'],
                "meeting_name" => $nowMeet['title'],
                "meeting_time" => date("H:i", strtotime($nowMeet['start_time'])).' ~ '.date("H:i", strtotime($nowMeet['end_time'])),
                "meeting_moderator" => $nowMeet['compere'],
            ];
            if ($nowMeet['type'] == 2) {
                $topics = $this->db->getall("select `id`, `topic_title`, `topic_attendee`, `topic_state`, `topic_info` from `[Q]meeting_topics` where mid = {$nowMeet['id']} order by sort");
                $this_topics = [];
                $next_topics = [];
                // 会议议题处理
                for ($i = 0; $i < count($topics); $i++) {
                    $info = $topics[$i];
                    $data['meetingTopic'][$i]['state'] = $info['topic_state'];
                    $data['meetingTopic'][$i]['tip'] = m("meeting")->ytarra[$info['topic_state']];
                    $data['meetingTopic'][$i]['text'] = $info['topic_title'];
                    if ($info['topic_state'] == 2) {
                        $this_topics = [
                            "topics" => $info['topic_title'],
                            "personnel_list" => $info['topic_info'],
                        ];

                        if (isset($topics[$i + 1])) {
                            $next_topics = [
                                "topics" => $topics[$i + 1]['topic_title'],
                                "personnel_list" => $topics[$i + 1]['topic_info'],
                            ];
                        }
                    }

                }

                $data['participants'][] = $this_topics;
                $data['participants'][] = $next_topics;

            } else {
                $data['meetingTopic'] = [];
                $data['participants'] = [];
            }
        } else {
            $data['$basic_information'] = [
                "meeting_room_name" => "空闲中",
                "meeting_name" => null,
                "meeting_time" => null,
                "meeting_moderator" => null,
            ];
            $data['meetingTopic'] = [];
            $data['participants'] = [];
        }

        $this->showreturn($data, "请求成功");
    }


    protected function getWeek($date=null) {
        if (!isset($date)) {
            $date = date("Y-m-d H:i:s");
        }
        // 将日期转换为时间戳
        $timestamp = strtotime($date);

        // 格式化输出星期几（返回值为英文）
        $weekday_en = date('l', $timestamp);

        // 如果需要显示中文星期几，则需要进行相关处理
        switch ($weekday_en) {
            case 'Monday':
                $weekday_cn = "星期一";
                break;
            case 'Tuesday':
                $weekday_cn = "星期二";
                break;
            case 'Wednesday':
                $weekday_cn = "星期三";
                break;
            case 'Thursday':
                $weekday_cn = "星期四";
                break;
            case 'Friday':
                $weekday_cn = "星期五";
                break;
            case 'Saturday':
                $weekday_cn = "星期六";
                break;
            default:
                $weekday_cn = "星期天";
        }
        return $weekday_cn;
    }

}