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

class Members extends WB_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->theme    = "admin";
    }

    public function login(){
        if( $this->member->is_login() ) {
            alert(langs('회원/login/already'), base_url("members/info"));
            exit;
        }

        $form_attributes['id'] = "form-login";
        $form_attributes['autocomplete'] = "off";
        $form_attributes['name'] = "form_login";
        $form_attributes['data-role'] = "form-login";
        $form_hidden_inputs['reurl'] = set_value('reurl', $this->input->get("reurl", TRUE, base_url()));

        $action_url = base_url( 'admin/members/login', SSL_VERFIY ? 'https' : 'http' );
        $this->data['form_open'] = form_open($action_url, $form_attributes, $form_hidden_inputs);
        $this->data['form_close'] = form_close();

        $this->site->meta_title = "관리자 로그인";
        $this->theme = "admin";
        $this->theme_file = "iframe";
        $this->view = "members/login";
    }

    /*****************************************************************
     * 회원 목록
     *****************************************************************/
    public function lists()
    {
        $this->load->model('member_model');

        $this->data['sdate'] = $this->input->get('sdate', TRUE);
        $this->data['startdate'] = $this->input->get('startdate', TRUE);
        $this->data['enddate'] = $this->input->get('enddate', TRUE);

        if( $this->data['sdate'] && $this->data['startdate'] ) $param['where']['mem_' . $this->data['sdate'] . ' >=' ] = $this->data['startdate'] . " 00:00:00";
        if( $this->data['sdate'] && $this->data['enddate'] ) $param['where']['mem_' . $this->data['sdate'] . ' <=' ] = $this->data['enddate'] . " 23:59:59";

        // 메타태그 설정
        $this->site->meta_title = "회원 목록";            // 이 페이지의 타이틀

        // 레이아웃 & 뷰파일 설정
        $this->view     = $this->active = "members/lists";
    }

    /**
     * @param $mem_idx
     */
    public function info($mem_idx)
    {
        if(empty($mem_idx))
        {
            alert_modal_close('잘못된 접근입니다.');
            exit;
        }

        $this->data['mem'] = $this->member->get_member($mem_idx,'mem_idx');

        $this->theme    = "admin";
        $this->theme_file = "iframe";
        $this->view     = "members/info";
    }

    /**
     * 회원 포인트 관리
     * @param $mem_idx
     */
    public function point($mem_idx)
    {
        if(empty($mem_idx))
        {
            alert_modal_close('잘못된 접근입니다.');
            exit;
        }

        $this->load->model('member_model');

        // 회원 정보
        $this->data['mem'] = $this->member->get_member($mem_idx,'mem_idx');

        $this->theme    = "admin";
        $this->theme_file = "iframe";
        $this->view     = "members/point";
    }

    /**
     * 회원 포인트 추가 폼
     * @param $mem_idx
     */
    public function point_form($mem_idx)
    {
        if(empty($mem_idx))
        {
            alert_modal2_close('잘못된 접근입니다.');
            exit;
        }

        $this->data['mem'] = $this->member->get_member($mem_idx,'mem_idx');

        $this->load->library('form_validation');

        $this->form_validation->set_rules('mem_idx', '회원번호', 'required|trim');
        $this->form_validation->set_rules('mpo_value', $this->site->config('point_name'), 'required|trim|numeric');
        $this->form_validation->set_rules('mpo_description', $this->site->config('point_name').' 내용', 'required|trim');

        if( $this->form_validation->run() != FALSE )
        {
            $data['mem_idx'] = $this->input->post('mem_idx', TRUE);
            $data['mpo_flag'] = $this->input->post('mpo_flag', TRUE, 1);
            $data['mpo_value'] = $this->input->post('mpo_value', TRUE);
            $data['mpo_description'] = $this->input->post('mpo_description', TRUE);
            $data['target_type'] = $this->input->post('target_type', TRUE);
            $data['mpo_regtime'] = date('Y-m-d H:i:s');

            if( $this->member->add_point($data['mem_idx'],$data['mpo_flag'], $data['mpo_value'], FALSE, $data['target_type'], $data['mpo_description'],0))
            {
                alert_modal2_close('등록완료');
                exit;
            }
            else {
                alert('DB 입력도중 오류가 발생하였습니다.');
                exit;
            }
        }
        else
        {
            $this->data['mem_idx'] = $mem_idx;

            $this->theme    = "admin";
            $this->theme_file = "iframe";
            $this->view     = "members/point_form";
        }
    }


    /**
     * 포인트 관리
     */
    function points()
    {
        // 메타태그 설정
        $this->site->meta_title = $this->site->config('point_name'). " 관리";

        // 레이아웃 & 뷰파일 설정
        $this->theme    = "admin";
        $this->view     = "members/points";
        $this->active   = "members/points";
    }

    /*****************************************************************
     * 회원 추가
     ****************************************************************/
    public function add()
    {
        $this->load->model('member_model');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('mem_userid', "아이디", "required|trim|min_length[6]" . (USE_EMAIL_ID ? '|valid_email' :'') . '|callback_userid_check' );
        $this->form_validation->set_rules('mem_password', '비밀번호', 'required|trim|min_length[6]');
        $this->form_validation->set_rules('mem_password2', "비밀번호 확인", "required|trim|matches[mem_password]");
        $this->form_validation->set_rules('mem_nickname', "닉네임", "required|trim|callback_nickname_check");
        $this->form_validation->set_rules('mem_email', '이메일', 'required|trim|valid_email');

        if( $this->form_validation->run() != FALSE)
        {
            $data['mode']           = "INSERT";
            $data['mem_userid']     = $this->input->post('mem_userid', TRUE);
            $data['mem_password']   = $this->input->post('mem_password', TRUE);
            $data['mem_nickname']   = $this->input->post('mem_nickname', TRUE);
            $data['mem_email']      = $this->input->post('mem_email', TRUE);
            $data['mem_verfy_email'] = ( USE_EMAIL_VERFY ) ?  ( $this->input->post('mem_verfy_email', TRUE) == 'Y' ? 'Y' : 'N' ) : 'Y';
            $data['mem_phone']      = $this->input->post('mem_phone', TRUE);
            $data['mem_auth']       = $this->input->post('mem_auth', TRUE);
            $data['mem_gender']     = $this->input->post('mem_gender', TRUE);
            $data['mem_recv_email'] = $this->input->post('mem_recv_email', TRUE) == 'Y' ? 'Y' : 'N';
            $data['mem_recv_sms']   = $this->input->post('mem_recv_sms', TRUE) == 'Y' ? 'Y' : 'N';

            $data['mem_password'] = get_password_hash($data['mem_password']);

            if( $this->member->info_process($data) )
            {
                alert('사용자 등록이 완료되었습니다.', base_url('admin/members/lists'));
                exit;
            }
            else {
                alert('등록도중 오류가 발생하였습니다.');
                exit;
            }
        }
        else
        {
            // 메타태그 설정
            $this->site->meta_title = "신규 회원 등록";            // 이 페이지의 타이틀

            // 레이아웃 & 뷰파일 설정
            $this->theme    = "admin";
            $this->view     = "members/add";
            $this->active   = "members/add";
        }
    }

    /*****************************************************************
     * 폼검증 : 회원 아이디 체크
     ****************************************************************/
    public function userid_check($str)
    {
        $deny_id = explode(',', $this->site->config('deny_id'));
        if( in_array($str, $deny_id) )
        {
            $this->form_validation->set_message('userid_check', "{field}에 사용할 수 없는 단어입니다 : {$str}");
            return FALSE;
        }
        if( $member = $this->member->get_member($str, 'mem_userid') )
        {
            $this->form_validation->set_message('userid_check', "이미 사용중인 {field}입니다 : {$str}");
            return FALSE;
        }

        return true;
    }

    /*****************************************************************
     * 폼검증 : 회원 닉네임 체크
     ****************************************************************/
    public function nickname_check_pre($str)
    {
        $deny_nickname = explode(',',$this->site->config('deny_nickname'));
        $deny_word = explode(',', $this->site->config('deny_word'));

        $deny = array();
        foreach($deny_nickname as $d) $deny[] = trim($d);
        foreach($deny_word as $d) $deny[] = trim($d);

        if ( in_array($str, $deny) )
        {
            $this->form_validation->set_message('nickname_check_pre', "{field}에 사용할 수 없는 단어입니다 : {$str}");
            return FALSE;
        }

        return TRUE;
    }

    /*****************************************************************
     * 폼검증 : 회원 닉네임 체크 + 사용여부 체크
     ****************************************************************/
    public function nickname_check($str)
    {
        if(! $this->nickname_check_pre($str) )
        {
            return FALSE;
        }

        if( $member = $this->member->get_member($str, 'mem_nickname') )
        {
            $this->form_validation->set_message('nickname_check', "이미 사용중인 {field} 입니다 : {$str}");
            return FALSE;
        }

        return TRUE;
    }

    /**
     * 사용자 로그인 로그
     */
    public function log()
    {
        $this->data['st'] = $this->input->get('st', TRUE);
        $this->data['sc'] = $this->input->get('sc', TRUE);

        $popup_mode = strtolower($this->input->get('mode', TRUE)) == 'popup';

        // 메타태그 설정
        $this->site->meta_title = "회원 로그인 기록";            // 이 페이지의 타이틀

        // 레이아웃 & 뷰파일 설정
        $this->view     = "members/log";
        $this->active   = "members/log";

        if($popup_mode) {
            $this->theme_file = 'iframe';
        }
    }

    /**
     * 사용자 비밀번호 변경
     */
    public function password($mem_idx)
    {

        if(empty($mem_idx))
        {
            alert_modal_close('잘못된 접근입니다.');
            exit;
        }

        $this->data['mem'] = $this->member->get_member($mem_idx,'mem_idx');

        $this->load->library('form_validation');

        $this->form_validation->set_rules('mem_password', '새 비밀번호', 'required|trim|min_length[6]');
        $this->form_validation->set_rules('mem_password2', "새 비밀번호 확인", "required|trim|matches[mem_password]");

        if( $this->form_validation->run() != FALSE)
        {
            $data['mem_password']   = $this->input->post('mem_password', TRUE);
            $data['mem_password'] = get_password_hash($data['mem_password']);

            if( $this->db->where('mem_idx', $mem_idx)->set('mem_password', $data['mem_password'])->update('member') )
            {
                alert_modal_close('사용자의 비밀번호가 변경되었습니다.');
                exit;
            }
            else {
                alert('비밀번호 변경도중 오류가 발생하였습니다.');
                exit;
            }
        }
        else
        {
            // 레이아웃 & 뷰파일 설정
            $this->theme    = "admin";
            $this->view     = "members/password";
            $this->theme_file   = "iframe";
        }
    }

    /**
     * 사용자 정보수정
     */
    public function modify($mem_idx)
    {
        if(empty($mem_idx))
        {
            alert_modal_close('잘못된 접근입니다.');
            exit;
        }

        if(! $this->data['mem'] = $this->member->get_member($mem_idx,'mem_idx'))
        {
            alert_modal_close('존재하지 않는 회원입니다.');
            exit;
        }

        $this->load->model('member_model');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('mem_nickname', "닉네임", "required|trim|callback_nickname_check_pre");
        $this->form_validation->set_rules('mem_email', '이메일', 'required|trim|valid_email');

        if( $this->form_validation->run() != FALSE)
        {
            $data['mode']           = "MODIFY";
            $data['mem_idx']        = $mem_idx;
            $data['mem_nickname']   = $this->input->post('mem_nickname', TRUE);
            $data['mem_email']      = $this->input->post('mem_email', TRUE);
            $data['mem_phone']      = $this->input->post('mem_phone', TRUE);
            $data['mem_auth']       = $this->input->post('mem_auth', TRUE);
            $data['mem_gender']     = $this->input->post('mem_gender', TRUE);
            $data['mem_recv_email'] = $this->input->post('mem_recv_email', TRUE) == 'Y' ? 'Y' : 'N';
            $data['mem_recv_sms']   = $this->input->post('mem_recv_sms', TRUE) == 'Y' ? 'Y' : 'N';

            if( $this->member->info_process($data) )
            {
                alert_modal_close('사용자 정보수정이 완료되었습니다.');
                exit;
            }
            else {
                alert('등록도중 오류가 발생하였습니다.');
                exit;
            }
        }
        else
        {
            // 메타태그 설정
            $this->site->meta_title = "신규 회원 등록";            // 이 페이지의 타이틀

            // 레이아웃 & 뷰파일 설정
            $this->theme    = "admin";
            $this->view     = "members/modify";
            $this->theme_file = "iframe";
        }
    }
}