<commit_before>import * as React from 'react'
import { Alert, Button, Checkbox, Input, Modal, Tabs } from 'antd'
import { Column, Table } from '../../lib/table'
import { Link } from '../../lib/link'
import { Icon } from '../../lib/icon'

import { md5Password } from '../../utils/manager'
import { AdminUserModel, AdminUserState } from './reducer'

interface FormSave {
  username?: string
  password?: string
}

interface FormEdit {
  username?: string
  password?: string
  enabled?: boolean
}

const formSave: FormSave = {}
const formEdit: FormEdit = {}

interface AdminUserProps extends AdminUserState {
  bodyWidth: number
  saveModel: (model: {}) => void
  editModel: (model: {}) => void
}

export function AdminUser(props: AdminUserProps) {

  function getColumns(): Column<AdminUserModel>[] {
    return [
      {
        key: 'id',
        title: '#',
        format: (t) => t.id
      },
      {
        key: 'username',
        title: '用户名',
        format: (t) => t.username
      },
      {
        key: 'enabled',
        title: '启用',
        format: (t) => t.enabled ? '是' : '否'
      },
      {
        key: 'registerDate',
        title: '注册时间',
        format: (t) => formatRegisterDate(t)
      },
      {
        key: 'lastLoggedIn',
        title: '最后登入',
        format: (t) => formatLastLoggedIn(t)
      },
      {
        key: 'control',
        title: '功能',
        format: (t) => <Link onClick={() => showEditModal(t)}>编辑</Link>
      }
    ]
  }

  function formatRegisterDate(t: AdminUserModel) {
    return formatLong(t.registerDate, 0, 10)
  }

  function formatLastLoggedIn(t: AdminUserModel) {
    const text = t.lastLoggedIn
    return text ? formatLong(text, 5, 11) : '从未登入'
  }

  function formatLong(text: string, start: number, length: number) {
    return props.bodyWidth <= 600 ? text.substr(start, length) : text
  }

  function saveUser() {
    const username = formSave.username
    const password = formSave.password

    if (!username) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户名称'})
      return
    }

    if (!password) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户密码'})
      return
    }

    const encode = md5Password(username, password)
    props.saveModel({username, password: encode})
  }

  function editUser(id: number) {
    const username = formEdit.username
    const password = formEdit.password
    const enabled = formEdit.enabled

    if (!username) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户名称'})
      return
    }

    const encode = password ? md5Password(username, password) : ''
    props.editModel({id, username, password: encode, enabled})
  }

  return (
    <div className="admin-users">
      <Tabs>
        <Tabs.TabPane tab="用户列表" key="1">
          {props.errors && (
            <Alert message={props.errors} type="error"/>
          )}
          {props.models && (
            <Table rows={props.models} columns={getColumns()}/>
          )}
        </Tabs.TabPane>
        <Tabs.TabPane tab="添加用户" key="2">
          <div style={{padding: 10}}>
            <Input
              prefix={<Icon type="user" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formSave.username}
              onChange={e => formSave.username = e.target.value}
              placeholder="请输入用户名称"
            />
          </div>
          <div style={{padding: 10}}>
            <Input
              type="password"
              prefix={<Icon type="key" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formSave.password}
              onChange={e => formSave.password = e.target.value}
              placeholder="请输入用户密码"
            />
          </div>
          <div style={{padding: '5px 10px'}}>
            <Button type="primary" onClick={saveUser}>添加用户</Button>
          </div>
        </Tabs.TabPane>
      </Tabs>
    </div>
  )

  function showEditModal(user: AdminUserModel) {
    formEdit.username = user.username
    formEdit.enabled = user.enabled
    formEdit.password = <PASSWORD>

    Modal.confirm({
      title: '编辑用户',
      okText: '保存',
      okType: 'primary',
      onOk: () => editUser(user.id),
      cancelText: '取消',
      content: (
        <div>
          <div style={{padding: 10}}>
            <Input
              prefix={<Icon type="user" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formEdit.username}<commit_msg>refactor(admin-user): Update some names<commit_after>import * as React from 'react'
import { Alert, Button, Checkbox, Input, Modal, Tabs } from 'antd'
import { Column, Table } from '../../lib/table'
import { Link } from '../../lib/link'
import { Icon } from '../../lib/icon'

import { md5Password } from '../../utils/manager'
import { AdminUserModel, AdminUserState } from './reducer'

interface FormSave {
  username?: string
  password?: string
}

interface FormEdit {
  username?: string
  password?: string
  enabled?: boolean
}

const formSave: FormSave = {}
const formEdit: FormEdit = {}

interface AdminUserProps extends AdminUserState {
  bodyWidth: number
  saveModel: (model: {}) => void
  editModel: (model: {}) => void
}

export function AdminUser(props: AdminUserProps) {

  function getColumns(): Column<AdminUserModel>[] {
    return [
      {
        key: 'id',
        title: '#',
        format: (t) => t.id
      },
      {
        key: 'username',
        title: '用户名',
        format: (t) => t.username
      },
      {
        key: 'enabled',
        title: '启用',
        format: (t) => t.enabled ? '是' : '否'
      },
      {
        key: 'registerDate',
        title: '注册时间',
        format: (t) => formatRegisterDate(t)
      },
      {
        key: 'lastLoggedIn',
        title: '最后登入',
        format: (t) => formatLastLoggedIn(t)
      },
      {
        key: 'control',
        title: '功能',
        format: (t) => <Link onClick={() => showEditModal(t)}>编辑</Link>
      }
    ]
  }

  function formatRegisterDate(t: AdminUserModel) {
    return formatLong(t.registerDate, 0, 10)
  }

  function formatLastLoggedIn(t: AdminUserModel) {
    const text = t.lastLoggedIn
    return text ? formatLong(text, 5, 11) : '从未登入'
  }

  function formatLong(text: string, start: number, length: number) {
    return props.bodyWidth <= 600 ? text.substr(start, length) : text
  }

  function saveModel() {
    const username = formSave.username
    const password = form<PASSWORD>

    if (!username) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户名称'})
      return
    }

    if (!password) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户密码'})
      return
    }

    const encode = md5Password(username, password)
    props.saveModel({username, password: encode})
  }

  function editModel(id: number) {
    const username = formEdit.username
    const password = formEdit.password
    const enabled = formEdit.enabled

    if (!username) {
      Modal.warning({title: '请检查输入项', content: '你必须输入用户名称'})
      return
    }

    const encode = password ? md5Password(username, password) : ''
    props.editModel({id, username, password: encode, enabled})
  }

  return (
    <div className="admin-users">
      <Tabs>
        <Tabs.TabPane tab="用户列表" key="1">
          {props.errors && (
            <Alert message={props.errors} type="error"/>
          )}
          {props.models && (
            <Table rows={props.models} columns={getColumns()}/>
          )}
        </Tabs.TabPane>
        <Tabs.TabPane tab="添加用户" key="2">
          <div style={{padding: 10}}>
            <Input
              prefix={<Icon type="user" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formSave.username}
              onChange={e => formSave.username = e.target.value}
              placeholder="请输入用户名称"
            />
          </div>
          <div style={{padding: 10}}>
            <Input
              type="password"
              prefix={<Icon type="key" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formSave.password}
              onChange={e => formSave.password = e.target.value}
              placeholder="请输入用户密码"
            />
          </div>
          <div style={{padding: '5px 10px'}}>
            <Button type="primary" onClick={saveModel}>添加用户</Button>
          </div>
        </Tabs.TabPane>
      </Tabs>
    </div>
  )

  function showEditModal(model: AdminUserModel) {
    formEdit.username = model.username
    formEdit.enabled = model.enabled
    formEdit.password = <PASSWORD>

    Modal.confirm({
      title: '编辑用户',
      okText: '保存',
      okType: 'primary',
      onOk: () => editModel(model.id),
      cancelText: '取消',
      content: (
        <div>
          <div style={{padding: 10}}>
            <Input
              prefix={<Icon type="user" style={{color: 'rgba(0,0,0,.25)'}}/>}
              defaultValue={formEdit.username}
{'fraction_non_alphanumeric': 0.15446418467805817, 'fraction_numerical': 0.00860020368903474, 'mean_word_length': 2.2161572052401746, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '660271', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2899, 'n_words': 604}