<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;

class Member extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/member.lang.php');
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function index()
    {
        $model_member = Model('member');
        $title = input('post.title');
        $time = input('post.timeRang');
        if(!empty($time)){
            $gap = explode(' - ', $time);
            $begin = strtotime($gap[0]);
            $end = strtotime($gap[1]);
        }
        $condition = array();
        if($title){
            $condition['member_contacts|member_mobile|member_name'] = $title;
        }
        if($time && $begin && $end){
            $condition['member_add_time'] = array('between', array($begin, $end));
        }
        $member_list = $model_member->getMemberList($condition, '*', 10);
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('member_list', $member_list);
        $this->assign('show_page', $model_member->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        if (request()->isPost()) {
            $model_member = Model('member');
            //判断用户名是否存在
            $usetime = input('post.member_usetime');
            $validitytime = $model_member->validity($usetime);
            $data = array(
                'member_name' => input('post.member_name'),
                'member_url' =>input('post.member_url'),
                'member_mobile' => input('post.member_mobile'),
                'member_contacts' =>input('post.member_contacts'),
                'member_mailbox' => input('post.member_mailbox'),
                'member_usetime' => $usetime,
                'member_validitytime' => $validitytime,
                'member_status' => input('post.member_status') ? input('post.member_status') : 1,
                'member_add_time' => TIMESTAMP
            );
            //添加到数据库
            $result = $model_member->addMember($data);

            if ($result) {
                $cjglz =array('wlcome','wlcome.index','config','config.index','config.add','config.edit','config.del','admin','admin.index','admin.add','admin.edit','admin.del','admingroup','admingroup.index','admingroup.add','admingroup.edit','admingroup.del','product','product.index','product.add','product.edit','product.del','news','news.index','news.add','news.edit','news.del','message','message.index','message.reply','message.del','service','service.index','Visitkf','Visitkf.index','glservice','glservice.index');
                $limit_str = '';
                if (is_array($cjglz)) {
                    $limit_str = implode('|', $cjglz);
                }
                $sgda = array(
                    'group_name' => '超级管理员',
                    'group_limits' => ds_encrypt($limit_str, MD5_KEY . md5('超级管理员')),
                    'lang' => 'zh-cn',
                    'suosgs' => $result,
                    'addtime' => TIMESTAMP
                );
                $groupid = Model('Admingroup')->addAdminGroup($sgda);
                if($groupid){
                    //创建一个客服帐号
                    $erda = array(
                        'service_name' => input('post.member_mailbox'),
                        'service_password' => md5('123456'),
                        'service_group_id' => $groupid,
                        'service_is_super' => 1,
                        'service_pid' => $result,
                        'service_add_time' => TIMESTAMP
                    );
                    Model('service')->addservice($erda);
                    dsLayerOpenSuccess(lang('member_add_succ'));
                }
            } else {
                $this->error(lang('member_add_fail'));
            }
        } else {
            $member_array = array(
                'member_status' => 0,
                'add' => 1,
            );
            $this->assign('member', $member_array);
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $member_id = input('param.member_id');
        if (empty($member_id)) {
            $this->error(lang('param_error'));
        }
        $model_member = Model('member');
        $usetime = input('post.member_usetime');
        $validitytime = $model_member->validity($usetime);
        if (!request()->isPost()) {
            $condition['member_id'] = $member_id;
            $member_array = $model_member->getMemberInfo($condition);
            $member_array['add'] = 0;
            $this->assign('member', $member_array);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        } else {
            $data = array(
                'member_name' => input('post.member_name'),
                'member_url' => input('post.member_url'),
                'member_mobile' => input('post.member_mobile'),
                'member_contacts' => input('post.member_contacts'),
                'member_mailbox' => input('post.member_mailbox'),
                'member_usetime' => $usetime,
                'member_validitytime' => $validitytime,
                'member_status' => input('post.member_status'),
            );
            //验证数据  END
            $result = $model_member->editMember(array('member_id' => intval($member_id)), $data);
            if ($result) {
                dsLayerOpenSuccess(lang('member_edit_succ'));
            } else {
                $this->error(lang('member_edit_fail'));
            }
        }
    }

    public function del()
    {
        $member_id = input('param.member_id');
        if (empty($member_id)) {
            $this->error(lang('param_error'));
        }
        $result = db('member')->delete($member_id);
        if ($result) {
            ds_json_encode(10000, lang('member_del_succ'));
        } else {
            ds_json_encode(10001, lang('member_del_fail'));
        }
    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('Member/index')
            ), array(
                'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('".url('Member/add')."','".lang('ds_add')."')"
            ),
        );
        return $menu_array;
    }

}
