<?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 bindDeviceAction() {
        $rawArr = $this->getpostarr();

        if (!isset($rawArr['device_id'])) {
            return [
                "code"=>-1,
                "message"=>"参数错误！"
            ];
        }

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

        $now = date('Y年m月d日('.$this->getWeek().') H:i:s');

        if ($rawData['id'] ?? 0 > 0) {
            return [
                "code"=>1,
                "message"=>"已绑定",
                "data"=>[
                    "room_id"=>$rawData['id'],
                    "meetingRoomName"=>$rawData['room_name'],
                    "meetingTime"=>$now,
                    "stateText"=>"空闲中"
                ]
            ];
        } else {
            return [
                "code"=>0,
                "message"=>"设备未绑定!"
            ];
        }
    }

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

        if (!isset($rawArr['room_id'])) {
            return [
                "code"=>-1,
                "message"=>"参数错误！"
            ];
        }

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

        if (!isset($rawData['room_name'])) {
            return [
                "code"=>-1,
                "message"=>"未找到会议室！"
            ];
        }

        $meetData = m('meeting')->getone(" state = 1 and meeting_room = '{$rawData['room_name']}'");

        if (!isset($meetData['id'])) {
            return [
                "code"=>2,
                "message"=>"暂无会议！"
            ];
        }

        $topics = $this->db->getall("select `id`, `topic_title`, `topic_attendee`, `topic_state` from `[Q]topics` where mid = {$meetData['id']} order by sort");


        $now = date('Y年m月d日('.$this->getWeek($meetData['meeting_time']).') H:i:s');
        $date = date('Y年m月d日('.$this->getWeek($meetData['meeting_time']).') H:i:s');
        $datetime = "2021-09-30 15:45:30"; // 给定的日期时间字符串

        // 将日期时间字符串转换为UNIX时间戳
        $ts = strtotime($meetData['meeting_time']);
        // 根据需要格式化日期
        $meetStartDate = date("Y年m月d日", $ts);
        $meetDate=$meetStartDate." (".$this->getWeek($meetStartDate).") ".date("H:i", strtotime($meetData['meeting_time']))."~".date("H:i", strtotime($meetData['end_time']));

        $topicsList = [];

        for ($i = 0; $i < count($topics); $i++) {
            $info = $topics[$i];
            $topicsList[$i]['id'] = $info['id'];
            $topicsList[$i]['text'] = $info['topic_title'];
            $topicsList[$i]['tip'] = $this->topicState($info['topic_state']);
            $topicsList[$i]['state'] = $info['topic_state'];
            $topicsList[$i]['person'] = explode(',', $info['topic_attendee'] );
        }

        $res=[
            "code"=>200,
            "message"=>"请求成功！",
            "data"=>[
                "meetingId"=>$rawData['id'],
                "meetingRoomName"=>$rawData['room_name'],
                "meetingName"=>$meetData['title'],
                "meetingTime"=>$meetDate,
                "meetingPerson"=>explode(",", $meetData['attendees']),
                "meetingTopic"=>$topicsList ?? []
            ]
        ];
        return $res;
    }

    // 切换议题
    public function switchTopicsAction() {
        $rawArr = $this->getpostarr();

        if (!isset($rawArr['meet_id']) || !isset($rawArr['topic_id'])) {
            return [
                "code"=>-1,
                "message"=>"参数错误！"
            ];
        }

        // 获取议题信息
        $topics = $this->db->getall("select `id`, `topic_title`, `topic_attendee`, `topic_attendee_id`, `topic_state` from `[Q]topics` where mid = {$rawArr['meet_id']} order by sort");

        if (!isset($topics) || !isset($rawArr['meet_id'])) {
            return [
                "code"=>-1,
                "message"=>"会议不存在！"
            ];
        }

        $topic_id = $rawArr['topic_id'];

        for ($i = 0; $i < count($topics); $i++) {
            $info = $topics[$i];
            if ($topic_id == $info['id']) {
                $topics[$i]['topic_state'] = $topics[$i]['topic_state'] + 1;
                $topics[$i]['topic_state_info'] = $this->topicState($topics[$i]['topic_state']);
                if (isset($topics[$i - 1])) {
                    $topics[$i-1]['topic_state'] = $topics[$i-1]['topic_state'] + 1;
                    $topics[$i-1]['topic_state_info'] = $this->topicState($topics[$i-1]['topic_state']);
                }

                if (isset($topics[$i + 1])) {
                    $topics[$i+1]['topic_state'] = $topics[$i+1]['topic_state'] + 1;
                    $topics[$i+1]['topic_state_info'] = $this->topicState($topics[$i+1]['topic_state']);
                }
                // 发送短信给 topic_attendee_id

            } else {
                $topics[$i]['topic_state_info'] = $this->topicState($topics[$i]['topic_state']);
            }
        }

        return [
            "code"=>200,
            "message"=>"切换成功！",
            "data"=>$topics
        ];

    }

    protected function topicState($code): string
    {
        $info = '等待';
        switch ($code) {
            case 1:
                $info = '待进行';
                break;
            case 2:
                $info = '进行中';
                break;
            case 3:
                $info = '已进行';
                break;

        }
        return $info;
    }

    protected function getWeek($date) {
        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;
    }

}