<?php


namespace app\admin\controller;


use app\admin\logic\AccountLogic;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Validate;

class Account extends BaseController
{
    //账户列表
    public function list()
    {

        $param = $this->request->only(['page' => 1, 'size' => 10, 'username' => '', 'name' => '', 'mobile' => ''], 'post');

        return AccountLogic::list($param);

    }

    //添加账户
    public function add()
    {
        $param = $this->request->only(['company_id', 'card_id', 'username', 'starttime', 'expiretime', 'video_ids', 'mobile' => '', 'name' => '', 'remark' => ''], 'post');

        $val = Validate::rule(Config::get('validate_rules.AccountAdd'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return AccountLogic::add($param);
    }

    //账户详情
    public function read()
    {
        $id = $this->request->post('id/d', 0);
        return AccountLogic::read($id);
    }

    //编辑账户
    public function edit()
    {
        $param = $this->request->only(['id', 'starttime', 'expiretime', 'video_ids', 'mobile' => '', 'name' => '', 'remark' => ''], 'post');

        $val = Validate::rule([
            'id' => 'require|number|gt:0',
            'starttime|开始日期' => 'require|date|lt:expiretime',
            'expiretime|结束日期' => 'require|date|gt:starttime',
            'video_ids|视频id集合' => 'require|array|max:100',
            'mobile|手机号' => 'mobile',
            'name|姓名' => 'max:255',
            'remark|备注' => 'max:255',
        ]);

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return AccountLogic::edit($param);
    }

    //批量添加账户
    public function batchAdd()
    {
        $param = $this->request->only(['company_id', 'card_id', 'username_prefix', 'username_year', 'starttime', 'expiretime'], 'post');

        $val = Validate::rule(Config::get('validate_rules.AccountBatchAdd'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return AccountLogic::batchAdd($param);
    }

    //删除
    public function delete()
    {
        $id = $this->request->post('id/d', 0);
        return AccountLogic::delete($id);
    }

    //批量添加账户列表
    public function batchLog()
    {
        $param = $this->request->only(['page' => 1, 'size' => 10, 'company_title' => '', 'card_title' => '', 'status' => ''], 'post');

        return AccountLogic::batchLog($param);
    }

    //修改密码
    public function changePasswod(){
        $param = $this->request->only(['id', 'new_password'], 'post');

        $val = Validate::rule(Config::get('validate_rules.adminChangePasswod'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return AccountLogic::changePasswod($param);
    }

}