<template>
  <view class="page-wraper">
    <view class="" v-if="detailSource.subList.length == 0">
      <com-blank-view @goScan="goScan(true)"></com-blank-view>
    </view>
    <view v-else class="page-wraper">
      <view class="page-main">
        <comDeliverRequestCreator ref="deliverRequest" :dataContent="detailSource"> </comDeliverRequestCreator>
        <button class="btn_add" type="primary" @click="goScan(false)">+去添加</button>
      </view>

      <div class="btn_bottom">
        <view class="" style="flex: 1">
          <button class="btn_single_commit" hover-class="btn_commit_after" @click="submit()">提交</button>
        </view>
      </div>
      <com-message ref="comMessageRef" />
    </view>
  </view>
  <comDeliverRequestPopup ref="comDeliverRequestPopupRef" @confirm="requestConfirm"></comDeliverRequestPopup>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, nextTick, onMounted } from 'vue'
import { onLoad, onNavigationBarButtonTap, onPullDownRefresh } from '@dcloudio/uni-app'
import { deliverRequestSubmit, issueRecordSubmit } from '@/api/request2.js'

import { calc } from '@/common/calc.js'

import { goHome, getRemoveOption, getISODateTime } from '@/common/basic.js'

import { getDataSource } from '@/pages/issue/js/issue.js'

import comBlankView from '@/mycomponents/common/comBlankView.vue'
import comDeliverRequestPopup from '@/pages/deliver/coms/comDeliverRequestPopup.vue'
import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import comDeliverRequestCreator from '@/pages/deliver/coms/comDeliverRequestCreator.vue'

const { proxy } = getCurrentInstance()
const subList = ref([]) // 接口返回的任务subList
const detailSource = ref({
  subList: []
}) // 绑定在页面上的数据源
const detailOptions = ref([])
const scanOptions = ref([])
const fromType = ref('')
const dataContent = ref({})
const comDeliverRequestPopupRef = ref()
const deliverRequest = ref()
const comMessageRef = ref()
onMounted(() => {
  goScan(true)
})
onLoad((option) => {
  fromType.value = option.fromType
})
const goScan = (editPosition) => {
  comDeliverRequestPopupRef.value.openRequestPopup(editPosition)
}

// 确定需求信息
const requestConfirm = (action, item) => {
  if (detailSource.value.subList.length == 0) {
    detailSource.value = {
      customerCode: item.customerCode,
      customerName: item.customerName,
      totalQty: 0,
      subList: []
    }
    var subItem = {
      itemCode: item.itemCode,
      itemName: item.itemName,
      qty: item.qty,
      uom: item.uom
    }
    detailSource.value.subList.push(subItem)
  } else {
    const result = detailSource.value.subList.filter((res) => {
      if (res.itemCode == item.itemCode && res.productionLineCode == item.productionLineCode && res.workStationCode == item.workStationCode) {
        return res
      }
    })

    // 没有添加数据
    if (result.length == 0) {
      var subItem = {
        productionLineCode: item.productionLineCode,
        productionLineName: item.productionLineName,
        workStationCode: item.workStationCode,
        workStationName: item.workStationName, // 工位
        itemCode: item.itemCode,
        itemName: item.itemName,
        qty: item.qty,
        uom: item.uom
      }

      detailSource.value.subList.push(subItem)
    } else {
      // 有了更新数据
      result[0].qty = calc.add(result[0].qty, item.qty)
    }
  }
  caclcQty()
  if (deliverRequest.value != undefined) {
    deliverRequest.value.update()
  }
}

const caclcQty = () => {
  let totalQty = 0
  detailSource.value.subList.forEach((res) => {
    totalQty = calc.add(totalQty, res.qty)
  })
  detailSource.value.totalQty = totalQty
}

const setParams = () => {
  return detailSource.value
}
const submit = () => {
  proxy.$modal.loading('提交中....')
  const params = setRequestParams()
  deliverRequestSubmit(params)
    .then((res) => {
      uni.hideLoading()
      if (res.data) {
        showCommitSuccessMessage(`提交成功<br>生成成品发货申请${res.data}`)
      } else {
        showErrorMessage(`提交失败[${res.msg}]`)
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}
const setRequestParams = () => {
  const requestContent = {}
  requestContent.customerCode = detailSource.value.customerCode
  const subList = []
  detailSource.value.subList.forEach((detail) => {
    subList.push(detail)
  })
  requestContent.subList = subList
  return requestContent
}
const showCommitSuccessMessage = (hint) => {
  comMessageRef.value.showSuccessMessage(hint, (res) => {
    uni.navigateTo({
      url: './deliverRequest'
    })
  })
}
const showErrorMessage = (message) => {
  comMessageRef.value.showErrorMessage(message, (res) => {
    if (res) {
    }
  })
}
</script>

<style></style>
