<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\matchmaker\controller;

use app\matchmaker\model\MatchmakerModel;
use app\matchmaker\model\MatchmakerUserModel;
use cmf\controller\AdminBaseController;
use think\db\Query;

class AdminMatchmakerController extends AdminBaseController
{

    /**
     *  红娘管理
     */
    public function index()
    {
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $list = MatchmakerModel::where(function (Query $query) use ($name, $mobile) {
                if ($name) {
                    $query->where('name', 'like', "%$name%");
                }

                if ($mobile) {
                    $query->where('mobile', 'like', "%$mobile%");
                }
            })
            ->order("id DESC")
            ->paginate(10);
        $list->appends(['name' => $name, 'mobile' => $mobile]);
        // 获取分页显示
        $page = $list->render();

        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加提交
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];
            $post['password'] = trim($post['password']);

            MatchmakerModel::create($post);

            $this->success('添加成功!', url('index'));
        }

    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        $post = MatchmakerModel::get($id);

        $this->assign('post', $post);
        return $this->fetch();
    }

    /**
     * 编辑提交
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];
            $post['password'] = trim($post['password']);

            if (empty($post['password'])) {
                unset($post['password']);
            }

            MatchmakerModel::update($post, ['id' => $post['id']]);

            $this->success('编辑成功!', url('index'));
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('参数错误');
        }

        $check = MatchmakerUserModel::where('matchmaker_id',$id)->find();
        if (!empty($check)) {
            $this->error('该红娘下还有人员,无法删除');
        }

        MatchmakerModel::destroy($id);

        $this->success('删除成功!');
    }
}