<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\controller;
use think\Validate;
use think\Db;
class Email extends Base
{
	public function test(){
  
       $test_email = input('post.test_email');

		$rule = [
		    'email' => 'email',
		];

		$msg = [
		    'email'        => '邮箱格式错误',
		];

		$data = [
		    'email' => $test_email,
		];

		$validate   = Validate::make($rule,$msg);
		$result = $validate->check($data);
		if(!$result) {
		   return error_json($validate->getError());
		}

       if(empty($test_email)){
         	return error_json('测试邮箱不能为空');
       }
       $result = send_email($test_email,'网站测试邮件','这是测试邮件,当你收到这封邮件的时候，说明你的邮箱已经配置成功了');
	   if($result == true){
           return success_json('发送成功');
	   }else{
	       return error_json($result);
	   }
	}


	public function index(){
      $alias        = '';  //需要别名时写别名，不需要的时候留空 
      $defaultWhere = '';   //默认where条件
      $defaultOrder = 'create_time desc';  //默认排序
//接收搜索表单数据
      $SearchFormData = $this->getSearchFormData($defaultWhere,$defaultOrder);
      $where          = $SearchFormData['where'];    //返回的条件sql
      $order          = $SearchFormData['order'];    //返回排序sql
      $pageNum        = $SearchFormData['pageNum']; //返回的分页数量
//组建关键词数据
     $keywordsData  =[
                  [
                  'field'   =>'id', //字段名
                  'name'    =>'模板ID', //文本显示名称
                  'alias'   => $alias,  //字段对应的别名
                  ],
                  [
                  'field'   =>'name',
                  'name'    =>'模板名称',
                  'alias'   => $alias,
                  ],
                  [
                  'field'   =>'use_name',
                  'name'    =>'调用名称',
                  'alias'   => $alias,
                  ]
     ];
//组建日期筛选   
       $dateData = [];
//单个字段筛选
    $customSingleField =[
                         [
                          'name'  => '审核状态',
                          'field' => 'status', 
                          'data'  => [ 0 =>'禁用',1=>'启用'],
                          'alias' => $alias,
                         ]
                       ]; 
// 组件字段组筛选
    $groupData = [];
     $orderData   = [];  

     $this->assign('keywordsData',$keywordsData);
     $this->assign('customSingleField',$customSingleField);
     $this->assign('groupData',$groupData);
     $this->assign('dateData',$dateData);
     $this->assign('orderData',$orderData);


     $list = DB::name('email')->where($where)->order($order)->paginate($pageNum);
     $page = $list->render();
     $this->assign('page', $page);
     $this->assign('list', $list->toArray()['data']);
     return $this->fetch();
  }
//增加
  public function add(){
    $param = $this->request->param(); 
    if($this->request->isPost()){   

        //验证
        $rule =[
          'tpl_name'=>'require',
          'use_name'=>'require',
          'content'=>'require'
        ];
        $msg= [
         'tpl_name.require' =>'模板名称不能为空',
         'use_name.require' =>'调用名称不能为空',
         'content.require' =>'内容名称不能为空',    
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
              return error_json($error);
         }
         //表单数据
          $data['tpl_name'] = $param['tpl_name'];
          $data['use_name'] = $param['use_name'];
          $data['content'] = $param['content'];
          $data['create_time'] = time();
          $data['status'] = !empty($param['status'])?1:0;

       if(model('email')->addData($data)){
         return success_json('添加成功'); 
       }
   }else{
        return $this->fetch();
   }      
  }
  public function edit(){
    $param = $this->request->param();
    if($this->request->isPost()){

        //验证
        $rule =[
          'tpl_name'=>'require',
          'use_name'=>'require',
          'content'=>'require'
        ];
        $msg= [
         'tpl_name.require' =>'模板名称不能为空',
         'use_name.require' =>'调用名称不能为空',
         'content.require' =>'内容名称不能为空',    
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
              return error_json($error);
         }
    
         //表单数据
          $data['tpl_name'] = $param['tpl_name'];
          $data['use_name'] = $param['use_name'];
          $data['content'] = $param['content'];
          $data['status'] = !empty($param['status'])?1:0;
          $where['id'] = $param['id'];
          $res = model('email')->editData($data,$where);
          echo model('email')->getLastSql();
          return success_json('编辑成功'); 
    }else{
         $email = model('email')->getEmailById($param['id']);
         $this->assign('email',$email);
         return $this->fetch();


    }  
  }
  public function del(){
      $param =$this->request->param();
     if(!$param['id']){  return error_json('请求参数错误');    }

     $res= model('email')->delData("id",$param['id']);
       if($res){
         return success_json('删除成功');
       }else{
         return error_json($res);
       }
    }
     





}