<?php

namespace app\admin\controller;

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

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

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

    /**
     * @auth {{/unCommonFiletype}}
     * @return type
     */
    function list() {
        $result = FileTypeApi::getList($this->request->param());
        return json($result);
    }

    /**
     * @auth {{/unCommonFiletype/add}}
     * @return type
     */
    function add() {
        if ($this->request->isPost()) {
            FileTypeApi::edit($this->request);
            $res = ["code" => 200, "msg" => "添加附件类型成功"];
            echo sprintf("<script>parent.UnCommonFiletypeInfoDlg.callBack(%s);</script>", json_encode($res));
            exit();
        }
        $declare_types = \app\common\api\DictApi::findChildDictByCode("declare_type");
        return view("", ["declare_types" => $declare_types]);
    }

    /**
     * @auth {{/unCommonFiletype/update}}
     * @return type
     */
    function edit() {
        if ($this->request->isPost()) {
            FileTypeApi::edit($this->request);
            $res = ["code" => 200, "msg" => "编辑附件类型成功"];
            echo sprintf("<script>parent.UnCommonFiletypeInfoDlg.callBack(%s);</script>", json_encode($res));
            exit();
        }
        $id = $this->request->param("id");
        $info = FileTypeApi::getOne($id);
        $declare_types = \app\common\api\DictApi::findChildDictByCode("declare_type");
        return view("", ["info" => $info, "declare_types" => $declare_types]);
    }

    /**
     * @auth {{/unCommonFiletype/delete}}
     * @return type
     */
    function delete() {
        $id = $this->request->param("id");
        FileTypeApi::delete($id);
        return json(["code" => 200, "msg" => "删除成功"]);
    }

}
