<?php

/**
 *
 */

include_once(dirname(dirname(__FILE__)) . "/library/publicBase.php");
include_once(SERVER_ROOT . "/model/mPage.php");

class admin extends publicBase {

    public function login() {
        $name = trim($this->get('name'));
        $password = trim($this->get('password'));

        if (!empty($name) && !empty($password)) {
            $m_admin_user = new mAdminUser();
            $res = $m_admin_user->checkAdminLogin($name, $password);
            if (!$res) $this->show_message($m_admin_user->getError(), '/admin/login');

            header('Location: /');
        }
    }

    public function logout() {
        setcookie("uid", '', time() + 3600 * 24, '/');
        setcookie("token", '', time() + 3600 * 24, '/');

        header('Location: /admin/login');
    }

    public function user_list() {
        $m_admin_user = new mAdminUser();
        $list = $m_admin_user->getUserList();

        $this->view['list'] = $list;
    }

    public function user_add() {}

    public function ajax_save_user() {
        $name = trim($this->post('name'));
        $password = md5('123456');

        $m_admin_user = new mAdminUser();
        $id = $m_admin_user->createUser(array('username' => $name, 'password' => $password));
        if (!$id) $this->ajax_json(false, '添加失败');

        $this->ajax_json(true, '添加成功');
    }

    public function ajax_update_user() {
        $id = $this->post('id') + 0;
        $data = array(
            'status' => $this->post('status') + 0
        );

        $m_admin_user = new mAdminUser();
        $id = $m_admin_user->updateUser($id, $data);
        if (!$id) $this->ajax_json(false, '操作失败');

        $this->ajax_json(true, '操作成功');
    }

    public function save_pass() {
        $username = trim($this->get('username'));

        $m_admin_user = new mAdminUser();
        $user = $m_admin_user->getAdminUserByName($username);
        if (!$user) $this->show_message("没有此用户", '/admin/formula_list');

        $this->view['id'] = $user['id'];
    }

    public function ajax_save_pass() {
        $id = $this->post('id') + 0;
        $password = trim($this->post('password'));
        if (!$id || !$password) $this->ajax_json(false, '参数错误');

        $uinfo = $this->get_uinfo();
        if ($uinfo['id'] != $id) $this->ajax_json(false, '用户不存在');

        $m_admin_user = new mAdminUser();
        $user = $m_admin_user->getAdminUserById($id);
        if (!$user) $this->ajax_json(false, '用户不存在');

        $data = array(
            'password' => md5($password)
        );
        $id = $m_admin_user->updateUser($id, $data);
        if (!$id) $this->ajax_json(false, '操作失败');

        $this->ajax_json(true, '操作成功');
    }

    public function formula_list() {
        $is_all = $this->get('is_all') + 0;
        $status = $this->get('status') + 0;
        $name = trim($this->get('name'));

        $condition = array();
        $url = "/admin/formula_list/is_all/{$is_all}/status/{$status}";
        if ($name) {
            $condition['name'] = $name;
            $url .= "/name/{$name}";
        }
        if (!$is_all) {
            $condition['uid'] = 0;
        }
        if ($status == 0) {
            $condition['is_delete'] = array(0, 2, 3);
        } elseif ($status == 1) {
            $condition['is_delete'] = 2;
        } elseif ($status == 2) {
            $condition['is_delete'] = 0;
        }

        $mformula = new mFormula();
        $total = $mformula->getFormulaTotal($condition);

        // 分页
        $page = new Page();
        $page->setTotalnum($total);
        $page->setUrl($url . '/page/');
        $curpage = $this->get('page') > 0 ? $this->get('page') : 1;
        $page->setPage($curpage);

        $this->view['page_list'] = $page->getPageList();
        $this->view['curpage'] = $curpage;

        if ($curpage > 1) $this->view['prev_page'] = $page->url . ($curpage - 1); //上一页连接
        if ($curpage < $page->totalpage) $this->view['post_page'] = $page->url . ($curpage + 1); //下一页连接

        //只取出当前页显示
        $list = $mformula->getFormulaList($condition, $curpage, $page->pagesize);

        //管理员列表
        $m_admin_user = new mAdminUser();
        $user_list = $m_admin_user->getUserList();
        $user_list = array_column($user_list, null, 'id');

        $this->view['list'] = $list;
        $this->view['user_list'] = $user_list;
    }

    public function formula_add() {
        $id = $this->get('id');
        if ($id) {
            $mformula = new mFormula();
            $data = $mformula->getFormulaInfo($id, 0);

            $this->view['data'] = $data;
        }
    }

    public function ajax_save_formula() {
        $user_info = $this->get_uinfo();

        $id = $this->post('id') + 0;
        $name = trim($this->post('name'));
        $source = trim($this->post('source'));
        $method = trim($this->post('method'));
        $remark = trim($this->post('remark'));
        $herbs = $this->post('herbs');

        $mformula = new mFormula();
        $id = $mformula->saveFormula(0, $id, $name, $source, $method, $herbs, $remark, $user_info['id']);
        if (!$id) $this->ajax_json(false, $mformula->getError());

        $this->ajax_json(true, '操作成功', array('id' => $id));
    }

    public function ajax_shibie_original() {
        $original = trim($this->post('original'));

        $mformula = new mFormula();
        $data = $mformula->convOriginal($original);

        $this->ajax_json(true, '识别成功', $data);
    }

    public function ajax_update_formula() {
        $user_info = $this->get_uinfo();

        $id = $this->post('id') + 0;
        $is_delete = $this->post('is_delete');
        $remark = trim($this->post('remark'));

        $data = array();
        if (is_numeric($is_delete)) $data['is_delete'] = $is_delete + 0;
        if (!empty($remark)) $data['remark'] = $remark;
        $data['user_id'] = $user_info['id'];

        $mformula = new mFormula();
        $res = $mformula->updateFormula($id, $data);
        if (!$res) $this->ajax_json(true, '操作失败');

        $this->ajax_json(true, '操作成功');
    }

    public function unit_conv() {
        $this->view['unit_list'] = $GLOBALS['weight_convert_list'];

        $unit_type = trim($this->get('unit_type')) + 0;

        $m_formula = new mFormula();
        $condition = array();
        if ($unit_type) $condition['unit_type'] = $unit_type;
        $this->view['list'] = $m_formula->getUnitConvList($condition);
    }

    public function ajax_change_unit_conv() {
        $id = $this->post('id') + 0;
        $data = array(
            'status' => $this->post('status') + 0
        );

        $m_formula = new mFormula();
        $id = $m_formula->updateUnitConv($id, $data);
        if (!$id) $this->ajax_json(false, '操作失败');

        $this->ajax_json(true, '操作成功');
    }

    public function unit_conv_add() {
        $this->view['unit_list'] = $GLOBALS['weight_convert_list'];

        $id = $this->get('id');
        if ($id) {
            $mformula = new mFormula();
            $data = $mformula->getUnitConvById($id);

            $this->view['data'] = $data;
        }
    }

    public function ajax_save_unit_conv() {
        $id = $this->post('id') + 0;
        $unit_type = trim($this->post('unit_type')) + 0;
        $num = trim($this->post('num')) + 0;
        $from = trim($this->post('from'));

        $m_formula = new mFormula();
        $id = $m_formula->saveUnitConv($id, $unit_type, $num, $from);
        if (!$id) $this->ajax_json(false, $m_formula->getError());

        $this->ajax_json(true, '操作成功', array('id' => $id));
    }
}