Newer
Older
xc-business-system / src / views / resource / person / register / component / authorize.vue
<!-- 人员登记 基本信息 -->
<script name="RegisterAuthorize" lang="ts" setup>
import { ElMessage, dayjs } from 'element-plus'
import type { IStaffAuthorizeInfo } from '../person-regitster'
import { useCheckList } from '@/commonMethods/useCheckList'
import { useSetAllRowReadable } from '@/commonMethods/useSetAllRowReadable'
import { addAuthorizeRecList, delAuthorizeRecList, getAuthorizeList } from '@/api/resource/register'

const props = defineProps({
  operation: { type: String, default: '' },
  staffId: { type: String, default: '' },
})

const listQuery = {
  id: '',
  limit: 1000,
  offset: 1,
}

const authorizeColumns = [
  { text: '授权参数', value: 'authorizeParam', align: 'center', width: '240', required: true },
  { text: '授权内容', value: 'authorizeContent', align: 'center', required: true },
  { text: '授权日期', value: 'authorizeDate', align: 'center', width: '160', required: true },
  { text: '有效日期', value: 'validDate', align: 'center', width: '160', required: true },
  { text: '附件', value: 'file', align: 'center' },
] // 表头

const authorizeList = ref<IStaffAuthorizeInfo[]>([]) // 表格数据

// 表格被选中的行
const authorizeSelected = ref<IStaffAuthorizeInfo[]>([])

const authorizeRecord: IStaffAuthorizeInfo = {
  id: '',
  staffId: '',
  authorizeParam: '',
  authorizeContent: '',
  authorizeDate: '',
  validDate: '',
  file: '',
  editable: true,
}

// 逻辑
const addEditableRow = (tableName: string) => {
  switch (tableName) {
    case 'authorize':
      authorizeList.value = authorizeList.value.map((item: any) => ({
        ...item,
        authorizeDate: dayjs(item.authorizeDate).format('YYYY-MM-DD'),
        validDate: dayjs(item.validDate).format('YYYY-MM-DD'),
      }))
      if (useCheckList(authorizeList.value, authorizeColumns, '授权项目信息')) {
        useSetAllRowReadable(authorizeList.value)
        authorizeList.value.push({ ...authorizeRecord })
      }
      break

    default:
      break
  }
}

const authMultiSelect = (e: any) => {
  authorizeSelected.value = e
}

const getAuthorizeListByStaffId = () => {
  getAuthorizeList(listQuery).then((res) => {
    if (res.code === 200) {
      authorizeList.value = res.data.rows.map((item: any) => ({
        ...item,
        authorizeDate: dayjs(item.authorizeDate).format('YYYY-MM-DD'),
        validDate: dayjs(item.validDate).format('YYYY-MM-DD'),
      }))
    }
  })
}

// 增加授权信息记录
const addAuthorizeRecords = () => {
  if (useCheckList(authorizeList.value, authorizeColumns, '授权项目信息') === false) {
    return
  }

  const newRecords = ref<Array<IStaffAuthorizeInfo>>([])
  newRecords.value = authorizeList.value.filter(item => item.id === '')

  if (newRecords.value.length > 0) {
    addAuthorizeRecList(newRecords.value).then((res) => {
      if (res.code === 200) {
        ElMessage.success('添加授权项目信息记录成功')
        getAuthorizeListByStaffId()
      }
      else {
        ElMessage.error(`添加授权项目信息记录失败:${res.message}`)
      }
    })
  }
}

// 删除授权项目信息记录
const delAuthorizeRecords = () => {
  if (authorizeSelected.value.length === 0) {
    ElMessage.warning('请至少选择一行')
    return
  }

  // 前端界面删除
  authorizeList.value = authorizeList.value.filter(item => authorizeSelected.value.includes(item) === false)

  // 调用后端接口进行删除
  const authIdsSelected = ref<string[]>([])
  authIdsSelected.value = authorizeSelected.value.map((item: { id: string }) => item.id)
  authIdsSelected.value = authIdsSelected.value.filter(item => item !== '')
  if (authIdsSelected.value.length > 0) {
    delAuthorizeRecList({ ids: authIdsSelected.value }).then((res) => {
      if (res.code === 200) {
        ElMessage.success('删除授权项目信息记录成功')
      }
      else {
        ElMessage.error(`删除授权项目信息记录失败:${res.message}`)
      }

      getAuthorizeListByStaffId()
    })
  }
}

watch(() => props.staffId, (newVal: string) => {
  listQuery.id = newVal
  authorizeRecord.staffId = newVal

  getAuthorizeListByStaffId()
})

defineExpose({
  addAuthorizeRecords,
})
</script>

<template>
  <app-container>
    <el-form ref="authorizeFormRef" label-position="right" label-width="110px" border stripe>
      <table-container title="授权项目信息">
        <template v-if="props.operation !== 'detail'" #btns-right>
          <el-button type="primary" @click="addEditableRow('authorize')">
            增加行
          </el-button>
          <el-button type="info" @click="delAuthorizeRecords">
            删除行
          </el-button>
        </template>

        <!-- 表格区域 -->
        <el-table :data="authorizeList" border style="width: 100%;" @selection-change="authMultiSelect">
          <el-table-column v-if="props.operation !== 'detail'" type="selection" align="center" width="38" />
          <el-table-column align="center" label="序号" width="55" type="index" />
          <el-table-column
            v-for="item in authorizeColumns"
            :key="item.value"
            :prop="item.value"
            :label="item.text"
            :width="item.width"
            align="center"
          >
            <template #header>
              <span v-show="item.required && props.operation !== 'detail'" style="color: red;">*</span><span>{{ item.text }}</span>
            </template>
            <template #default="scope">
              <span v-if="!scope.row.editable">{{ scope.row[item.value] }}</span>
              <el-date-picker v-else-if="item.value === 'authorizeDate'" v-model="scope.row.authorizeDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD HH:mm:ss" type="date" style="width: 100%;" />
              <el-date-picker v-else-if="item.value === 'validDate'" v-model="scope.row.validDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD HH:mm:ss" type="date" style="width: 100%;" />
              <el-input v-else v-model="scope.row[item.value]" :autofocus="true" :placeholder="`${item.text}`" class="input" />
            </template>
          </el-table-column>
        </el-table>
      </table-container>
    </el-form>
  </app-container>
</template>