<?php

class flow_meeting_keyClassModel extends flowModel
{

    //删除单据时调用
    public function flowdeletebill($sm) {

    }

    //流程全部完成后调用
    public function flowcheckfinsh($sm) {
    }

    //提交时调用
    protected function flowsubmit($na, $sm){
        m('meeting_topics')->update(["topic_state"=>1], '1=1');
        $topic = m('meeting_topics')->getone("mid={$this->id}",'id, topic_state', 'sort');
        if (isset($topic['id']) && $topic['topic_state']<2) {
            m('meeting')->firstMeetingTopicStart($this->id);
        }
    }


    // 初始化单据可替换其他属性，$lx,0默认,1详情展示，2列表显示，3打印页，4外部详情页
    public function flowrsreplace($rs, $lx=0)
    {

//        m('log')->addlog('会议单据', json_encode($rs, true));

        $zt 		 = $rs['meet_state'];
        $is_hand 	 = $rs['is_hand'] ?? 1;
        $nzt 		 = $zt;
        $time 		 = time();

        $stime 	= strtotime($rs['start_time']);
        $etime 	= strtotime($rs['end_time']);


        if($is_hand != 2){
            if($etime < $time){
                $nzt = 2;
            } else if ($stime > $time){
                $nzt = 0;
            } else {
                $nzt = 1;
                $topic = m('meeting_topics')->getone("mid={$this->id}",'id, topic_state', 'sort');
                if (isset($topic['id']) && $topic['topic_state']<2) {
                    m('meeting')->firstMeetingTopicStart($this->id);
                }
            }
        }

        if ($stime <= $time && $nzt != 2) {
            $nzt = 1;
        }

        // 更新数据状态
        if($zt != $nzt){
            $this->update('meet_state='.$nzt.'', $rs['id']);
            $zt = $nzt;
        }

        // 将id转换为文字
        $rs['meet_state'] = m("meeting")->getstatezt($zt);


        return $rs;
    }


    // $ors当前单据操作信息，$crs提交过来的信息
    public function flowoptmenu($ors, $crs)
    {
        // 切换会议议题
        if ($ors['num'] == 'changeTopic') {
            $topics = $this->db->getall("select * from `[Q]meeting_topics` where mid = {$this->id} order by sort");

            for ($i = 0; $i < count($topics); $i++) {
                if (isset($topics[$i]['topic_state']) && $topics[$i]['topic_state'] == 2) {
                    $id = $topics[$i]['id'];
                    $data = ['topic_state' => $topics[$i]['topic_state'] + 1];
                    m('meeting_topics')->update($data, "id={$id}");
                    if (isset($topics[$i + 1]['topic_state'])) {
                        $id = $topics[$i + 1]['id'];
                        $data = ['topic_state' => 2];
                        m('meeting_topics')->update($data, "id={$id}");
                    }
                    break;
                }
            }

            if (isset($topics[count($topics) - 1]) && $topics[count($topics) - 1]['topic_state'] == 2) {
                m('meeting_key')->update(['is_hand' => 2, 'meet_state' => 2], "id={$this->id}");
            }

        } else if ($ors['num'] == 'enableMeeting') {

            $time 		 = time();

            $stime 	= strtotime($this->rs['start_time']);
            $etime 	= strtotime($this->rs['end_time']);

            if ($stime < $time){
                $data = [
                    'meet_state'=>1,
                    'is_hand'=>2
                ];
            } else if ($stime >= $time) {
                $data = [
                    'meet_state'=>0,
                    'is_hand'=>2
                ];
            }
            m('meeting_key')->update($data, "id={$this->id}");

//            $topic = m('meeting_topics')->getone("mid={$this->id}", "id", 'sort desc');
//            if (isset($topic['id'])) {
//                m('meeting_topics')->update(['topic_state'=>'2'], 'id='.$topic['id']);
//            }
        } else if ($ors['num'] == 'disenableMeeting') {

//            $data = [
//                'meet_state'=>2,
//                'is_hand'=>2
//            ];
//
//            m('meeting_key')->update($data, "id={$this->id}");
        }
    }


    //子表数据替换处理$lx=0编辑时,1展示时
    protected function flowsubdata($rows, $lx=0){
        if ($lx == 1) {
            m('log')->addlog('编辑', $lx);
            for ($i = 0; $i < count($rows); $i++) {
                if (isset($rows[$i]['topic_state']) && !empty($rows[$i]['topic_state'])) {
                    $index = $rows[$i]['topic_state'] ?? 1;
                    $rows[$i]['topic_state'] = m('meeting')->ytarra[$index];
                } else {
//                    $id = $rows[$i]['id'];
//                    m('meeting_topics')->update(['topic_state'=>1], "id={$id}");
                }
            }
        }
        return $rows;
    }

}
