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

class Area extends Admin_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('area_model', 'area');
    }

    public function index()
    {
        $data['info'] = $this->area->area_list();
		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/district',$data);
    }

    public function add()
    {
        $rules_config = array(
            array(
                'field' => 'district[]',
                'label' => '区域/商圈',
                'rules' => 'trim'
            )
        );
        $this->load->library('form_validation', $rules_config);  
        if($this->form_validation->run() == FALSE)
        {
            print_r($this->form_validation->error_array());exit;
        }
        else
        {
            $areas = $this->input->post('district');
            if($this->area->area_count() == 0 && count(array_filter($areas)) == 0)
            {
                print_r('信息不能为空');exit; 
            }
            $area = array_filter($areas);
            foreach ($area as $k=>$v) {
                if( ! preg_match("/^([\x{4e00}-\x{9fa5}]|\w|-){0,6}$/u",$v))
                {
                    print_r($v.' 名称格式 不正确');exit; 
                } 
                if($this->area->checkdata($k))
                {
                    $this->area->area_update($k,$v);
                }
                else
                {
                    $this->area->area_insert($k,$v);
                }
               
            }
            redirect('admin/area/index');
        }
    }
    public function del()
    {
        $this->area->area_del($this->input->post('id'));
        $this->json(0,'');
    }


}