<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Index extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->permission->is_admin();
        $this->load->model('form_model');
    }

    //表单列表
    public function index() {
        $data = array();
        $form_web = $this->config->item('site_code');
        $data['form_list'] = $this->form_model->get_froms(FALSE, $form_web);
        $this->load->view('bootstrap3/header', $data);
        $this->load->view('index');
        $this->load->view('bootstrap3/footer');
    }

    public function create_form($form_sn = false, $selected_type = 'ContactUs') {
        $data = array();
        $form_web = $this->config->item('site_code');
        $data['form_list'] = $this->form_model->get_froms(FALSE, $form_web);
        $data['form_type'] = $selected_type;
        if (!empty($form_sn)) {
            $data['form_detail'] = $this->form_model->get_froms($form_sn, $form_web);
        }

        $this->load->view('bootstrap3/header', $data);
        $this->load->view('form_builder');
        $this->load->view('bootstrap3/footer');
    }

    public function test($form_sn) {
        $data = array();
        $data['form_data'] = $this->form_model->get_froms($form_sn);
        $this->load->view('test', $data);
    }

    //表单入库
    public function do_create_form() {
        if (!$this->session->userdata('session_admin')) {
            $data[] = array("name" => "no", "value" => "请先登录信息平台系统！");
            echo json_encode($data);
            return false;
        }
        $session_admin = $this->session->userdata('session_admin');
        $form_sn = $this->input->post('form_sn');
        $form_name = $this->input->post('form_name');
        $form_structure = $this->input->post('form_structure'); //trim(htmlspecialchars($this->input->post('form_structure')));
        $form_web_structure = $this->input->post('form_web_structure');
        $form_type = $this->input->post('form_type');
        //设置表单类型
        $form_web_structure = str_ireplace('@##actionurl##@', 'https://info.chinahighlights.com/info.php/thirdparty/form/call/save_order/' . $form_type, $form_web_structure);
        $form_web_structure = trim(htmlspecialchars(str_ireplace(array('<legend>', '</legend>'), array('<!-- ', ' -->'), $form_web_structure)));
        $form_status = 1;
        $form_user = $session_admin['OPI_SN'];
        $form_web = $this->config->item('site_code');
        if (empty($form_sn)) {
            $form_sn = $this->form_model->add($form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type);
        } else {
            $this->form_model->update($form_sn, $form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type);
        }
        $data[] = array("name" => "ok_go", "form_sn" => $form_sn, "value" => site_url('thirdparty/form/index/create_form/' . $form_sn));
        echo json_encode($data);
        redirect(site_url('thirdparty/form/index/create_form/' . $form_sn));
    }

    public function stop($form_sn, $form_status) {
        $this->form_model->stop($form_sn, $form_status);
        $data[] = array("name" => "ok_go", "value" => site_url('thirdparty/form/index'));
        echo json_encode($data);
    }

    public function delete($form_sn) {
        $this->form_model->delete($form_sn);
        redirect(site_url('thirdparty/form/index/'));
    }

}
