<template>
  <el-dialog
    :modelValue="modelValue"
    @update:modelValue="
      (newValue: boolean) => $emit('update:modelValue', newValue)
    "
    title="用户信息"
    width="800"
    draggable
    overflow
  >
    <div class="table-box">
      <div class="search">
        <el-row gutter="20">
          <el-col :span="8"></el-col>
          <el-col :span="12"><el-input placeholder="请输入学生学号" v-model="name"></el-input></el-col>
          <el-col :span="4"><el-button type="primary" @click="search" >搜索</el-button></el-col>
        </el-row>
      </div>
      <el-table
        ref="multipleTableRef"
        border
        :data="tableData"
        @row-click="(row: any) => (selectedRowId = row.id)"
        append-to-body
        class="table"
        v-loading="loading"
        style="margin-top: 20px"
      >
        <el-table-column label="选择" width="55">
          <template #default="scope">
            <el-radio v-model="selectedRowId" :value="scope.row.id" />
          </template>
        </el-table-column>
        <el-table-column label="序号">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column property="realname" label="真实姓名" sortable />
        <el-table-column property="work_no" label="学号" sortable />
      </el-table>
    </div>

    <template #footer>
      <div class="pagin-box">
        <el-pagination
          class="pagination"
          style="width: 100%"
          v-model:current-page="params.pageNo"
          v-model:page-size="params.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="slo, total,slot,  sizes, prev, pager, next, jumper"
          :total="total"
          @change="getList"
        />
      </div>
      <div class="dialog-footer">
        <!-- <el-button @click="$emit('update:modelValue', false)">关闭</el-button> -->
        <el-button 
        @click="closeDialog"
          >关闭</el-button>
        <el-button
          type="primary"
          @click="
            () => {
              $emit('update:modelValue', false)
              $emit(
                'selected',
                tableData.find((o) => o.id === selectedRowId),
              )
              selectedRowId = null
            }
          "
        >
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { getMembersList } from '@/api/person'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const props = defineProps(['modelValue'])
const $emit = defineEmits(['update:modelValue', 'selected'])
const route = useRoute()

watch(
  () => props.modelValue,
  () => getList(),
)

// 数据
const tableData = reactive<any[]>([])
const selectedRowId = ref()

//取消
const closeDialog = ()=>{
  $emit('update:modelValue', false)
  $emit('selected', {});
  selectedRowId.value = null
}
// 分页
const total = ref(0)

const params = reactive<any>({
  pageNo: 1,
  pageSize: 10,
  onlRepUrlParamStr: `annualCompid=${route.query.objName}`,
  entryFormat: '团队',
  id: route.query.id,
  name: '',
})
const name = ref('')
// 搜索
const search = () => {
  params.work_no="*"+name.value+"*"
  getList()
}
const loading = ref(false)
const getList = async () => {
  if (!props.modelValue) return
  loading.value = true
  try {
    // const data = 
    const res: any = await getMembersList(params)
    tableData.length = 0
    tableData.push(...res.result.records)
    total.value = res.result.total
  } catch (error: any) {
    ElMessage({
      message: error.message,
      type: 'error',
    })
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.form-box {
  padding: 0 60px;
}

.pagin-box {
  width: 100%;
  height: 64px;
  display: flex;
  align-items: center;

  .pagination {
    padding: 0 24px;

    :deep() {
      .el-pagination__total {
        margin-right: auto;
      }

      li.number.is-active {
        background-color: #42d9ac;
        color: rgba(255, 255, 255, 0.9);
        font-family:
          Microsoft YaHei UI,
          Microsoft YaHei UI;
      }

      span.el-pagination__jump {
        background-color: #f3f3f3;
        padding: 2px 8px;
      }
    }
  }
}
</style>
