<script lang="ts" setup name="standardListAdd">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, UploadProps, UploadUserFile } from 'element-plus'
import dayjs from 'dayjs'
import type { ICertification } from './cert-interface'
import { getStaffList } from '@/api/measure/person'
import { getTypeSelect } from '@/api/system/price'
import { UploadFile } from '@/api/measure/file'
import useUserStore from '@/store/modules/user'
import showPhoto from '@/views/system/tool/showPhoto.vue'
const loading = ref(false) // 表单加载状态
const infoId = ref('') // 证书id
const printStatus = ref('') // 可打印状态
const buttonArray = ref<string[]>([]) // 顶部按钮数组
const pageType = ref('add') // 页面类型: add,edit, detail
const buttonLoading = ref(false) // 按钮加载状态

// 从路由中获取页面类型参数
const $route = useRoute()
if ($route.params && $route.params.id) {
  infoId.value = $route.query.id as string
}
if ($route.query && $route.query.printStatus) {
  printStatus.value = $route.query.printStatus as string
}

// 逻辑代码
const dataForm = ref<ICertification>({
  certificationId: '',
  certificationReportCode: '',
  certificationReportName: '',
  certificateReportType: '',
  certificateReportTypeName: '',
  sampleId: '',
  sampleNo: '',
  sampleName: '',
  sampleModel: '',
  manufacturingNo: '',
  orderId: '',
  orderNo: '',
  customerNo: '',
  customerName: '',
  measureCatogory: '',
  measureCatogoryName: '',
  createUser: '',
  createUserName: '',
  printNum: '',
  printStatus: '',
  createTime: '',
  calibrationMajor: '',
  issuanceDate: '',
  signId: '',
  signName: '',
  approvalSignId: '',
  approvalSignName: '',
  certificateReportFile: '',
  originalRecordId: '',
  originalRecordCode: '',
  roomNumber: '',
  temperature: '',
  humidity: '',
  manufacturer: '',
  customerPhone: '',
  customerAddress: '',
  deliverer: '',
  delivererTel: '',
  planDeliverTime: '',
  requireOverTime: '',
  mesureContent: '',
  remark: '',
})

// 下拉框数组类型
interface IOptions {
  id: string
  name: string
  value: string
  organizeName: string
  deptId: string
}
const checkTypeOptions = ref<IOptions[]>([])
const calibrationMajorList = ref<IOptions[]>([]) // 计量专业下拉框
const standardManagerStateList = ref<IOptions[]>([]) // 管理状态下拉框
const standardLevelList = ref<IOptions[]>([]) // 标准等级下拉框
const transmitRangeList = ref<IOptions[]>([]) // 传递范围下拉框
// 标准所在部门下拉框
const standardUsersDeptList = ref<IOptions[]>([])
// 获取下拉框
const getSelectList = (code: string) => {
  getTypeSelect(code).then((res) => {
    if (code === 'calibrationMajor') {
      calibrationMajorList.value = res.data
    }
    else if (code === 'standardManagerState') {
      standardManagerStateList.value = res.data
    }
    else if (code === 'standardLevel') {
      standardLevelList.value = res.data
    }
    else if (code === 'transmitRange') {
      transmitRangeList.value = res.data
    }
    else if (code === 'standardCategory') {
      checkTypeOptions.value = res.data
    }
  })
}
getSelectList('calibrationMajor') // 获取校准专业下拉框
getSelectList('standardManagerState') // 获取管理状态下拉框
getSelectList('standardLevel') // 获取标准等级下拉框
getSelectList('transmitRange') // 获取传递范围下拉框
getSelectList('standardCategory') // 获取类别下拉框
const approvalDialog = ref()
const params = ref({
  director: '',
  meterMajor: '',
  createEndTime: '',
  createstartTime: '',
  organizeName: '',
  organizeNo: '',
  organizeType: '2',
  pdeptId: null,
  offset: 1,
  limit: 999999,
})

// 数据
const list = ref<object[]>([])
// 标准配套设备更换表格总数
const total = ref(0)

// 所用设备列表
const deviceList = ref([])
// 标准装置列表
const standardDeviceList = ref([])
// 文件列表
const fileList = ref([])

// 标准配套设备表头类型
interface columnsType {
  text: string
  value: string
  align: string
  required: Boolean
  choose: Boolean
  time: Boolean
}

// 标准配套设备更换表头
const columns = ref<columnsType[]>([
  { text: '设备名称', value: 'equipmentName', align: 'center', required: true, choose: true, time: false },
  { text: '设备编号', value: 'equipmentNo', align: 'center', required: true, choose: false, time: false },
  { text: '型号', value: 'modelNo', align: 'center', required: true, choose: false, time: false },
  { text: '出厂编号', value: 'mesureResult', align: 'center', required: true, choose: false, time: false },
  { text: '证书有效期', value: 'validDate', align: 'center', required: true, choose: false, time: false },
])
// 标准配套设备更换查询条件
const listQuery = ref<object>({
  limit: 10,
  offset: 1,
})
// 标准配套设备更换表格loding
const loadingTable = ref(false)
// 申请文档表头
const columnsFiles = ref<columnsType[]>([
  {
    text: '文件编号',
    value: 'fileName',
    align: 'center',
    required: true,
    choose: true,
    time: false,
  },
  {
    text: '文件名称',
    value: 'createTime',
    align: 'center',
    required: true,
    choose: false,
    time: true,
  },
  {
    text: '文件代号',
    value: 'remark',
    align: 'center',
    required: true,
    time: false,
    choose: false,
  },
])
// 添加表格数据对象
const addList = ref({})
// 添加表格弹窗开关
const dialogVisible = ref(false)
// 选择标准装置数据列表下拉框类型
interface standardListType {
  id: string
  standardName: string
}
// 选择标准装置数据列表下拉框
const standardList = ref<standardListType[]>([])
// 初始化路由
const $router = useRouter()
// 关闭
const close = () => {
  // emit('close')
  $router.back()
}
// 点击文件预览
const uploadShow = (e: any) => {
  const URL = window.URL || window.webkitURL
  window.open(URL.createObjectURL(e.raw))
}

// 获取详情信息
const getInfo = (id: string | number) => {
  dataForm.value = {
    certificationId: '1111',
    certificationReportCode: '1111',
    certificationReportName: '校准证书',
    certificateReportType: '1',
    certificateReportTypeName: '校准证书',
    sampleId: '1',
    sampleNo: '45678652',
    sampleName: '压力表',
    sampleModel: 'xx',
    manufacturingNo: '北无',
    orderId: '123456',
    orderNo: '123456',
    customerNo: '4578545',
    customerName: '京东',
    measureCatogory: '1',
    measureCatogoryName: '光学',
    createUser: '',
    createUserName: '张三',
    printNum: '0',
    printStatus: '1',
    createTime: '2023-01-05',
    calibrationMajor: '1',
    issuanceDate: '2023-02-05',
    signId: '',
    signName: '公司公章',
    approvalSignId: '',
    approvalSignName: 'CNAS章',
    certificateReportFile: '',
    originalRecordId: '',
    originalRecordCode: '11111',
    roomNumber: '304',
    temperature: '36',
    humidity: '27',
    manufacturer: '北无',
    customerPhone: '',
    customerAddress: '',
    deliverer: '张三',
    delivererTel: '87447',
    planDeliverTime: '2023-02-05',
    requireOverTime: '2023-02-05',
    mesureContent: 'IP68',
    remark: '',
  }
}

getInfo(infoId.value)

// 保存后的id
const addId = ref('')

// 打印证书
const printCert = () => {

}
// 点击关闭
const handleClose = () => {
  dialogVisible.value = false
}
</script>

<template>
  <app-container>
    <detail-page title="证书打印">
      <template #btns>
        <el-button v-if="printStatus === '1'" type="primary" @click="printCert">
          打印
        </el-button>
        <el-button type="primary" @click="printCert">
          预览
        </el-button>
        <el-button type="info" @click="close">
          关闭
        </el-button>
      </template>
      <el-form ref="ruleFormRef" :model="dataForm" label-position="right" label-width="auto">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="检校专业:">
              <el-select
                v-model="dataForm.calibrationMajor"
                clearable
                size="default"
                class="full-width-input"
                placeholder=""
                disabled
              >
                <el-option
                  v-for="item in calibrationMajorList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出具日期:">
              <el-input v-model="dataForm.issuanceDate" class="full-width-input" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="证书编号:">
              <el-input
                v-model.trim="dataForm.certificationReportCode"
                :placeholder="pageType === 'detail' ? '' : '请输入证书编号'"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="证书报告名称:">
              <el-input
                v-model.trim="dataForm.certificationReportName"
                :placeholder="pageType === 'detail' ? '' : '请输入证书报告名称'"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="电子签章:">
              <el-input
                v-model.trim="dataForm.signName"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="认可标志专用章:">
              <el-input
                v-model.trim="dataForm.approvalSignName"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="证书报告附件:" prop="contactInfo">
              <show-photo v-if="dataForm.certificateReportFile" :minio-file-name="dataForm.certificateReportFile" />
              <span v-else>无附件</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="原始记录编号:" prop="category">
              <el-input
                v-model.trim="dataForm.originalRecordCode"
                class="full-width-input"
                disabled
              >
                <template v-if="pageType !== 'detail'" #append>
                  <el-button
                    size="small"
                  >
                    查看
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="检校地点:" prop="standardLevel">
              <el-input
                v-model.trim="dataForm.roomNumber"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="温度(℃):" prop="transmitRange">
              <el-input
                v-model.trim="dataForm.temperature"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="相对湿度(%):">
              <el-input
                v-model.trim="dataForm.humidity"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="样品编号:" prop="preparationDate">
              <el-input
                v-model.trim="dataForm.sampleNo"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="样品名称:" prop="standardLaboratory">
              <el-input
                v-model.trim="dataForm.sampleName"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出厂编号:" prop="laboratoryOwner">
              <el-input
                v-model.trim="dataForm.manufacturingNo"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="型号:" prop="laboratoryOwnerContact">
              <el-input
                v-model.trim="dataForm.sampleModel"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="生产厂家:" prop="managerState">
              <el-input
                v-model.trim="dataForm.manufacturer"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托方代码:" prop="measureMajor">
              <el-input
                v-model.trim="dataForm.customerNo"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托方名称:" prop="assessmentUnit">
              <el-input
                v-model.trim="dataForm.customerName"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托方电话:" prop="assessDate">
              <el-input
                v-model.trim="dataForm.customerPhone"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="委托方地址:" prop="uncertainty">
              <el-input
                v-model.trim="dataForm.customerAddress"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="送检人:" prop="mesureRange">
              <el-input
                v-model.trim="dataForm.deliverer"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系方式:" prop="mesureRange">
              <el-input
                v-model.trim="dataForm.delivererTel"
                class="full-width-input"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预计送达时间:" prop="authorityOfDate">
              <el-date-picker
                v-model="dataForm.planDeliverTime"
                type="datetime"
                class="full-width-input"
                clearable
                format="YYYY-MM-DD hh:mm:ss"
                value-format="YYYY-MM-DD h:m:s a"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预计检完时间:" prop="data">
              <el-date-picker
                v-model="dataForm.requireOverTime"
                type="datetime"
                class="full-width-input"
                format="YYYY-MM-DD hh:mm:ss"
                clearable
                value-format="YYYY-MM-DD h:m:s a"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="检定项目:" prop="standardTemperature">
              <el-input
                v-model.trim="dataForm.mesureContent"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="备注:">
              <el-input
                v-model.trim="dataForm.remark"
                class="full-width-input"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </detail-page>
    <detail-block title=" 所用设备">
      <el-table :data="deviceList" border style="width: 100%;">
        <el-table-column align="center" label="序号" width="80" type="index" />
        <el-table-column
          v-for="item in columns"
          :key="item.value"
          :prop="item.value"
          :label="item.text"
          align="center"
        />
      </el-table>
    </detail-block>
    <detail-block title="所依据的技术文件">
      <el-table :data="fileList" border style="width: 100%;">
        <el-table-column align="center" label="序号" width="80" type="index" />
        <el-table-column
          v-for="item in columnsFiles"
          :key="item.value"
          :prop="item.value"
          :label="item.text"
          align="center"
        />
      </el-table>
    </detail-block>
  </app-container>
</template>

<style lang="scss">
.el-upload-list {
  display: none !important;
}
</style>
