<view class="container">
  <view class="hero fade-up">
    <view class="hero-badge">USER ADMIN</view>
    <view class="hero-title">用户与权限管理</view>
    <view class="hero-sub">支持账号查询、权限调整、批量导入，适用于企业商用场景。</view>
  </view>

  <view class="card fade-up fade-up-delay-1">
    <view class="card-title">搜索用户</view>
    <view class="field">
      <text class="field-label">关键词</text>
      <input class="input" placeholder="输入用户名或昵称" value="{{keyword}}" data-field="keyword" bindinput="onInput" />
    </view>
    <button class="btn btn-primary" loading="{{loading}}" bindtap="fetchUsers">查询</button>
  </view>

  <view class="card fade-up fade-up-delay-1">
    <view class="card-title">批量导入</view>
    <view class="card-desc">粘贴 JSON 数组，支持批量新增或更新用户信息。</view>
    <textarea class="textarea" value="{{importText}}" data-field="importText" bindinput="onInput" />
    <button class="btn btn-accent" bindtap="importUsers">执行导入</button>
  </view>

  <view class="card fade-up fade-up-delay-2" wx:if="{{users.length}}">
    <view class="card-title">用户列表</view>

    <view class="list-item" wx:for="{{users}}" wx:key="id">
      <view class="item-title">{{item.nickname}}（{{item.username}}）</view>
      <view class="item-sub">{{item.company || '未填写公司'}} · {{item.title || '未填写岗位'}} · {{item.is_admin ? '管理员' : '普通用户'}}</view>

      <view wx:if="{{editUserId === item.id}}">
        <input class="input" placeholder="昵称" value="{{editForm.nickname}}" data-field="nickname" bindinput="onEditInput" />
        <input class="input" placeholder="公司" value="{{editForm.company}}" data-field="company" bindinput="onEditInput" />
        <input class="input" placeholder="岗位" value="{{editForm.title}}" data-field="title" bindinput="onEditInput" />
        <input class="input" placeholder="手机号" value="{{editForm.phone}}" data-field="phone" bindinput="onEditInput" />
        <input class="input" placeholder="新密码（可选）" password value="{{editForm.password}}" data-field="password" bindinput="onEditInput" />

        <view class="row">
          <text class="label">管理员权限</text>
          <switch checked="{{editForm.is_admin}}" bindchange="onAdminSwitch" />
        </view>

        <view class="btn-row">
          <button class="btn btn-primary" bindtap="saveEdit">保存</button>
          <button class="btn btn-ghost" bindtap="cancelEdit">取消</button>
        </view>
      </view>

      <view wx:else class="btn-row">
        <button class="btn btn-ghost" data-id="{{item.id}}" bindtap="startEdit">编辑</button>
        <button class="btn btn-accent" data-id="{{item.id}}" bindtap="removeUser">删除</button>
      </view>
    </view>
  </view>
</view>
