<template>
  <el-card class="hl-card">
    <template #header>
      <span>零件采购收货</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="120px">
        <el-form-item label="采购订单号" prop="purchaseNo">
          <el-input
v-model="queryParams.purchaseNo" placeholder="采购订单号" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>

        <el-form-item label="项目编码" prop="projectCode">
          <el-input
            v-model="queryParams.projectCode" placeholder="请输入项目编码" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="项目简称" prop="projectName">
          <el-input
            v-model="queryParams.projectName" placeholder="请输入项目简称" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="客户简称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户简称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="子项目简称" prop="projectSubName">
          <el-input
            v-model="queryParams.projectSubName" placeholder="子项目简称" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="零件" prop="boomName">
          <el-input
            v-model="queryParams.boomName" placeholder="请输入零件" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="图号" prop="blueprintNo">
          <el-input
            v-model="queryParams.blueprintNo" placeholder="请输入图号" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="工序" prop="procedureName">
          <el-input
            v-model="queryParams.procedureName" placeholder="请输入工序" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input
v-model="queryParams.supplierName" placeholder="供应商" clearable @keyup.enter="handleQuery"
            class="!w-240px" />
        </el-form-item>
        <el-form-item label="责任人" prop="username">
          <el-input
            v-model="queryParams.username" placeholder="责任人" clearable @keyup.enter="handleQuery"     :readonly="isReadOnly"
          class="!w-240px" />
        </el-form-item>
        <el-form-item label="收货状态" prop="receivingStatus">
          <el-select v-model="queryParams.receivingStatus" placeholder="下拉选择" clearable class="!w-240px">
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PURCHASE_RECEIVING_STATUS)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>


        <el-form-item style="margin-left:50px">
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> 重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <el-card class="hl-card-info">
      <template #header>
        <div class="hl-card-info-icona"></div><span class="hl-card-info-text">订单明细</span>
        <el-button  style="margin-left: 20px" @click="receiveGoods" type="success"  size="large">收货</el-button>
      </template>
      <el-row>
        <el-col>
          <el-card class="hl-incard">
            <el-form ref="subFormRef" :model="list"  v-loading="formLoading" label-width="0" >
              <el-table
                v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table" ref="multipleTableRef" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" />
              <el-table-column label="收货状态" align="center" prop="receivingStatus"  min-width="120">
                <template #default="scope">
                  <dict-tag :type="DICT_TYPE.HELI_PURCHASE_RECEIVING_STATUS" :value="scope.row.receivingStatus" />
                </template>
              </el-table-column>
              <el-table-column prop="projectSubCode" min-width="150" label="子项目编码" align="center"/>
              <el-table-column prop="blueprintNo" min-width="100" label="图号" align="center"/>
              <el-table-column prop="boomName" min-width="120" label="零件名称" align="center"/>
                <el-table-column prop="supplierName" min-width="120" label="供应商" align="center"/>
              <el-table-column prop="procedureName" min-width="120" label="工序" align="center"/>
              <el-table-column prop="compositionName" min-width="100" label="材质" align="center"/>
              <el-table-column prop="purchaseAmount" min-width="100" label="采购数量" align="center" />
              <el-table-column prop="purchaseRemAmount" min-width="100" label="剩余数量" align="center"/>
              <el-table-column prop="purchaseAmount" min-width="180" align="center">
                <template #header><span class="hl-table_header">*</span>入库数量</template>
                <template #default="scope">
                  <el-form-item :prop="`${scope.$index}.purchaseRemAmounts`"  class="mb-0px!">
                    <el-input-number style="width: 100%" v-model="scope.row.purchaseRemAmounts" placeholder="入库数量"  :min="0" :precision="2"  />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column prop="estimatedPrice" min-width="180" label="预估总价(元)" align="center">
                <template #header><span class="hl-table_header">*</span>预估总价(元)</template>
                <template #default="scope">
                  <el-form-item :prop="`${scope.$index}.estimatedPrice`" class="mb-0px!">
                    <el-input-number
                      style="width: 100%" v-model="scope.row.estimatedPrice" placeholder="预估总价"  :min="0" :precision="2"  
                      @input="(val)=>handleInput(val,scope.row)"
                      @keyup.enter="(e)=>handleEnter(e,scope.row)"
                      :id="'estimatedPrice'+scope.row.id"
                      />
                  </el-form-item>
                </template>
              </el-table-column>
                <el-table-column prop="createTime" min-width="150" label="订单生成日期" align="center" :formatter="dateFormatter1"/>
                <el-table-column prop="requireTime" min-width="150" label="需要完成日期" align="center" :formatter="dateFormatter1"/>
<!--              <el-table-column prop="arriveTime" min-width="150" label="收货日期" align="center" :formatter="dateFormatter1" />-->
              <el-table-column prop="description" min-width="150" label="技术要求" align="center"/>
              <el-table-column prop="theWeight" min-width="100" label="理论重量" align="center"/>
              <el-table-column label="操作" align="center" fixed="right" min-width="200">
                <template #default="scope">
                  <el-button link type="primary"  v-if="scope.row.receivingStatus!=3" @click="receiveGood(scope.row)">
                    收货
                  </el-button>
                  <el-button link type="primary" @click="updateFrom(scope.row)" v-if="scope.row.receivingStatus==3">
                    总价修改
                  </el-button>
                </template>
              </el-table-column>
              </el-table>
              <!-- 分页 -->
              <Pagination
                :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
                @pagination="getList" />
            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </el-card>
  </el-card>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import * as PurchaseOrderApi from '@/api/heli/purchaseorder'
import * as UserApi from '@/api/system/user'
import { ElTable } from 'element-plus'
import {useUserStore} from "@/store/modules/user";
import {dateFormatter1} from "@/utils/formatTime";
import * as PurchaseOrderNoDetailApi from "@/api/heli/purchaseordernodetail";
defineOptions({ name: 'PurchaseordernopartReceived' })
const userStore = useUserStore()
const username = userStore.getUser.nickname
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const loading = ref(true) // 列表的加载中
const isReadOnly = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  ids: undefined,
  purchaseNo: undefined,
  supplierId: undefined,
  contractNo: undefined,
  purchaseType: undefined,
  projectMaterialPlanId: undefined,
  goodsType: 2,
  currencyType: undefined,
  taxRatio: undefined,
  estimatedPrice: undefined,
  actualPrice: undefined,
  status: undefined,
  submitUserId: undefined,
  submitTime: [],
  auditor: undefined,
  auditTime: [],
  description: undefined,
  creator: undefined,
  createTime: undefined,
  receivingStatus:1,
  projectName:undefined,
  projectSubName:undefined,
  username:username,
  supplierName:undefined,
  boomName:undefined,
  blueprintNo:undefined,
  procedureName:undefined,
  customerName:undefined,
  projectCode:undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const handleInput = (val: number, row: PurchaseOrderNoDetailApi.PurchaseOrderNoDetailVO) => {
  console.log(val, 'val');
  //输入判断当前行是否选中如无将当前行选中
  if (multipleSelection.value.indexOf(row) == -1) {
    multipleSelection.value.push(row);
    multipleTableRef.value.toggleRowSelection(row)

  };

}
const handleEnter = (e: KeyboardEvent, row: PurchaseOrderNoDetailApi.PurchaseOrderNoDetailVO) => {
  //敲下回车时切换到下一行的预估总价 如果为0.00将其=null
  console.log(e, 'e');
  //判断当前行是否为最后一行如为最后一行则不切换
  if (list.value.indexOf(row) == list.value.length - 1) {
    return
  }
  const index = list.value.findIndex((item) => item.id == row.id)
  //切换到下一行
  const input = document.getElementById("estimatedPrice" + list.value[index + 1].id);
  if (input) {
    input.focus();
    if (list.value[index + 1].estimatedPrice == 0.00) {
      list.value[index + 1].estimatedPrice = ''
    }
  }
  console.log(input, 'input');


}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.ids = undefined
    const data = await PurchaseOrderNoDetailApi.getReceiveGoodDetail(queryParams)

    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const updateFrom = async (row) => {
  await PurchaseOrderNoDetailApi.updateFrom(row)
  loading.value = true;

  message.success("修改成功");
  getList(); // 确保刷新完成
  emit('success');
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}



/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const multipleTableRef: any = ref<InstanceType<typeof ElTable>>()
const multipleSelection: any = ref([])

const handleSelectionChange = (val: PurchaseOrderApi.PurchaseOrderVO[]) => {
  multipleSelection.value = val

}
const receiveGood = async (row) => {
  multipleTableRef.value.clearSelection()
  multipleTableRef.value.toggleRowSelection(row)
  multipleSelection.value.indexOf(row) == -1 && multipleSelection.value.push(row) ; 
  receiveGoods();
}


const receiveGoods = async () => {
  try {

    const list = multipleSelection.value|| []; // 安全获取数据
    // 1. 检查空数据
    if (!list || list.length==null||list.length==0) {
      message.error("采购单信息未选择，请确认");
      return;
    }
    for (let i = 0; i < list.length; i++) {
      if (list[i].purchaseRemAmounts==null||list[i].purchaseRemAmounts==0){
        message.error("零件"+list[i].boomName+"入库数量为空或数量为0,请确认")
        return
      }
      if (list[i].estimatedPrice==null||list[i].estimatedPrice==''){
        message.error("零件"+list[i].boomName+"预估总价为空,请确认")
        return
      }
    }
    await PurchaseOrderNoDetailApi.verification(list)
    const res = await Promise.race([
      PurchaseOrderNoDetailApi.receiveGoods(list),
      new Promise((_, reject) =>
        setTimeout(() => reject(new Error("请求超时")), 30000)
      )
    ]);
    loading.value = true;

    message.success("收货成功");
    getList(); // 确保刷新完成
    emit('success');
  } catch (error) {
  } finally {
    loading.value = false;
  }
}

/** 初始化 **/
const route = useRoute()
const routeValue = ref('')
onMounted(async () => {
  const newVar = await UserApi.isAdministrator();
  if (newVar==false){
    isReadOnly.value=true
  }
  console.log(newVar)
   getList()
})
</script>
