<!-- 意见登记表详情 -->
<script name="ExamineDetailApproved" lang="ts" setup>
import { ElMessage } from 'element-plus'
import { sendTo } from '@/api/resource/examine'
import FilterCustomerStaff from '@/views/resource/common/filterCustomerStaff.vue'

// 从路由中传过来的参数
const id = ref<string>('')
const customerId = ref<string>('')
const signUserId = ref<string>('')

const refCustomerStaffFilter = ref()

const route = useRoute()
const router = useRouter()

// 逻辑
// 关闭
const resetForm = () => {
  sessionStorage.removeItem('examineInfo') // 返回列表时 将缓存中的数据删除
  router.go(-1)
}

// 点击 发送 按钮
const sendToCustomer = () => {
  refCustomerStaffFilter.value.showOrHideFilterDialog(true)
}

const sysUserSelectedHandler = (row: any) => {
  refCustomerStaffFilter.value.showOrHideFilterDialog(false)

  if (row.id !== '') {
    customerId.value = row.deptId // 委托方表中的deptId
    signUserId.value = row.id // 委托方签名人员id（发送给委托方要进行签名的人员id，系统用户表中的userId）

    sendTo({ id: id.value, customerId: customerId.value, signUserId: signUserId.value }).then((res) => {
      if (res.code === 200) {
        // 提示保存成功
        ElMessage.success(`审批表发送到 ${row.customerName} 的 ${row.name} 成功`)
      }
      else {
        // 提示失败信息
        ElMessage.error(`审批表发送至受检单位失败：${res.message}`)
      }
    })
  }
  else {
    ElMessage.error('发送给委托方人员不能为空，请重新选择')
  }
}

// 打印Word
const printToWord = () => {
  // exportFile({ id: noticeInfo.value.id, pdf: false })
}

// 打印PDF
const printToPDF = () => {

}

// 打印
const printClickedHandler = () => {
  ElMessage.success('打印成功')
}

const initDialog = (params: any) => {
  // 从路由中获取参数
  id.value = params.id !== undefined ? params.id : ''
}

onMounted(() => {
  initDialog(route.query)
})
</script>

<template>
  <app-container>
    <detail-page title="要求、委托书及合同评审表">
      <template #btns>
        <el-button type="primary" @click="sendToCustomer">
          发送给委托方
        </el-button>
        <el-button type="primary" @click="printToWord">
          导出Word
        </el-button>
        <el-button type="primary" @click="printToPDF">
          导出PDF
        </el-button>
        <el-button type="primary" @click="printClickedHandler">
          打印
        </el-button>

        <el-button type="info" @click="resetForm()">
          关闭
        </el-button>
      </template>
    </detail-page>

    <filter-customer-staff ref="refCustomerStaffFilter" title="请选择委托方和签收人员" @record-selected="sysUserSelectedHandler" />
  </app-container>
</template>
