Newer
Older
xc-business-system / src / views / resource / customer / info / list.vue
<!-- 委托方名录 -->
<script name="CustomerInfoList" lang="ts" setup>
import { type DateModelType, ElMessage, ElMessageBox } from 'element-plus'
import type { ICustomerInfo, IDictType, IListQuery } from './customer-info'
import { getDictByCode } from '@/api/system/dict'
import type { IMenu } from '@/components/buttonBox/buttonBox'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { draftDelete, getCustomerInfoList } from '@/api/resource/customer'

const active = ref('')
const menu = ref<IMenu[]>([]) // 审批状态按钮组合
const approvalStatusList = ref<String[]>([
  '全部', '已审批', '草稿箱', '待审批', '审批中', '已通过', '未通过', '已取消',
])
const buttonBoxActive = 'customerInfoApproval' // 存储在sessionstorage里面的字段名,用于记录右上角buttonbox点击状态
const flowFormId = 'zyglwtfml'

const { proxy } = getCurrentInstance() as any
const router = useRouter()

// 查询条件
const searchQuery = ref<IListQuery>({
  customerNo: '', // 委托方编号
  customerName: '', // 委托方名字
  contacts: '', // 联系人
  createTimeStart: '', // 创建时间-起始
  createTimeEnd: '', // 创建时间-结束
  approvalStatus: active.value, // 审批状态
  formId: flowFormId, // 表单id(流程定义对应的表单id,等价于业务id),此处为固定值
  offset: 1,
  limit: 20,
})
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '委托方编号', value: 'customerNo', align: 'center' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '联系人', value: 'contacts', align: 'center' },
  { text: '座机电话', value: 'mobile', align: 'center', width: '160' },
  { text: '手机号码', value: 'phone', align: 'center', width: '160' },
  { text: '地址', value: 'address', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180' },
])
const customerInfoList = ref<Array<ICustomerInfo>>([]) // 表格数据

// 逻辑
const addCustomerInfo = () => {
  router.push({
    query: {
      type: 'create',
    },
    path: '/customer/info/detail',
  })
}

const detail = (row: ICustomerInfo) => {
  router.push({
    query: {
      type: 'detail',
    },
    path: '/customer/info/detail',
  })
}

const deleteById = (id: number, name: string) => {
  ElMessageBox.confirm(`是否删除委托方 ${name}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    draftDelete({ id }).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`委托方 ${name} 删除成功`)
        fetchData()
      }
      else {
        ElMessage.error(`委托方 ${name} 删除失败: ${res.message}`)
      }
    })
  })
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getCustomerInfoList(searchQuery.value).then((response) => {
    customerInfoList.value = response.data.rows.map((item) => {
      return {
        ...item,
        createTime: item.createTime.length > 16 ? item.createTime.substring(0, 16) : '',
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}

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

// 重置
const reset = () => {
  searchQuery.value = {
    customerNo: '', // 委托方编号
    customerName: '', // 委托方名字
    contacts: '', // 联系人
    createTimeStart: '', // 创建时间-起始
    createTimeEnd: '', // 创建时间-结束
    approvalStatus: active.value, // 审批状态
    formId: flowFormId, // 表单id(流程定义对应的表单id,等价于业务id),此处为固定值
    offset: 1,
    limit: 20,
  }
  dateRange.value = ['', '']
  fetchData(true)
}

const changeCurrentButton = (val: string) => {
  active.value = val // 此时的tab
  window.sessionStorage.setItem(buttonBoxActive, val) // 记录tab状态
  reset() // 刷新
}

const getApprovalStatusDict = () => {
  getDictByCode('approvalStatus').then((res) => {
    if (res.code === 200) {
      res.data.forEach((item: IDictType) => {
        if (approvalStatusList.value.includes(item.name)) {
          menu.value.push({
            name: item.name,
            id: `${item.value}`,
          })
        }
      })
    }
  })
}

const getDict = async () => {
  getApprovalStatusDict()
}

watch(dateRange, (val) => {
  if (val) {
    searchQuery.value.createTimeStart = `${val[0]}`
    searchQuery.value.createTimeEnd = `${val[1]}`
  }
  else {
    searchQuery.value.createTimeStart = ''
    searchQuery.value.createTimeEnd = ''
  }
})

onMounted(async () => {
  await getDict()

  if (window.sessionStorage.getItem(buttonBoxActive)) {
    active.value = window.sessionStorage.getItem(buttonBoxActive)!
  }
  else {
    active.value = '0' // 全部
  }
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.customerNo" placeholder="委托方编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.customerName" placeholder="委托方名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.contacts" placeholder="联系人" clearable />
      </search-item>
      <search-item>
        <el-date-picker v-model="dateRange" type="daterange" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container>
      <!-- 表头区域 -->
      <template #btns-right>
        <input v-show="(searchQuery.limit === 0)" ref="fileRef" type="file" multiple @change="onFileChange">
        <icon-button icon="icon-print" title="新建" @click="pirintList" />
        <icon-button icon="icon-add" title="新建" @click="addCustomerInfo" />
      </template>

      <!-- 表格区域 -->
      <normal-table
        id="registerTabel"
        :data="customerInfoList" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>
              <el-button
                v-if="proxy.hasPerm(`/resource/customer/infoList/delete`)"
                size="small" type="danger" link
                @click="deleteById(row.id, row.customerName)"
              >
                删除
              </el-button>
              <el-button size="small" type="primary" link @click="detail(row)">
                同意
              </el-button>
              <el-button size="small" type="primary" link @click="detail(row)">
                拒绝
              </el-button>
              <el-button size="small" type="primary" link @click="detail(row)">
                取消
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>

    <!-- 右上角按钮集合 -->
    <button-box :active="active" :menu="menu" @change-current-button="changeCurrentButton" />
  </app-container>
</template>