Newer
Older
smart-metering-front / src / views / measure / source / components / listPage.vue
<script lang="ts" setup name="ListSource">
import { reactive, ref } from 'vue'
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { IlistType, IlistTypes } from '../list_interface'
import ManageBox from './manageBox.vue'
import { getSoucreListDelete, getapprovalListPage } from '@/api/system/source'
import { uploadApi } from '@/api/system/notice'
import { exportExcel } from '@/utils/exportXlsx'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import { SCHEDULE } from '@/utils/scheduleDict'
const props = defineProps({
  name: {
    type: String,
    default: '全部',
  },
  buttoms: {
    type: Array,
    default: () => [],
  },
})
const emit = defineEmits(['setData']) // 关闭
// 查询条件
const listQuery: Ref<IlistTypes> = ref({
  approvalStatus: '0',
  businessContent: '',
  companyArea: '',
  companyCity: '',
  companyProvince: '',
  formId: SCHEDULE.SUPPLIER_APPROVAL,
  supplierName: '',
  supplierNo: '',
  offset: 1,
  limit: 20,
})
// 控制是否显示新增页面
const show = ref(true)
// 表格数据
const list = ref([])
// 总数
const total = ref(0)
// 控制审批操作弹窗的开关
const applyShow = ref(false)
// 表头
const columns = ref([
  {
    text: '溯源供方编号',
    value: 'supplierNo',
    width: '120',
    align: 'center',
  },
  {
    text: '溯源供方名称',
    value: 'supplierName',
    width: '120',
    align: 'center',
  },
  {
    text: '业务内容',
    value: 'businessContent',
    align: 'center',
  },
  {
    text: '业务范围',
    value: 'businessScope',
    align: 'center',
  },
  {
    text: '负责人',
    value: 'director',
    align: 'center',
  },
  {
    text: '联系方式',
    value: 'phone',
    align: 'center',
  },
  {
    text: '地址',
    value: 'address',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'name',
    align: 'center',
  },
  {
    text: '备注',
    value: 'name',
    align: 'center',
  },
])
// 初始化路由
const $router = useRouter()
const buttonArray = ref<string[]>([])
// 选中的内容
const checkoutList = ref([])
// 文件上传input
const fileRef = ref()
// 删除id
const deleteId = ref('')
// 详情id
const infoId = ref('0')
// 点击按钮
const buttonType = ref('')
const loadingTable = ref(false)
const fetchData = (isNowPage: boolean) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  getapprovalListPage(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// fetchData(true)
// 多选发生改变时
const handleSelectionChange = (e: any) => {
  checkoutList.value = e
}
// 点击删除和编辑的参数类型
interface rowReturn {
  id: string
  supplierName: string
}
const pageTypeMap = ref('')
// 点击删除或者取消
const handleDelete = (row: rowReturn, pageType: string) => {
  ElMessageBox.confirm(
    `确认${pageType}${row.supplierName}吗?`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      if (pageType === '删除') {
        getSoucreListDelete({ id: row.id as string }).then((res) => {
          if (res.code === 200) {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            fetchData(true)
          }
        })
      }
      else {
        getSoucreListDelete({ id: row.id as string }).then((res) => {
          if (res.code === 200) {
            ElMessage({
              type: 'success',
              message: '取消成功',
            })
            fetchData(true)
          }
        })
      }
    })
}

const approvalDialog = ref()
// 点击每条数据操作
const handleEdit = (row: rowReturn, pageType: string) => {
  if (pageType === '编辑' || pageType === '查看') {
    pageTypeMap.value = pageType === '编辑' ? 'edit' : 'detail'
    $router.push(`/source/approve/${pageTypeMap.value}/${row.id}`)
  }
  else if (pageType === '同意' || pageType === '驳回' || pageType === '拒绝') {
    // applyShow.value = true
    approvalDialog.value.initDialog('agree', '1')
  }
  else {
    handleDelete(row, pageType)
  }
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
const cancelEvent = () => {
  console.log('cancel!')
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    approvalStatus: '',
    businessContent: '',
    companyArea: '',
    companyCity: '',
    companyProvince: '',
    formId: '1',
    supplierName: '',
    supplierNo: '',
    offset: 1,
    limit: 20,
  }
}
// 导出
const exportExcelBtn = () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (checkoutList.value.length <= 0) {
    exportExcel({
      json: list.value.map((item: IlistType, index: number) => ({ index: index + 1, supplierNo: item.supplierNo, supplierName: item.supplierName, businessContent: item.businessContent, briefName: item.briefName, director: item.director, mobile: item.mobile, companyCity: item.companyCity, createTime: item.createTime, remark: item.remark })),
      name: '溯源供方',
      titleArr: ['序号', '溯源供方编号', '溯源供方名称', '业务内容', '业务资质', '负责人', '联系方式', '地址', '创建时间', '备注'],
      sheetName: 'sheet1',
    })
  }
  else {
    exportExcel({
      json: checkoutList.value.map((item: IlistType, index: number) => ({ index: index + 1, supplierNo: item.supplierNo, supplierName: item.supplierName, businessContent: item.businessContent, briefName: item.briefName, director: item.director, mobile: item.mobile, companyCity: item.companyCity, createTime: item.createTime, remark: item.remark })),
      name: '溯源供方',
      titleArr: ['序号', '溯源供方编号', '溯源供方名称', '业务内容', '业务资质', '负责人', '联系方式', '地址', '创建时间', '备注'],
      sheetName: 'sheet1',
    })
  }
  loading.close()
}
// 上传文件/批量导入
const onFileChange = (event: any) => {
  if (event.target.files[0].type === 'application/pdf') {
    if (event.target.files?.length !== 0) {
      // 创建formdata对象
      const fd = new FormData()
      fd.append('multipartFile', event.target.files[0])
      uploadApi(fd).then((res) => {
        if (res.code === 200) {
          ElMessage.success('上传成功')
        }
        else {
          ElMessage.error(res.message)
        }
      })
    }
  }
  else {
    ElMessage.error('请上传pdf格式')
  }
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData(true)
}
const upload = () => {
  fileRef.value.click()
}
const uploadAll = () => {
  upload()
}

const add = () => {
  $router.push('/source/approve/add')
}
const exportAll = () => {
  exportExcelBtn()
}
// 审批结束回调
const approvalSuccess = () => {
  fetchData(true)
}
// 审批弹窗关闭
const handleClose = () => {
  applyShow.value = false
}
// 点击审批的提交
const applyListSubmit = (row: any) => {
  console.log(row)
  handleClose()
}
watch(() => show.value, (newVal) => {
// 变化后存储
  emit('setData', newVal)
},
{ deep: true, immediate: true })
// 打印
const printObj = ref({
  id: 'print', // 需要打印元素的id
  popTitle: '溯源供方编号模板', // 打印配置页上方的标题
  extraHead: '', // 最上方的头部文字,附加在head标签上的额外标签,使用逗号分割
  preview: false, // 是否启动预览模式,默认是false
  standard: '',
  extarCss: '',
})
</script>

<template>
  <div v-if="show">
    <app-container>
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input
            v-model.trim="listQuery.supplierNo"
            placeholder="溯源供方编号"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.supplierName"
            placeholder="溯源供方名称"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.businessContent"
            placeholder="业务内容"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.businessContent"
            placeholder="地址"
            clearable
          />
        </search-item>
      </search-area>
      <table-container>
        <template #btns-right>
          <icon-button icon="icon-add" title="新建" type="primary" @click="add" />
          <icon-button icon="icon-export" title="导出" type="primary" @click="exportAll" />
          <icon-button v-print="printObj" icon="icon-print" title="打印" type="primary" />
        </template>
        <input v-show="false" ref="fileRef" type="file" accept="pdf/*" @change="onFileChange">
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery"
          :list-loading="loadingTable" is-showmulti-select @change="changePage" @multiSelect="handleSelectionChange"
        >
          <template #columns>
            <el-table-column label="操作" align="center" width="300">
              <template #default="{ row }">
                <el-button
                  v-for="(item, index) in buttoms"
                  :key="index"
                  size="small"
                  :type="item.type" link
                  @click="handleEdit(row, item.name)"
                >
                  {{ item.name }}
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
  <manage-box :apply-show="applyShow" @applyListSubmit="applyListSubmit" @handleClose="handleClose" />
  <approval-dialog ref="approvalDialog" @on-success="approvalSuccess" />
</template>