<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\Member;
use app\model\MemberAuthGroup;
use app\model\RegisterField;
use app\model\Operatingcity;
use app\model\Users;

class MemberController extends Base
{

	function index()
	{
		$weid = weid();
		$page = input('post.page', 1, 'intval');
		$ptype = 'member';

		$Fielddata = RegisterField::where(['weid' => $weid, 'ptype' => $ptype])->select()->toArray();

		if (empty($Fielddata)) {
			RegisterField::datainitial($ptype);
		}

		$query = $this->setSearch();

		$res = $query->order('sort asc,id desc')
			->paginate(getpage())
			->toArray();

		if (!empty($res['data'])) {
			foreach ($res['data'] as &$vo) {
				$vo['gid'] = MemberAuthGroup::getgroupName($vo['gid']);
				$vo['pid'] = Member::get_name($vo['pid']) ?? '平台';
				$vo['regdate'] = time_format($vo['regdate']);
				$vo['lastdate'] = time_format($vo['lastdate']);

				if (!empty($vo['uuid'])) {
					$vo['username'] = Users::getusername($vo['uuid']);
				}
				$vo['region_name'] = $vo['province_name'] . $vo['city_name'] . $vo['district_name'];
			}
		}
		$data['data'] = $res;
		if ($page == 1) {
			$data['field_data']['RegisterField'] = RegisterField::getlistViewField($ptype);
		}

		return $this->json($data);
	}
	function setSearch()
	{
		$keyword = trim(input('post.keyword', '', 'serach_in'));
		$status = input('post.status', '', 'serach_in');
		$path = input('post.path', '', 'serach_in');
		$weid = weid();

		if ($path == "/member/audit") {
			$status = "0";
		}

		$query = Member::where(['weid' => $weid]);

		if (!empty($keyword)) {

			$query->where('nickname|telephone', 'like', '%' . $keyword . '%');
		}

		if (!empty($status) || $status === "0") {
			$query->where(['status' => $status]);
		}
		return $query;
	}
	function listUpdate()
	{
		$data = only('id,status');
		if (!$data['id']) throw new ValidateException('参数错误');
		Member::update($data);

		return $this->json(['msg' => '操作成功']);
	}
	function delete()
	{
		return $this->del(new Member());
	}
}
