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

      <div class="inp-item">
        <div class="i-name">英文名称：</div>
        <div class="i-content">
          <el-input v-model="params.code" 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="name" label="角色名称" width="300" />
      <el-table-column prop="code" label="英文名称" />
      <el-table-column prop="deptName" label="归属机构" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <div class="tb-btns">
            <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" />
  </div>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import OrgModel from './components/OrgModel.vue'
import { getEquipmentBasics, equipmentDelete } from '@/api/system'
import { defaultProps, handleTree } from '@/utils/tree'
import { getOrgainzations, deleteOne, getSimpleDeptList, deleteBatch } from '@/api/system/role'

const params = reactive({
  code: '',
  name: '',
  deptName: '',
  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.code = ''
  params.name = ''
  params.deptName = ''
  getOrganizes()
}

// 新增、查看编辑
const goCtrls = (type, row?) => {
  if (type === 'delete') {
    ElMessageBox.confirm(`确认要删除角色【${row.name}】吗？`, '注意', {
      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()
  }
}

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

  loading.value = true
  const res = await getOrgainzations(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 goRemove = async () => {
  // 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-organize {
  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-organize {
  .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: 440px;
  }

  .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>
