<template>
  <div class="system-user">
    <div class="search">
      <div class="inp-item">
        <div class="i-name">姓名：</div>
        <div class="i-content">
          <el-input v-model="params.nickname" class="x-inp" placeholder="请输入..." />
        </div>
      </div>

      <div class="inp-item">
        <div class="i-name">用户名：</div>
        <div class="i-content">
          <el-input v-model="params.username" class="x-inp" placeholder="请输入..." />
        </div>
      </div>

      <div class="inp-item">
        <div class="i-name">所属组织：</div>
        <div class="i-content">
          <el-input v-model="params.deptName" class="x-inp" placeholder="请输入..." />
        </div>
      </div>

      <div class="btns">
        <el-button type="primary" @click="getOrganizes">搜索</el-button>
        <el-button @click="goRest">重置</el-button>
      </div>
    </div>

    <div class="ctrls-btns">
      <el-button @click="goCtrls('new')" type="primary">新增</el-button>

      <el-button class="def" @click="goRemove"> 批量删除 </el-button>
    </div>

    <el-table
      ref="tableRef"
      :data="listData"
      :row-class-name="
        ({ rowIndex }) => {
          return rowIndex % 2 === 0 ? 'stripe-row' : ''
        }
      "
      @selection-change="selectChange"
      class="list def"
      v-loading="loading"
      row-key="id"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="nickname" label="姓名" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="deptName" label="所属组织" />
      <el-table-column prop="mobile" label="手机号码" />
      <el-table-column prop="status" label="账户状态">
        <template #default="scope">
          <span v-if="scope.row.status == 0">正常</span>
          <span v-else>关闭</span>
        </template>
      </el-table-column>

      <el-table-column prop="loginNum" label="登录次数" />
      <el-table-column align="center" label="操作" width="300">
        <template #default="{ row }">
          <div class="tb-btns">
            <div @click="handleResetPwd(row)" class="def-btn">重置密码</div>
            <div class="dv-line"></div>
            <div @click="goCtrls('check', row)" class="def-btn">查看</div>
            <div class="dv-line"></div>
            <div @click="goCtrls('edit', row)" class="def-btn">编辑</div>
            <div class="dv-line"></div>
            <div @click="goCtrls('delete', row)" class="def-btn">删除</div>
          </div>
        </template>
      </el-table-column>

      <template #empty>
        <div class="empty">
          <span v-if="!loading">没有数据</span>
        </div>
      </template>
    </el-table>

    <!-- 分页 -->
    <div class="pages">
      <div class="sum">共 {{ params.total }} 条</div>

      <el-select
        class="page-select"
        @change="getOrganizes()"
        v-model="params.pageSize"
        :teleported="false"
      >
        <el-option
          v-for="item in [10, 20, 30, 50]"
          :key="item"
          :label="item + '条/页'"
          :value="item"
        />
      </el-select>

      <el-pagination
        v-if="params.total / params.pageSize > 1"
        :page-size="params.pageSize"
        layout="prev, pager, next"
        :total="params.total"
        class="car-pages"
        @change="getOrganizes"
      />
    </div>

    <OrgModel ref="modelRef" @update="getOrganizes" />
    <ResetPwd ref="resetPwd" />
  </div>
</template>
<script lang="ts" setup>
import OrgModel from './components/OrgModel.vue'
import ResetPwd from './components/ResetPwd.vue'
import { getEquipmentBasics, equipmentDelete } from '@/api/system'

import { getUserList, deleteOne, deleteBatch } from '@/api/system/user'

const params = reactive({
  nickname: '',
  deptName: '',
  username: '',
  total: 0,
  pageNo: 1,
  pageSize: 10
})

const selectMap = ref<any>({})
const listData = ref([])

const loading = ref(false)

const modelRef = ref(null)
const tableRef = ref(null)

const selectedIds = computed(() => {
  return Object.values(selectMap.value).flat()
})

// 条件重置
const goRest = () => {
  params.nickname = ''
  params.deptName = ''
  params.username = ''
  getOrganizes()
}

// 新增、查看编辑
const goCtrls = (type, row?) => {
  if (type === 'delete') {
    ElMessageBox.confirm(`确认要删除用户【${row.nickname}】吗？`, '注意', {
      confirmButtonText: '确定',
      cancelButtonClass: 'cancelButtonClass',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        // 删除
        deleteOne({ id: row.id }).then((res) => {
          if (res) {
            getOrganizes()
          }
        })
      })
      .catch()
  } else {
    modelRef.value.diaOpen(type, row)
    modelRef.value.getDeptlist()
    modelRef.value.getRolelist()
  }
}

// 获取列表数据
const getOrganizes = async (x?: number) => {
  params.pageNo = x || 1

  loading.value = true
  const res = await getUserList(params)
  listData.value = res.list || []
  params.total = res.total

  const selecteds = selectMap.value[params.pageNo] || []

  nextTick(() => {
    listData.value.forEach((item) => {
      if (selecteds.includes(item.id)) {
        tableRef.value!.toggleRowSelection(item, true)
      }
    })
  })

  loading.value = false
}

const resetPwd = ref(null)

/** 重置密码 */
const handleResetPwd = (row: any) => {
  resetPwd.value.diaOpen(row)
}
// 批量删除
const goRemove = () => {
  // const rows = tableRef.value.getSelectionRows()
  const rows = selectedIds.value || []

  if (rows.length) {
    // let ids = []
    // rows.forEach((v) => {
    //   ids.push(v.id)
    // })

    ElMessageBox.confirm(`确认要删除选中的用户吗？`, '注意', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        deleteBatch(rows).then((res) => {
          if (res) {
            getOrganizes()
          }
        })
      })
      .catch()
  } else {
    ElMessage.error('请选择!')
  }
}

// 筛选变化
const selectChange = (e) => {
  // selectMap.value[params.page]
  selectMap.value[params.pageNo] = e.map((item) => item.id)
}
onMounted(() => {
  getOrganizes()
})
</script>

<style lang="scss" scoped>
.system-user {
  margin: 0 45px;
}

.search {
  margin: 20px 0;
  display: flex;
  flex-wrap: wrap;
  color: white;
  background: rgba(69, 116, 182, 0.2);
  box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.1);
  padding: 18px 48px;
  border-radius: 8px;

  .inp-item {
    display: flex;
    align-items: center;
    margin-right: 60px;
    .i-name {
      font-size: 14px;
      width: 72px;
    }
    .i-content {
      display: flex;
      align-items: center;
      color: white;
      & > span {
        white-space: nowrap;
      }
      :deep(.el-select) {
        width: 300px;
      }
    }
  }
  .x-inp {
    width: 300px;
  }
  .num-inp {
    width: 126px;
    margin: 0 5px;
  }
  .btns {
    display: flex;
    align-items: center;
  }
}

.empty {
  color: rgba(240, 245, 255, 0.45);
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ctrls-btns {
  display: flex;
  align-items: center;
  .tips {
    margin-left: 20px;
    color: white;
    font-size: 14px;
    span {
      color: #ffa71a;
    }
  }
}

.tb-btns {
  margin: 10px 0;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;

  .def-btn {
    cursor: pointer;
    color: #00aeff;
    font-weight: 600;
    margin: 0 8px;
  }

  .dv-line {
    height: 12px;
    width: 1px;
    background: rgba(240, 245, 255, 0.45);
    margin: 0 8px;
  }
}

.list {
  margin-top: 20px;
  :deep(.el-checkbox__inner) {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 2px;
  }
}

.pages {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  color: white;
  .sum {
    margin-right: 20px;
  }
  .page-select {
    width: 128px;
    margin-right: 20px;
    :deep(.el-select__wrapper) {
      border-radius: 0;
      background: rgba(32, 58, 96, 0.6);
      border: 1px solid rgba(32, 58, 96, 0.6);
      box-shadow: 0 0 0;

      .el-select__placeholder {
        color: white;
      }
    }
  }
}
</style>

<style lang="scss">
.cancelButtonClass {
  background: #ffffff;
  color: #606266;
  border: 1px solid #909399;
  // border-color: #606266;
}
.system-user {
  .el-input__inner,
  .el-select__input {
    color: #ffffff;
  }
  .el-input__wrapper {
    border-radius: 0;
    background: transparent;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
  }
  .el-select {
    display: block;
    .el-select__selected-item {
      color: #ffffff;
    }
  }
  .el-form-item__label {
    color: #ffffff;
    width: 120px;
  }
  .el-form-item__content {
    width: 400px;
  }

  .el-radio {
    color: white;
    height: 26px;
    .el-radio__inner {
      background: transparent;
      border-color: rgba($color: #ffffff, $alpha: 0.3);
      &::after {
        background-color: #007dfa;
      }
    }
  }
}
.el-pagination {
  &.car-pages {
    --el-pagination-bg-color: rgba(32, 58, 96, 0.6);
    --el-pagination-button-color: white;
    --el-pagination-button-disabled-bg-color: rgba(32, 58, 96, 0.4);
    --el-pagination-button-disabled-color: rgba(255, 255, 255, 0.5);
    .number {
      margin: 0 4px;
      &.is-active {
        background: #007dfa;
        color: white;
      }
    }
  }
}
</style>
