<nz-page-header>
  <nz-page-header-title
    >申报单位管理
    <br />
    <div class="subtitle">
      使用组织机构树管理使用“十四五”高等教育国家规划教材申报系统的所有单位，单位与申报工作流程相关联。
    </div>
  </nz-page-header-title>
  <nz-page-header-extra>
    <nz-space>
      <button
        style="background: #3e49b3; border: 1px #3e49b3"
        *nzSpaceItem
        nz-button
        nzType="primary"
        (click)="addMember()"
      >
        添加账号
      </button>
    </nz-space>
  </nz-page-header-extra>
</nz-page-header>
<div class="edit-content">
  <div class="tool">
    <div class="tool-left">
      <div class="search">
        <nz-input-group style="width: 210px" [nzPrefix]="prefixTemplateUser">
          <input
            type="text"
            nz-input
            placeholder="搜索"
            [(ngModel)]="searchValue"
          />
        </nz-input-group>
        <ng-template #prefixTemplateUser
          ><span nz-icon nzType="search"></span
        ></ng-template>
        <button
          nz-button
          nzType="default"
          nz-dropdown
          [nzDropdownMenu]="menutep"
          [nzPlacement]="'bottomLeft'"
          style="margin-left: 10px"
        >
          <span nz-icon nzType="plus" nzTheme="outline"></span>新建
        </button>
        <nz-dropdown-menu #menutep="nzDropdownMenu">
          <ul nz-menu>
            <li nz-menu-item>
              <button
                nz-button
                nzType="link"
                (click)="showModalDepart('add')"
                style="color: #231c1f"
              >
                <span nz-icon nzType="plus" nzTheme="outline"></span>添加部门
              </button>
            </li>
            <li nz-menu-item>
              <button
                nz-button
                nzType="link"
                style="color: #231c1f"
                (click)="showModalOrganize()"
              >
                <span nz-icon nzType="merge" nzTheme="outline"></span>
                新建组织
              </button>
            </li>
          </ul>
        </nz-dropdown-menu>
      </div>
      <div class="">{{ currentDepart ? currentDepart.title : "" }}</div>
      <div class="tag">
        <span nz-icon nzType="team" nzTheme="outline"></span>
        {{ profiles.length }}人
      </div>
      <label nz-checkbox [(ngModel)]="checkedShowFilter"
        >仅显示部门直属成员</label
      >
    </div>
    <div class="tool-right">
      <a nz-button nzType="link" (click)="addMember()">
        <span nz-icon nzType="plus" nzTheme="outline"></span>添加成员
      </a>
    </div>
  </div>
  <div class="layout" #maxWidth>
    <div class="sider" #sider>
      <nz-tree
        [nzData]="nodes"
        nzAsyncData
        [nzSearchValue]="searchValue"
        (nzClick)="nzEvent($event)"
        (nzExpandChange)="nzEvent($event)"
        (nzSearchValueChange)="onSearch($event)"
        [nzTreeTemplate]="nzTreeTemplate"
      ></nz-tree>
      <ng-template #nzTreeTemplate let-node let-origin="origin">
        <span
          class="custom-node"
          (contextmenu)="contextMenu($event, menu, node)"
        >
          <span class="folder-name">{{ node.title }}</span>
        </span>
      </ng-template>
      <!-- 节点右键菜单 -->
      <nz-dropdown-menu #menu="nzDropdownMenu">
        <ul nz-menu>
          <li nz-menu-item (click)="showModalDepart('add')">添加下级部门</li>
          <li nz-menu-item (click)="showModalDepart('edit')">编辑部门</li>
          <li nz-menu-item (click)="onDelDepart()">删除部门</li>
        </ul>
      </nz-dropdown-menu>
    </div>
    <div class="breadcrumb">
      <nz-table
        #tableData
        [nzData]="profiles"
        [nzTotal]="profiles.length"
        [nzPageSize]="10"
        style="margin: 10px 0"
        [nzLoading]="loading"
        nzSize="middle"
        [nzNoResult]="emptyResult"
      >
        <thead>
          <tr>
            <th
              nzWidth="120px"
              nzLeft
              [nzChecked]="checkedAll"
              [nzIndeterminate]="indeterminate"
              nzLabel="Select all"
              (nzCheckedChange)="onAllChecked($event)"
            ></th>
            <th nzWidth="120px" nzLeft>用户</th>
            <th nzWidth="120px">手机号</th>
            <th nzWidth="120px">邮箱</th>
            <th nzWidth="120px">人员类型</th>
            <th nzWidth="120px">所属部门</th>
            <th nzWidth="50px" nzRight>操作</th>
          </tr>
        </thead>
        <tbody>
          @for (data of profiles; track data.objectId) {
          <tr>
            <td
              nzLeft
              [nzChecked]="data.checked"
              (nzCheckedChange)="onItemChecked(data.objectId, $event)"
            ></td>
            <td
              nzLeft
              nzEllipsis
              (click)="goDateil(data.user?.objectId)"
              class="activeTd"
            >
              <nz-avatar nzIcon="user"></nz-avatar>
              {{ data?.user.name }}
            </td>
            <td>
              {{ data?.user.phone }}
            </td>
            <td>
              {{ data?.user.email || data?.email }}
            </td>
            <td>
              {{ data?.identity }}
            </td>
            <td>
              {{ data?.user.departmentName }}
            </td>
            <td nzRight>
              <button
                nz-button
                nz-dropdown
                [nzDropdownMenu]="menu"
                [nzPlacement]="'bottomLeft'"
              >
                <span nz-icon nzType="ellipsis" nzTheme="outline"></span>
              </button>
              <nz-dropdown-menu #menu="nzDropdownMenu">
                <ul nz-menu>
                  <li nz-menu-item>
                    <button nz-button style="color: #231c1f" nzType="link">
                      <span nz-icon nzType="stop" nzTheme="outline"></span
                      >移除部门
                    </button>
                  </li>
                </ul>
              </nz-dropdown-menu>
            </td>
          </tr>
          }
        </tbody>
      </nz-table>
      <ng-template #emptyResult>
        <nz-empty nzNotFoundImage="/img/group-empty.png"></nz-empty>
      </ng-template>
    </div>
  </div>
</div>
<nz-modal
  [(nzVisible)]="isVisible"
  nzTitle="{{ editType == 'edit' ? '编辑' : '新建' }}部门"
  (nzOnCancel)="handleCancel()"
  nzWidth="800px"
  nzCentered
>
  <ng-container *nzModalContent>
    <div nz-row class="depart-modal">
      <div nz-col nzSpan="12">
        <div class="row">
          <div class="label">部门名称</div>
          <div class="value">
            <input
              nz-input
              placeholder="请输入部门名称"
              [(ngModel)]="editObject.name"
              type="text"
            />
          </div>
        </div>
        <div class="row">
          <div class="label">code</div>
          <div class="value">
            <input
              nz-input
              placeholder="请输入code"
              [(ngModel)]="editObject.code"
              type="text"
            />
          </div>
        </div>
        <div class="row">
          <div class="label">部门描述</div>
          <div class="value">
            <textarea
              [nzAutosize]="{ minRows: 4, maxRows: 4 }"
              placeholder="请输入部门描述"
              nz-input
              [(ngModel)]="editObject.desc"
            ></textarea>
          </div>
        </div>
      </div>
      <div nz-col nzSpan="12">
        <div class="row">
          <div class="label">上级部门</div>
          <div class="value">
            <input
              nz-input
              placeholder="请选择上级"
              [ngModel]="editObject.parent?.title"
              type="text"
            />
          </div>
        </div>
        <div class="select">
          <div class="bar">
            <a style="color: #86909c" (click)="onPre(nodes[0].key)">{{
              nodes[0].title
            }}</a>
            @for (data of parentMap; track data.title) {
            <span style="margin: 0 10px">/</span>
            <a (click)="onPre(data.key, $index)">{{ data.title }}</a>
            }
          </div>
          <div class="tree">
            @if (parentList.length > 0) { @for (data of parentList; track
            $index) {
            <div class="li" (click)="onCheckedDepart(data)">
              <label
                nz-radio
                ngModel
                [nzValue]="data.key"
                (click)="onCheckedDepart(data, true)"
                >{{ data.title }}</label
              >
              <span nz-icon nzType="right" nzTheme="outline"></span>
            </div>
            } }
            @else {
              <nz-empty nzNotFoundContent="暂无部门"></nz-empty> 
            }
          </div>
        </div>
      </div>
    </div>
  </ng-container>
  <div *nzModalFooter>
    <button nz-button nzType="default" (click)="handleCancel()">取消</button>
    <button
      nz-button
      nzType="primary"
      style="background: #3e49b3; border: 1px #3e49b3"
      (click)="handleOk()"
    >
      确定
    </button>
  </div>
</nz-modal>
