<?php

namespace app\admin\controller;

use think\Lang;
use think\Validate;

class Message extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/message.lang.php');
    }

    /**
     * 留言管理
     */
    public function index()
    {
        $model_message = Model('message');
        if(request()->isPost()){
            $lyaddtime = $_POST['lyaddtime'];
        }else{
            $lyaddtime = 0;
        }

        if($lyaddtime==1){
            $kstime = strtotime(date("Y-m-d 23:59:59",strtotime("-3 day")));
        }elseif($lyaddtime==2){
            $kstime = strtotime(date("Y-m-d 23:59:59",strtotime("-1 week")));
        }elseif($lyaddtime==3){
            $kstime = strtotime(date("Y-m-d",strtotime("-1 month")));
        }else{
            $kstime = "1546272000";
        }
        $endtime = TIMESTAMP;

        if (input('post.kstime')) {
            $kstime = strtotime(input('post.kstime'));
        }
        if (input('post.endtime')) {
            $endtime = strtotime(input('post.endtime'));
        }

        $readok = input('post.message_readok');
        $sid = input('post.message_sid');
        $condition = array();
        $condition['message_addtime'] = array('between', array($kstime, $endtime));
        if($readok){
            $condition['message_readok'] = $readok;
        }
        if($sid){
            $condition['message_sid'] = $sid;
        }
        $message_list = $model_message->getMessageList($condition, '*', 10);
        if($message_list){
            for ($i=0; $i < count($message_list); $i++) { 
                $where['member_id'] = $message_list[$i]["admin_id"];
                $admin_list[] = model('member')->getMemberInfo($where,$field = 'member_name');
            }
            $this->assign('admin_list',$admin_list);
        }
        $allgs = Model('member')->allcompany('');
        $allpower = $this->qxhans();
        $this->assign('allgs',$allgs);
        $this->assign('allpower',$allpower);
        $this->assign('message_list', $message_list);
        $this->assign('lyaddtime',$lyaddtime);
        $this->assign('endtime',$endtime);
        $this->assign('kstime',$kstime);
        $this->assign('show_page', $model_message->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 留言回复
     */
    public function reply(){
        $message_id = intval(input('param.message_id'));
        if (!request()->isPost()) {
            $message = model('message')->getOneMessage(['message_id' => $message_id]);
            $this->assign('message', $message);
            $this->setAdminCurItem('reply');
            return $this->fetch('form');
        } else {
            $data = array(
                'admin_id' => $this->admin_info['admin_id'],
                'message_useinfo' => input('post.message_useinfo'),
                'message_result' => input('post.message_result'),
                'message_readok' => 1,
                'message_replytime' => TIMESTAMP,
            );

            $result = model('message')->editMessage(['message_id' => $message_id], $data);
            if ($result) {
                $this->log(lang('ds_message').'-'.lang('edit_succ') . '[' . $data['message_useinfo'] . ']', null);
                dsLayerOpenSuccess(lang('edit_succ'));
            } else {
                $this->error(lang('edit_fail'));
            }
        }
    }

    /**
     * 删除留言
     */
    public function del()
    {
        $message_id = intval(input('param.message_id'));
        if ($message_id) {
            $condition['message_id'] = $message_id;
            $result = model('message')->delMessage($condition);
            if ($result) {
                $this->log(lang('ds_message').'-'.lang('del_succ') . '[' . $message_id . ']', null);
                ds_json_encode(10000, lang('del_succ'));
            } else {
                ds_json_encode(10001, lang('del_fail'));
            }
        } else {
            ds_json_encode(10001, lang('del_fail'));
        }

    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('message/index'),
            )
        );
        return $menu_array;
    }

}

?>
