<?php
run_query("CREATE TABLE `" . $config['table_prefix'] . "msg_threads` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `add_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `user_id` int(11) NOT NULL DEFAULT 0,
  `form_id` int(11) NOT NULL DEFAULT 0,
  `wait` int(11) NOT NULL DEFAULT 0,
  `sended` int(11) NOT NULL DEFAULT 0,
  `failed` int(11) NOT NULL DEFAULT 0,
  `pause` tinyint(1) NOT NULL DEFAULT 0,
  `scheduled_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `send_status` tinyint(1) NOT NULL DEFAULT 0,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `form_id` (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

run_query("CREATE TABLE `" . $config['table_prefix'] . "msg_queue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` bigint(20) NOT NULL DEFAULT 0,
  `thread_id` int(11) NOT NULL DEFAULT 0,
  `add_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `user_id` int(11) NOT NULL DEFAULT 0,
  `line_id` int(11) NOT NULL DEFAULT 0,
  `contact` varchar(255) NOT NULL DEFAULT '',
  `text` text NOT NULL,
  `channel` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `thread_id` (`thread_id`),
  KEY `user_id` (`user_id`),
  KEY `line_id` (`line_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

run_query("CREATE TABLE `" . $config['table_prefix'] . "msg_archive` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` bigint(20) NOT NULL DEFAULT 0,
  `thread_id` int(11) NOT NULL DEFAULT 0,
  `add_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `user_id` int(11) NOT NULL DEFAULT 0,
  `line_id` int(11) NOT NULL DEFAULT 0,
  `contact` varchar(255) NOT NULL DEFAULT '',
  `text` text NOT NULL,
  `channel` varchar(255) NOT NULL DEFAULT '',
  `sended_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `error_type` tinyint(4) NOT NULL DEFAULT 0,
  `error_text` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `thread_id` (`thread_id`),
  KEY `user_id` (`user_id`),
  KEY `line_id` (`line_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

run_query("ALTER TABLE `" . $config['table_prefix'] . "acc_tables` ADD `adf_acc_msg` INT(11) NOT NULL DEFAULT 0 AFTER `adf_acc_sms`");

run_query("ALTER TABLE `" . $config['table_prefix'] . "forms` ADD `channel` VARCHAR(255) NOT NULL DEFAULT ''");

run_query("INSERT INTO `" . $config['table_prefix'] . "cron` (`description`, `period`, `last_start_time`, `last_stop_time`, `command`, `thread_name`, `check_enabled`, `check_notify`, `check_email`, `check_cond`, `check_text`) VALUES ('Отправка сообщений мессенджеров', '* * * * *', now(), now(), 'send_all_msg();\r\necho \'Done\';', 'send_msg', '1', '1', '1', 'like', 'Done')");

run_query("INSERT INTO `" . $config['table_prefix'] . "calc_template` (`name`, `calculate_tpl`, `params`, `type`, `tpl_name`, `javascript_tpl`, `tpl_category`, `calc_types`, `calc_num`) VALUES ('Отправить шаблон мессенджера', 'global \$thread{\$send_tpl}, \$sended;\r\nif (!\$thread{\$send_tpl}) {\r\n    \$thread{\$send_tpl} = send_template({\$send_tpl}, \'id = \' . \$line[\'id\']);\r\n    \$sended = 1;\r\n} else {\r\n    form_msg_template(\$thread{\$send_tpl}, \'id = \' . \$line[\'id\']);\r\n    \$sended++;\r\n}\r\nif (!isset(\$_REQUEST[\'sel\']) || \$sended == count(\$_REQUEST[\'sel\'])) {\r\n    if (!isset(\$_REQUEST[\'sel\'])) {\r\n        send_all_msg(\$thread{\$send_tpl});\r\n    }\r\n    \$_SESSION[\$ses_id][\'send_mode\'] = \'msg\';\r\n    \$_SESSION[\$ses_id][\'send_complit\'] = 1;\r\n}', '{\r\n    \"title\": \"Отправить шаблон мессенджера\",\r\n    \"help\": \"tpl_send_msg\",\r\n    \"parameters\": {\r\n        \"send_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"send_tpl\",\r\n            \"description\": \"Шаблон мессенджера\",\r\n            \"tpl_type\": \"msg\",\r\n            \"help\": \"tpl_send_msg_template\"\r\n        }\r\n    }\r\n}', '', 'tpl_send.tpl', '', '4', '[1]', '3')");
