/**
  * Creates a new MeetingDetails model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new MeetingDetails();
     $modelMeetingSchedules = new MeetingSchedules();
     $request = Yii::$app->request;
     $id = $request->get('id');
     $model->congress_id = $id;
     $isModelLoaded = $model->load(Yii::$app->request->post());
     if ($isModelLoaded) {
         //If data comes from post and loaded in models
         $modelMeetingSchedules = Model::createMultiple(MeetingSchedules::classname());
         Model::loadMultiple($modelMeetingSchedules, Yii::$app->request->post());
         $model->date = \DateTime::createFromFormat('m-d-Y', $model->date)->format('Y-m-d');
         // validate all models
         $valid = $model->validate();
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     //If data is saved then save the amgen attendee records
                     foreach ($modelMeetingSchedules as $modelMeetingSchedule) {
                         $modelMeetingSchedule->meeting_id = $model->id;
                         $modelMeetingSchedule->congress_id = $model->congress_id;
                         $modelMeetingSchedule->isNewRecord = true;
                         if ($modelMeetingSchedule->attendee_id > 0 || $modelMeetingSchedule->ol_attendee_id > 0) {
                             $flag = $modelMeetingSchedule->save(false);
                         }
                     }
                     $postData = \Yii::$app->request->post();
                     $olAttendeeData = isset($postData['OlAttendees']) ? $postData['OlAttendees'] : [];
                     //If data is saved then save the ol attendee records
                     foreach ($olAttendeeData as $olAttendeeInfo) {
                         if (!isset($olAttendeeInfo['id']) || empty($olAttendeeInfo['id'])) {
                             $olAttendeeModel = new \backend\models\OlAttendees();
                             $olAttendeeModel->user_type_id = 1;
                             $parts = explode(" ", $olAttendeeInfo['name']);
                             $lastname = array_pop($parts);
                             $firstname = implode(" ", $parts);
                             $olAttendeeModel->first_name = $firstname;
                             $olAttendeeModel->last_name = $lastname;
                             $olAttendeeModel->name = $olAttendeeInfo['name'];
                             $olAttendeeModel->ol_bio = $olAttendeeInfo['ol_bio'];
                             $olAttendeeModel->ol_phone_no = $olAttendeeInfo['ol_phone_no'];
                             $olAttendeeModel->ol_email = $olAttendeeInfo['ol_email'];
                             $olAttendeeModel->is_active = 1;
                             $olAttendeeModel->isNewRecord = true;
                             if ($olAttendeeModel->validate()) {
                                 if ($flag = $olAttendeeModel->save(false)) {
                                     $olAttendeeId = $olAttendeeModel->id;
                                     $meetingScheduleModel = new MeetingSchedules();
                                     $meetingScheduleModel->meeting_id = $model->id;
                                     $meetingScheduleModel->congress_id = $model->congress_id;
                                     $meetingScheduleModel->ol_attendee_id = $olAttendeeId;
                                     $meetingScheduleModel->isNewRecord = true;
                                     $flag = $meetingScheduleModel->save(false);
                                     if (!$flag) {
                                         $transaction->rollBack();
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (\Exception $ex) {
                 $transaction->rollBack();
             }
         }
     }
     //Meeting date should be between congress start date and end date
     $congressDetail = CongressDetails::findOne($model->congress_id);
     $congressStartDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->start_date)->format('m-d-Y');
     $congressEndDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->end_date)->format('m-d-Y');
     return $this->render('create', ['model' => $model, 'modelMeetingSchedules' => empty($modelMeetingSchedule) ? [new MeetingSchedules()] : $modelMeetingSchedule, 'congressStartDate' => $congressStartDate, 'congressEndDate' => $congressEndDate]);
 }