<?php
namespace Factory\Admin;

use Dever;

class User extends Core
{
    # 获取基本信息
    public function getData()
    {
        $id = Dever::input('id');
        if ($id) {
            $data = Dever::db('factory/member')->find($id);
        } else {
            $data = false;
        }
    
        return $data;
    }

    # 获取角色
    public function getRole()
    {
        $role = Dever::db('factory/member')->config['role'];

        $html = '<input update_type="checked" class="checkbox-checkall update_role_id update_value" type="checkbox" name="temp_update_role_id[]" value="0_update_role_id"/> 全选';

        $info = $this->getData();

        foreach ($role as $k => $v) {
            $checked = '';
            if ($info && $info['role_id']) {
                $temp = explode(',', $info['role_id']);
                if (in_array($k, $temp)) {
                    $checked = 'checked';
                }
            }
            $html .= '&nbsp;&nbsp;<input update_type="checked" class="autoload checkbox-checkall-0_update_role_id update_role_id update_value validate[required]" type="checkbox" name="update_role_id[]" value="'.$k.'" '.$checked.' /> '.$v.' ';
        }

        return $html;
    }

    # 订单列表
    public function getList()
    {
        $data = Dever::db('factory/member')->list(array('factory_id' => $this->id));

        $html = '';
        if ($data) {
            $role = Dever::db('factory/member')->config['role'];
            foreach ($data as $k => $v) {

                $edit = Dever::url('user_edit?id=' . $v['id'], 'factory');

                $temp = explode(',', $v['role_id']);
                $v['role_name'] = array();
                foreach ($temp as $k1 => $v1) {
                    if (isset($role[$v1])) {
                        $v['role_name'][] = $role[$v1];
                    }
                }
                $v['role_name'] = implode(',', $v['role_name']);

                $cdate = date('Y-m-d H:i', $v['cdate']);

                $button = '<a href="javascript:;" onclick="fastEdit($(this), \''.$edit.'\', \'编辑\', \'\', 1)"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-xs">编辑</button></a>';

                if ($v['state'] == 1) {
                    $state = 2;
                } else {
                    $state = 1;
                }

                $del = Dever::url('admin/user.del?id=' . $v['id'] . '&state=' . $state, 'factory');

                if ($state == 2) {
                    $button .= '&nbsp;&nbsp;<a href="'.$del.'" class="oper_delete"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-danger layui-btn-xs"><span class="am-icon-trash-o"></span>封禁</button></a>';
                } else {
                    $button .= '&nbsp;&nbsp;<a href="'.$del.'" class="oper_delete"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5 layui-btn layui-btn-danger layui-btn-xs"><span class="am-icon-trash-o"></span>恢复</button></a>';
                }
                


                $html .= '<tr><td>'.$v['name'].'</td><td>'.$v['mobile'].'</td><td>'.$v['role_name'].'</td><td>'.$cdate.'</td><td>'.$button.'</td></tr>';
            }
        }
        return $html;
    }

    # 获取基本信息
    public function getConfig()
    {
        $data['update'] = Dever::url('admin/user.update', 'factory');
        $data['add'] = Dever::url('user_edit', 'factory');
        $data['add'] = 'fastEdit($(this), \''.$data['add'].'\', \'添加新员工\', \'\', 1)';
        $data['search'] = Dever::url('user', 'factory');
        $data['delete'] = Dever::url('admin/del', 'factory');
        return $data;
    }

    # 认证
    public function update_api()
    {
        $input = Dever::input();

        $update['name'] = $input['update_name'];
        $update['avatar'] = $input['update_avatar'];
        $update['mobile'] = $input['update_mobile'];
        $update['role_id'] = $input['update_role_id'];
        $update['factory_id'] = $this->id;

        $update['where_id'] = Dever::input('update_where_id');
        if ($update['where_id']) {
            Dever::db('factory/member')->update($update);
        } else {
            Dever::db('factory/member')->insert($update);
        }

        return 'reload';
    }

    # 删除
    public function del_api()
    {
        $update['where_id'] = Dever::input('id');
        if ($update['where_id']) {
            $update['state'] = Dever::input('state');
            Dever::db('factory/member')->update($update);
        }

        return 'reload';
    }
}