<!-- 交接单管理详情 -->
<script name="InterchangeReceiptDetail" lang="ts" setup>
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules, UploadUserFile } from 'element-plus'
import dayjs from 'dayjs'
import type { IDetailTableList, IList as IOrderList } from '../order/order-interface'
import type { IForm, IList } from './interchangeReceipt-interface'
import selectOrderDialog from './dialog/selectOrderDialog.vue'
import selectEquipmentDialog from './dialog/selectEquipment.vue'
import { getSealInfoList } from '@/api/resource/seal'
import { getDictByCode } from '@/api/system/dict'
import { getUid } from '@/utils/getUid'
import scanSampleDialog from '@/components/ScanSampleDialog/index.vue'
import type { dictType } from '@/global'
import { useCheckList } from '@/commonMethods/useCheckList'
import { useDoubleClickTableRow, useSetAllRowReadable } from '@/commonMethods/useSetAllRowReadable'
import useUserStore from '@/store/modules/user'
import { listPageApi } from '@/api/system/tool'
import { getOrderDetail } from '@/api/business/manager/order'
import {
  addInterchangeReceipt,
  getInterchangeReceiptDetail,
  updateInterchangeReceipt,
} from '@/api/business/manager/interchangeReceipt'
const textMap: { [key: string]: string } = {
  edit: '编辑',
  add: '新建',
  detail: '详情',
}// 页面类型字典
const user = useUserStore() // 用户信息
const pageType = ref('add') // 页面类型: add, edit, detail
const infoId = ref('') // id
const ruleFormRef = ref() // 表单ref
// 表单
const form: Ref<IForm> = ref({
  interchangeNo: '', // 交接单编号
  createUserId: '', // 创建人id
  createUserName: '', // 创建人名称
  createTime: '', // 创建时间
  orderNo: '', // 任务单编号
  orderId: '', // 任务单编号
  deliverer: '', // 送检人
  customerId: '', // 委托方id
  customerNo: '', // 委托方代码
  customerName: '', // 委托方名称
  delivererTel: '', // 委托方电话
  requireOverTime: '', // 要求检完时间
  customerAddress: '', // 委托方地址
  isUrgent: '', // 是否加急
  maintainMajor: '', // 检修专业
})
const rules = reactive<FormRules>({ // 表单验证规则
  orderNo: [{ required: true, message: '任务单编号不能为空', trigger: ['change', 'blur'] }],
  deliverer: [{ required: true, message: '送检人不能为空', trigger: 'blur' }],
  delivererTel: [{ required: true, message: '电话不能为空', trigger: 'change' }],
})

// -------------------------------------------字典------------------------------------------
const isUrgentList = ref<dictType[]>([]) // 是否加急
const receiveCompanyList = ref<dictType[]>([]) // 印章

// 获取字典值
async function getDict() {
  // 是否加急
  const response = await getDictByCode('isUrgent')
  isUrgentList.value = response.data

  const res = await getDictByCode('interchangeReceiptSealNo')
  receiveCompanyList.value = res.data
}
// -----------------------------------------路由参数----------------------------------------
// 从路由中获取页面类型参数
const $route = useRoute()
if ($route.params && $route.params.type) {
  pageType.value = $route.params.type as string
  if ($route.params.id) {
    infoId.value = $route.params.id as string
  }
}
console.log('页面类型', pageType.value)

// --------------------------------------------选择任务单------------------------------------
const selectOrderRef = ref() // 选择任务单组件ref
// 点击选择任务单
const selectOrder = () => {
  selectOrderRef.value.initDialog()
}
// 选好任务单
const clickConfirmOrder = (val: IOrderList[]) => {
  getOrderDetail({ id: val[0].id }).then((res) => {
    form.value.orderNo = res.data.data.orderNo!// 任务单编号
    form.value.orderId = val[0].id// 任务单id
    form.value.customerId = res.data.data.customerId!// 委托方id
    form.value.customerNo = res.data.data.customerNo!// 委托方代码
    form.value.customerName = res.data.data.customerName!// 委托方名称
    form.value.deliverer = res.data.data.deliverer!// 送检人
    form.value.delivererTel = res.data.data.customerPhone!// 委托方电话
    form.value.requireOverTime = res.data.data.requireOverTime!// 要求检完时间
    form.value.customerAddress = res.data.data.customerAddress!// 委托方地址
    form.value.maintainMajor = res.data.data.maintainMajor!// 检修专业
    form.value.isUrgent = `${res.data.data.isUrgent}` // 是否加急
  })
}
// -------------------------------------------表格------------------------------------------
const list = ref<IDetailTableList[]>([])// 表格数据
const selectEquipmentRef = ref() // 选择设备组件ref
const isMulti = ref(false) // 表格是否多选
const selectIndex = ref(0) // 选择的第几列
const checkoutList = ref<IDetailTableList[]>([]) // 选中的内容
// 表头
const columns = ref([
  { text: '统一编号', value: 'equipmentNo', align: 'center', width: '240', disabled: true, required: true },
  { text: '设备名称', value: 'equipmentName', align: 'center', required: false },
  { text: '型号规格', value: 'model', align: 'center', required: false },
  { text: '附件', value: 'appendixDescn', align: 'center', required: true },
  { text: '外观和功能检查', value: 'appearanceInspect', align: 'center', required: true },
  { text: '特殊要求', value: 'specialRequire', align: 'center', required: true },
  { text: '检校项目', value: 'measureContent', align: 'center', required: true },
])

// 多选发生改变时
const handleSelectionChange = (e: any) => {
  checkoutList.value = e
}

// 点击选择
const selectEquipment = (index: number) => {
  isMulti.value = false
  selectIndex.value = index
  selectEquipmentRef.value.initDialog(isMulti.value, form.value.customerId)
}

// 点击批量添加
const multiAdd = () => {
  if (!form.value.orderNo) {
    ElMessage.warning('请先选择任务单')
    return false
  }
  isMulti.value = true
  selectEquipmentRef.value.initDialog(isMulti.value, form.value.customerId)
}

// 增加行
const addRow = () => {
  if (!form.value.orderNo) {
    ElMessage.warning('请先选择任务单')
    return false
  }
  const checkResult = useCheckList(list.value, columns.value, '交接单列表') // 检查表格
  if (checkResult) {
    useSetAllRowReadable(list.value)
    list.value.push({
      id: '', // 主键
      equipmentNo: '', // 统一编号
      equipmentName: '', // 设备名称
      model: '', // 型号规格
      appendixDescn: '', // 附件
      appearanceInspect: '', // 外观和功能检查
      specialRequire: '', // 特殊要求
      measureContent: '', // 检校项目
      editable: true,
    })
  }
}

// 选好设备
const clickConfirmEquipment = (val: IDetailTableList[]) => {
  if (isMulti.value) { // 多选
    val.forEach((item: IDetailTableList) => {
      // 只添加列表里不存在的
      const index = list.value.findIndex((i: IDetailTableList) => item.sampleNo === i.equipmentNo)
      if (index === -1) {
        const param = {
          id: '', // 主键
          sampleId: item.sampleId,
          equipmentNo: item.sampleNo, // 统一编号
          equipmentName: item.sampleName, // 设备名称
          model: item.sampleModel, // 型号规格
          appendixDescn: item.appendixDescn, // 附件
          appearanceInspect: item.appearanceInspect, // 外观和功能检查
          specialRequire: item.specialRequire, // 特殊要求
          measureContent: item.measureContent, // 检校项目
          editable: true,
        }
        list.value.push(param)
      }
    })
  }
  else { // 单选
    const getValue = val[0]
    const index = list.value.findIndex((i: IDetailTableList) => val[0].sampleNo === i.equipmentNo)
    if (index !== -1) {
      ElMessage.warning('此设备已添加过')
      return
    }
    const param = {
      id: '', // 主键
      sampleId: getValue.sampleId,
      equipmentNo: getValue.sampleNo, // 统一编号
      equipmentName: getValue.sampleName, // 设备名称
      model: getValue.sampleModel, // 型号规格
      appendixDescn: getValue.appendixDescn, // 附件
      appearanceInspect: getValue.appearanceInspect, // 外观和功能检查
      specialRequire: getValue.specialRequire, // 特殊要求
      measureContent: getValue.measureContent, // 检校项目
      editable: true,
    }
    list.value.splice(selectIndex.value, 1, param)
  }
}

// 双击行
const rowDbClick = (row: IList) => {
  if (pageType.value !== 'detail') {
    useDoubleClickTableRow(row, list.value)
  }
}

// 删除行
const delRow = () => {
  if (checkoutList.value.length <= 0) {
    ElMessage({
      message: '请选中要删除的行',
      type: 'warning',
    })
  }
  else {
    checkoutList.value.forEach((item: IDetailTableList) => {
      list.value.forEach((element, index) => {
        if (element.equipmentNo === item.equipmentNo) {
          list.value.splice(index, 1)
        }
      })
    })
  }
}

// ---------------------------------------------按钮-------------------------------------------
const $router = useRouter()
// 关闭新增页面的回调
const close = () => {
  $router.back()
}

// 点击保存
const saveForm = (formEl: FormInstance | undefined) => {
  if (!list.value.length) {
    ElMessage.warning('要求交接单列表不能为空')
    return
  }
  if (!formEl) { return }
  formEl.validate((valid) => {
    if (valid) {
      ElMessageBox.confirm(
        '确认保存吗？',
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        },
      ).then(() => {
        const loading = ElLoading.service({
          lock: true,
          background: 'rgba(255, 255, 255, 0.8)',
        })
        const params = {
          ...form.value,
          delivererTel: form.value.delivererTel, // 委托方电话
          sampleIdList: list.value.map(item => item.sampleId),
        }
        if (pageType.value === 'edit') { // 编辑
          updateInterchangeReceipt(params).then(() => {
            ElMessage.success('已保存')
            loading.close()
            pageType.value = 'detail'
          }).catch(() => {
            loading.close()
          })
        }
        if (pageType.value === 'add') { // 新建
          addInterchangeReceipt(params).then((res) => {
            ElMessage.success('已保存')
            form.value.interchangeNo = res.data
            loading.close()
            pageType.value = 'detail'
          }).catch(() => {
            loading.close()
          })
        }
      })
    }
  })
}
// 点击编辑
const edit = () => {
  pageType.value = 'edit'
}
// 打印
const print = () => {
  ElMessage.info('敬请期待')
}
// ---------------------------------------------扫描--------------------------------------
const scanSampleRef = ref()
// 点击扫描收添加
const scan = () => {
  ElMessage.info('敬请期待')
  // if (!form.value.customerNo) {
  //   ElMessage.warning('请先选择委托方')
  //   return
  // }
  // 参数：不是标签绑定，委托书，业务场景
  // scanSampleRef.value.initDialog(false, '2', '', 'list', form.value.customerId)
}
// 扫描结束
const scanOver = (sampleList: any) => {
  sampleList.forEach((item: any) => {
    list.value.push({
      ...item,
      isExistSample: '1', // 是否存在样品库中 1存在、0不存在
      delId: getUid(),
    })
  })
  scanSampleRef.value.closeDialog()
}
// ------------------------------------------签名签章--------------------------------------------
const minioFileNameSignature = ref() // 签章图片
const minioFileNameAutograph = ref() // 签名图片

// 签章查询数据
const searchQuerySignature = ref({
  sealNo: '', // 印章编号
  sealName: '', // 印章名
  status: '',
  offset: 1,
  limit: 20,
})
// 签名查询数据
const searchQueryAutograph = ref({
  signNo: '', // 编号
  signName: '', // 名称
  signDirector: '', // 负责人
  signUserId: '', // 用户id
  createStartTime: '',
  createEndTime: '',
  limit: 20,
  offset: 1,
  signType: '',
  ids: [],
})
// 获取签章数据
const getSignatureList = () => {
  searchQuerySignature.value.sealNo = receiveCompanyList.value[0].value as string
  getSealInfoList(searchQuerySignature.value).then((res) => {
    if (res.code === 200) {
      minioFileNameSignature.value = res.data.rows[0].minioFileName
    }
  })
}

// 获取签名数据
const getAutographList = () => {
  searchQueryAutograph.value.signUserId = user.id // 创建人的签名
  listPageApi(searchQueryAutograph.value).then((res) => {
    if (res.code === 200) {
      if (res.data.rows.length) {
        minioFileNameAutograph.value = res.data.rows[0].minioFileName
      }
      else {
        minioFileNameAutograph.value = ''
      }
    }
  })
}
// ----------------------------------------------------------------------------------------------
// 获取详细信息
const getInfo = () => {
  const loading = ElLoading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.8)',
  })
  getInterchangeReceiptDetail({ id: infoId.value }).then((res) => {
    form.value = res.data
    list.value = res.data.sampleList.map((item: { sampleNo: string; sampleName: string; sampleModel: string }) => {
      return {
        ...item,
        equipmentNo: item.sampleNo, // 统一编号
        equipmentName: item.sampleName, // 设备名称
        model: item.sampleModel, // 型号规格
      }
    }) // 交接单列表
    form.value.isUrgent = `${res.data.isUrgent}`
    loading.close()
  }).catch(() => {
    loading.close()
  })
}

// ----------------------------------------------------钩子----------------------------------------------------
onMounted(async () => {
  getDict().then(() => {
    if (pageType.value === 'edit' || pageType.value === 'detail') { // 编辑和详情
      getInfo() // 获取详情信息
    }
    else { // 新建
      form.value.createUserId = user.id// 创建人id
      form.value.createUserName = user.name // 创建人
      form.value.createTime = dayjs().format('YYYY-MM-DD HH-mm:ss')// 创建时间
    }
    nextTick(() => {
      getSignatureList() // 获取签章数据
      getAutographList() // 获取签名
    })
  })
})
</script>

<template>
  <app-container>
    <detail-page :title="`交接单管理(${textMap[pageType]})`">
      <template #btns>
        <el-button v-if="pageType === 'detail'" type="primary" @click="print">
          打印
        </el-button>
        <el-button v-if="pageType === 'detail'" type="primary" @click="edit">
          编辑
        </el-button>
        <el-button v-if="pageType !== 'detail'" type="primary" @click="saveForm(ruleFormRef)">
          保存
        </el-button>
        <el-button type="info" @click="close">
          关闭
        </el-button>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="form"
        :label-width="130"
        label-position="right"
        :rules="rules"
      >
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="交接单编号:" prop="interchangeNo ">
              <el-input
                v-model="form.interchangeNo"
                :placeholder="pageType === 'detail' ? '' : '系统自动生成'"
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建人:">
              <el-input v-model="form.createUserName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间:">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
                disabled
                class="full-width-input"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="任务单编号" prop="orderNo">
              <el-input
                v-model="form.orderNo"
                :placeholder="pageType === 'detail' ? '' : '请选择'"
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
              >
                <template v-if="pageType !== 'detail'" #append>
                  <el-button size="small" @click="selectOrder">
                    选择
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="委托方" prop="customerName">
              <el-input
                v-model="form.customerName"
                :placeholder="pageType === 'detail' ? ' ' : '委托方'"
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="送检人:" prop="deliverer">
              <el-input
                v-model="form.deliverer"
                :placeholder="pageType === 'detail' ? '' : '请输入送检人'"
                :class="{ 'detail-input': pageType === 'detail' }"
                :disabled="pageType === 'detail'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="电话" prop="delivererTel">
              <el-input
                v-model="form.delivererTel"
                :placeholder="pageType === 'detail' ? '' : '请输入电话'"
                :class="{ 'detail-input': pageType === 'detail' }"
                :disabled="pageType === 'detail'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="要求检完时间:" prop="requireOverTime">
              <el-date-picker
                v-model="form.requireOverTime"
                type="datetime"
                :placeholder="pageType === 'detail' ? ' ' : '要求检完时间'"
                class="full-width-input"
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委托方地址:">
              <el-input
                v-model="form.customerAddress"
                type="textarea"
                :placeholder="pageType === 'detail' ? ' ' : '委托方地址'"
                autosize
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否加急:" prop="isUrgent">
              <el-select v-model="form.isUrgent" :placeholder="pageType === 'detail' ? ' ' : '是否加急'" disabled class="full-width-input">
                <el-option
                  v-for="i in isUrgentList"
                  :key="i.value"
                  :label="i.name"
                  :value="i.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="检修专业:" prop="maintainMajor">
              <el-input
                v-model="form.maintainMajor"
                :placeholder="pageType === 'detail' ? ' ' : '检修专业'"
                :class="{ 'detail-input': pageType === 'detail' }"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </detail-page>
    <!-- 表格 -->
    <detail-block title="交接单列表">
      <template #btns>
        <el-button v-if="pageType === 'add'" type="primary" @click="scan">
          扫描添加
        </el-button>
        <el-button v-if="pageType === 'add'" type="primary" @click="multiAdd">
          批量添加
        </el-button>
        <el-button v-if="pageType === 'add'" type="primary" @click="addRow">
          增加行
        </el-button>
        <el-button v-if="pageType === 'add'" type="info" @click="delRow">
          删除行
        </el-button>
      </template>
      <el-table
        :data="list"
        border
        style="width: 100%;"
        max-height="600"
        @selection-change="handleSelectionChange"
        @row-dblclick="rowDbClick"
      >
        <el-table-column v-if="pageType === 'add'" type="selection" width="38" />
        <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"
          :width="item.width"
          show-overflow-tooltip
          align="center"
        >
          <template #header>
            <span v-show="item.required" style="color: red;">*</span><span>{{ item.text }}</span>
          </template>
          <template v-if="pageType !== 'detail'" #default="scope">
            <el-input
              v-if="scope.row.editable && item.value === 'equipmentNo'"
              v-model="scope.row[item.value]"
              placeholder="请选择"
              :disabled="item.disabled"
            >
              <template #append>
                <el-button size="small" @click="selectEquipment(scope.$index)">
                  选择
                </el-button>
              </template>
            </el-input>
            <span v-if="!scope.row.editable">{{ scope.row[item.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </detail-block>
    <!-- 签章部分 -->
    <div v-if="pageType === 'detail'" class="receipt-signature">
      <div class="content">
        <span class="title">甲方</span>
        <div class="deliverer-time">
          <span class="text-title">送检人:</span>
          <span class="time">时ㅤ间:</span>
        </div>
      </div>
      <div class="content">
        <span class="title">乙方</span>
        <div class="deliverer-time">
          <div style="display: flex;">
            <span class="text-title">接收人:</span>
            <show-photo style="position: absolute;left: 220px; top: -40px;" :minio-file-name="minioFileNameAutograph" />
          </div>
          <div style="margin-top: 30px;">
            <span class="time" style="margin-top: 20px;">时ㅤ间: </span>
            <span class="text">{{ form.createTime }}</span>
            <show-photo style="position: absolute;left: 360px; top: 1px;" :minio-file-name="minioFileNameSignature" />
          </div>
        </div>
      </div>
    </div>
    <!-- 选择任务单 -->
    <select-order-dialog ref="selectOrderRef" @confirm="clickConfirmOrder" />
    <!-- 选择设备组件 -->
    <select-equipment-dialog ref="selectEquipmentRef" :is-multi="isMulti" @confirm="clickConfirmEquipment" />
    <!-- 扫描 -->
    <scan-sample-dialog ref="scanSampleRef" title="扫描添加设备" @confirm="scanOver" />
  </app-container>
</template>

<style lang="scss" scoped>
.receipt-signature {
  display: flex;
  justify-content: space-around;
  // padding: 50px 250px 50px 150px;
  padding: 50px;
  font-size: 28px;
  color: #000;
  font-weight: 600;

  .content {
    display: flex;
    position: relative;

    .img {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 2;
    }

    .img-name {
      position: absolute;
      left: 220px;
      top: -30px;
      z-index: 1;
    }

    .title {
      margin-right: 32px;
      letter-spacing: 6px;
    }

    .deliverer-time {
      display: flex;
      flex-direction: column;

      .text-title {
        letter-spacing: 2px;
      }

      .time {
        margin-top: 30px;
        letter-spacing: 2px;
      }

      .text {
        font-size: 24px;
        font-weight: 500;
        margin-left: 10px;
      }
    }
  }
}
</style>

