<script setup name="userListDialog">
import { reactive, ref, watch } from 'vue'
import { userList } from '@/http/apis/systemManage/userManage'

const
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: {
      type: Array,
      default: undefined
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    }
  }),
  emits = defineEmits(['close', 'getProjectData']),
  searchForm = reactive({
    name: undefined
  }),
  tableListRef = ref(),
  total = ref(0),
  column = [
    {
      type: 'selection',
      reserveSelection: true,
      width: '55'
    },
    {
      label: '姓名',
      key: 'name',
      prop: 'name'
    },
    {
      label: '手机号码',
      key: 'phoneNo',
      prop: 'phoneNo',
      width: '150'
    },
    {
      label: '所属单位（主职）',
      key: 'orgName',
      prop: 'orgName',
      showOverflowTooltip: true,
      width: 250
    },
    {
      label: '所属区域',
      key: 'regionName',
      prop: 'regionName',
      width: '150'
    }
  ],
  tableData = ref([]),
  getTableData = async (pageParams = tableListRef.value?.pageParams) => {
    const res = await userList({
      ...pageParams,
      ...searchForm,
      ...props.params
    })
    total.value = res.data.total
    tableData.value = res.data.records || []
    const selectCopyData = JSON.parse(JSON.stringify(selectData.value)) || []
    if (selectCopyData?.length) {
      tableData.value && tableData.value.forEach(item => {
        const dataIds = selectCopyData.map(i => i.userId)
        if (dataIds.includes(item.userId)) {
          tableListRef.value.toggleRowSelect(item, true)
        }
      })
    }
  },
  search = () => {
    tableListRef.value.pageParams.pageNumber = 1
    getTableData()
  },
  reset = () => {
    searchForm.name = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  selectData = ref([]),
  selectionChange = val => {
    const ids = val.map(i => i.userId)
    tableData.value.forEach(row => {
      if (ids.includes(row.userId)) {
        selectData.value.push(row)
      } else {
        selectData.value = selectData.value.filter(i => i.userId !== row.userId)
      }
    })
  },
  confirm = () => {
    const obj = {}
    const data = selectData.value.reduce((cur, next) => {
      obj[next.userId] ? '' : obj[next.userId] = true && cur.push(next)
      return cur
    }, [])
    emits('getProjectData', data)
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      if (props.data?.length) {
        selectData.value = props.data.map(i => i)
      } else {
        selectData.value = []
      }
      getTableData()
    }
  }
)
</script>
<template>
  <el-dialog
    :model-value="visible"
    title="添加人员"
    width="70%"
    destroy-on-close
    @close="emits('close')"
  >
    <div class="search mb-16">
      <el-form label-suffix=":" :model="searchForm" size="small">
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="用户姓名">
              <el-input v-model="searchForm.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item class="btn">
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <table-list
      ref="tableListRef"
      :column="column"
      :data="tableData"
      :total="total"
      row-key="userId"
      @selection-change="selectionChange"
      @get-table-data="getTableData"
    />
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('close')">关闭</el-button>
        <el-button
          type="primary"
          @click="confirm"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
