<!-- 选择成员 -->
<script setup lang="ts" name="SelectEmployeesDialog">
import { computed, onMounted, reactive, ref, watch } from 'vue'
import type { IEmployees } from './dialog'
import { getUserList } from '@/api/system/user'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: () => [],
  },
  // 默认查询组织
  deptId: {
    type: String,
    default: '',
  },
  // 是否显示部门选择下拉菜单
  showDeptSelect: {
    type: Boolean,
    default: true,
  },
  // 是否多选
  multi: {
    type: Boolean,
    default: true,
  },
})
const emits = defineEmits(['update:visible', 'change'])
const table = ref() // 表格ref

// 表格表头
const columns = ref<TableColumn[]>([
  { text: '真实姓名', value: 'name', align: 'center' },
  { text: '所在组织机构', value: 'deptName', align: 'center' },
])

// 表格数据
const list = ref<IEmployees[]>([])

const loading = ref(false)
const total = ref(0)
// 查询参数
const listQuery = reactive({
  keywords: '',
  beginTime: '',
  endTime: '',
  deptId: '',
  offset: 1,
  limit: 10,
  sort: 'id',
  deptType: '',
})
// 默认给deptId赋值
listQuery.deptId = props.deptId
const checkedList = ref([])
// 多选的数据
const multipleTable = ref([])
const singleChecked = ref('') // 单选选中id
const closeDialog = () => {
  emits('update:visible', false)
}
const visibleDialog = computed({
  get() {
    return props.visible
  },
  set() {
    closeDialog()
  },
})

watch(() => props.visible, (val) => {
  // 清除上次选中
  if (!val) {
    table.value.clearMulti()
  }
})

// 点击确定，保存选择的成员配置
const saveDialog = () => {
  // 多选
  if (props.multi) {
    const checkedList = multipleTable.value.map((item: IEmployees) => {
      return {
        ...item,
        type: 1, // type: 1.成员 2.角色
        // targetId: item.roleId,
        // name: item.roleName,
        targetId: item.id,
        name: item.name,
      }
    })
    emits('change', checkedList)
  }
  else {
    const checkValue = list.value.find(item => item.id == singleChecked.value)
    emits('change', checkValue)
  }
}

const select = ref('')
// 获取列表数据
const fetchData = (isNowPage = false) => {
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  loading.value = true
  getUserList(listQuery).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loading.value = false
  })
}

// 多选
const multiSelect = (val: any) => {
  multipleTable.value = val
}

// 查询数据
const search = () => {
  fetchData(true)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData(true)
}

onMounted(() => {
  // 获取列表数据
  fetchData(true)
})
</script>

<template>
  <div class="select-emplotees-dialog">
    <el-dialog v-model="visibleDialog" title="选择成员" max-width="80%" append-to-body custom-class="chengyuan-dialog promoter_person">
      <!-- 右侧表格 -->
      <div ref="tableContainer" class="table-area">
        <!-- 筛选条件 -->
        <search-area @search="search">
          <search-item>
            <el-input v-model.trim="listQuery.keywords" placeholder="账户/真实姓名/手机号" clearable />
          </search-item>
        </search-area>
        <!-- 表头标题 -->
        <table-container :title-show="false" style="margin-top: 0;padding-top: 0;">
          <!-- 查询结果Table显示 -->
          <normal-table
            ref="table"
            :data="list"
            :total="total"
            :columns="columns"
            :query="listQuery"
            :list-loading="loading"
            :is-showmulti-select="multi"
            @change="changePage"
            @multi-select="multiSelect"
          >
            <!-- 序号 -->
            <template #preColumns>
              <el-table-column label="#" width="55" align="center">
                <template #default="scope">
                  {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
                </template>
              </el-table-column>
              <el-table-column v-if="!props.multi" label="" width="40" align="center">
                <template #default="scope">
                  <el-radio v-model="singleChecked" :label="scope.row.id" class="radio" />
                </template>
              </el-table-column>
            </template>
            <!-- 状态 -->
            <template #columns>
              <el-table-column label="状态" align="center" width="80">
                <template #default="scope">
                  <el-tag :type="scope.row.statusName === '启用' ? 'success' : 'danger'">
                    {{ scope.row.statusName }}
                  </el-tag>
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </table-container>
      </div>
      <!-- </div> -->
      <template #footer>
        <el-button @click="closeDialog">
          取 消
        </el-button>
        <el-button type="primary" @click="saveDialog">
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
@import "@/css/dialog.css";

.chengyuan-dialog {
  .el-table .cell {
    white-space: nowrap;
  }

  .el-dialog__body {
    display: flex;
    overflow: auto;
  }
}
</style>

<style lang="scss" scoped>
.chengyuan-dialog {
  width: 100%;

  .left-container {
    white-space: nowrap;
    height: 100%;
  }

  .table-area {
    flex: 1;
    overflow: auto;

    :deep(.el-radio__label) {
      display: none;
    }

    .pagination {
      margin-top: 10px;
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
}
</style>
