<script lang='ts' setup>

import OrgModel from './components/OrgModel.vue'

import { getOrgainzations, deleteOrganization } from '@/api/system/organization'

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

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

const transToTree = (tree, map) => {

  tree.forEach(item => {
    item.children = map[item.id] || null
    if (item.children) {
      transToTree(item.children, map)
    }
  })

  return tree

}

// 获取列表数据
const getOrganizes = async (x?: number) => {

  params.pageNo = x || 1
  loading.value = true
  const res = await getOrgainzations(params)

  const pMap = {}
  const cMap = {}

  res.forEach(item => {
    if (!pMap[item.parentId]) {
      pMap[item.parentId] = []
    }
    pMap[item.parentId].push(item)
    cMap[item.id] = item
  })

  const tree = res.filter(item => !cMap[item.parentId])
  transToTree(tree, pMap)

  listData.value = tree
  loading.value = false

}

// 批量删除
const goRemove = () => {
  const rows = tableRef.value.getSelectionRows()
  if (!rows.length) {
    return ElMessage.warning('请勾选需要删除的组织！')
  }
  ElMessageBox.confirm(
      `确认要删除组织【${rows.map(item => item.name).join('、')}】吗？`,
      '注意',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    ).then(() => {
      // 删除
      deleteOrganization(rows.map(item => item.id)).then(res => {
        if (res) {
          getOrganizes()
        }
      })
    }).catch()
}

// 筛选变化
const selectChange = () => {
  selectMap.value[params.page]
}

onMounted(() => {

  getOrganizes()

})

</script>

<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.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.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.alias" class="x-inp" placeholder="请输入..." />
      </div>
    </div>

    <div class="btns">
      <el-button @click="getOrganizes" type="primary">搜索</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"
      :tree-props="{
        checkStrictly: true
      }"
      :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="code" label="组织编码" width="300" />
      <el-table-column prop="name" label="组织名称" />
      <el-table-column prop="alias" label="组织别名" width="200" />
      <el-table-column prop="type" label="组织类型" width="200" />
      <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>

  <OrgModel :data="listData" ref="modelRef" @update="getOrganizes" />

</div>



</template>

<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;
  }
}

</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: 400px;
  }
}
.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>
