<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\BatchApi;

/**
 * Description of Batch
 *
 * @author sgq
 */
class Batch extends AdminController {

    /**
     * @auth {{/batch}}
     * @return type
     */
    public function index() {
        $declare_type_list = \app\common\api\DictApi::findChildDictByCode("declare_type");
        return view("", ["declare_type_list" => $declare_type_list]);
    }

    /**
     * @auth {{/batch/list}}
     * @return type
     */
    public function list() {
        return json(BatchApi::getList($this->request));
    }

    /**
     * @auth {{/batch/add}}
     * @return type
     */
    public function add() {
        if ($this->request->isPost()) {
            return json(BatchApi::create($this->request->param()));
        }
        return view("", ["source" => $this->user["type"]]);
    }

    /**
     * @auth {{/batch/update}}
     * @return type
     */
    public function edit() {
        if ($this->request->isPost()) {
            return json(BatchApi::update($this->request->param()));
        }
        $batch = BatchApi::getOne($this->request->param("id"));
        return view("", ["batch" => $batch]);
    }

    /**
     * @auth {{/batch/delete}}
     * @return type
     */
    public function delete() {
        if ($this->request->isPost()) {
            return json(BatchApi::delete($this->request->param("batchId")));
        }
    }

    /**
     * @auth {{/batch/setActive}}
     * @return type
     */
    public function setActive() {
        if ($this->request->isPost()) {
            return json(BatchApi::setActive($this->request->param("batchId"), 1));
        }
    }

    /**
     * @auth {{/batch/setNotActive}}
     * @return type
     */
    public function setNotActive() {
        if ($this->request->isPost()) {
            return json(BatchApi::setActive($this->request->param("batchId"), 2));
        }
    }

    /**
     * @auth {{/batch/detail}}
     * @return type
     */
    public function detail() {

        return view();
    }

    public function listBatchByType() {
        $type = $this->request["type"];
        $talentType = $this->user["type"];
        return json(BatchApi::getValidBatchs($type, $talentType));
    }

}
