<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shop extends Score_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('score/shopInfo_model', 'info');
    }
    /**
     * [index 开通首页显示]
     * @return [type] [description]
     */
    public function index()
    {
        $result = $this->info->getShopInfo();
        if(!empty($result))
        {
            redirect('score/shop/info');
            exit;
        }
        $this->load->view('admin/score/index',$data);
    }
    /**
     * [addShop 商城开通显示页面]
     */
    public function add($id=0)
    {

        if($id>0)
        {
           $data = $this->info->getShopInfo();
        }

        $this->load->helper('discuz_helper');
        $extcredits = get_extcredits();
        $data['extcredits'] = $extcredits;

        $this->load->view('common/innerheader');
        $this->load->view('admin/score/addShopInfo',$data);
        $this->load->view('common/footer');
    }
    /**
     * [info 商城显示页面]
     */
    public function info()
    {
        $data = $this->info->getShopInfo();
        $this->load->helper('discuz_helper');
        $extcredits = get_extcredits();
        $data['score_name'] = $extcredits[$data['type']]['title'];

        $this->load->view('common/innerheader');
        $this->load->view('admin/score/shopInfo',$data);
    }
    /**
     * [save 商户添加]
     * @param  integer $id [商户ID]
     * @return [type]      [description]
     */
    public function  save($id=0)
    {
        $rules_config = array(
            array(
                'field' => 'name',
                'label' => '联系人',
                'rules' => 'trim|required|min_length[2]|max_length[5]|regex_match[/^([\x{4e00}-\x{9fa5}]|\w|-){0,6}$/u]'
            ),
            array(
                'field' => 'tel',
                'label' => '电话',
                'rules' => 'trim|required|regex_match[/^([0-9]{3,4}-)?[0-9]{7,8}$/]'
            ),
            array(
                'field' => 'phone',
                'label' => '手机',
                'rules' => 'trim|required|regex_match[/^1[34578]\d{9}$/]'
            ),
            array(
                'field' => 'qq',
                'label' => 'qq',
                'rules' => 'trim|numeric'
            ),
            array(
                'field' => 'addr',
                'label' => '地址',
                'rules' => 'trim|required|max_length[20]'
            ),
            array(
                'field' => 'type',
                'label' => '兑换积分分类',
                'rules' => 'trim|required'                
            )
        );
        $this->load->library('form_validation', $rules_config);
        if($this->form_validation->run() == FALSE)
        {
            print_r($this->form_validation->error_array());
        }
        else
        {
            $shopInfo_id = $id ? $this->info->update($_POST, $id) : $this->info->insert($_POST);
        }
        redirect('score/shop/info');
    }
}