<template>
  <el-dialog
    :model-value="modelValue"
    :title="`修正工单实际投料(${props?.['date']})`"
    fullscreen
    :before-close="()=>{emits('update:modelValue', false )}"
  >
    <el-button
      type="primary"
      style="width:80px; height: 40px;margin-bottom: 20px"
      @click="handleUpdate"
    >更新</el-button>

    <el-button
      type="primary"
      style="width:80px; height: 40px;margin-bottom: 20px"
      @click="handleSearch"
    >定位</el-button>

    <el-button
      type="primary"
      style="width:80px; height: 40px;margin-bottom: 20px"
      @click="emits('update:modelValue', false )"
    >退出</el-button>

    <el-table
      ref="table"
      highlight-current-row
      show-overflow-tooltip
      border
      :data="tableData"
      :row-style="{ height: '25px' }"
      :cell-style="{ padding: '0px' }"
      :header-row-style="{ height: '20px' }"
      :header-cell-style="{ padding: '0px' }"
      style="width: 100%;height: 75vh;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="工单编号"
        prop="Gd_gdbh"
        width="100"
      />
      <el-table-column
        label="印件号"
        prop="yj_Yjno"
        width="100"
      />
      <el-table-column
        label="印件代号"
        prop="yj_yjdh"
        width="100"
      />
      <el-table-column
        label="开数*联数"
        prop="yj_ls"
        width="120"
      />
      <el-table-column
        label="物料代码"
        prop="yj_zzdh"
        width="100"
      />
      <el-table-column
        label="物料名称"
        prop="BOM_物料名称"
        width="250"
      />
      <el-table-column
        label="规格"
        prop="yj_tlgg"
        width="100"
      />
      <el-table-column
        label="领用单位"
        prop="BOM_投料单位"
        width="100"
      />
      <el-table-column
        label="订单数量（万张）"
        prop="订单数量"
        width="150"
      />
      <el-table-column
        label="当前投料"
        prop="yj_平张投料"
        width="100"
      />
      <el-table-column
        label="实际用量"
        prop="BOM_实际用量"
        width="100"
      />
      <el-table-column
        label="换算率"
        prop=""
        width="100"
      />
      <el-table-column
        label="折算投料（万张）"
        width="150"
      >
        <template #default="{ row }">
          <el-input v-model="row['实际投料']" />
        </template>
      </el-table-column>
      <el-table-column
        label="投料确认"
        prop="投料确认"
        width="200"
      />

    </el-table>

  </el-dialog>

</template>

<script>
export default {
  name: 'Xzgdtl',
}
// 5.1修正工单实际投料-获取列表
import service from '@/utils/request'

const getOrderFeedList = (params) => {
  return service({
    url: '/mes_server/order_super_loss/getOrderFeedList',
    method: 'get',
    params
  })
}

// 5.2更新工单实际投料
const updateOrderFeed = (data) => {
  return service({
    url: '/mes_server/order_super_loss/updateOrderFeed',
    method: 'post',
    data
  })
}
</script>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { watch, ref, reactive, defineProps, defineEmits, onBeforeUnmount } from 'vue'

const props = defineProps(['modelValue', 'date'])
const emits = defineEmits(['update:modelValue'])
const tableData = ref([])
const table = ref(null)
const multipleSelection = ref([])
const getTable = async() => {
  let params
  if (props?.['date']) {
    const { date } = props
    params = {
      year: date.slice(0, 4),
      month: date.slice(4, 6),
    }
  } else {
    return
  }
  const res = await getOrderFeedList(params)
  if (res['code'] === 0) {
    const { data } = res
    tableData.value = data
  }
}
getTable()

const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const handleSearch = () => {
  ElMessageBox.prompt('输入工单编号', '定位', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
    .then(({ value }) => {
      console.log(value)
      if (!value) {
        getTable()
      } else {
        tableData.value = tableData.value.filter(item => {
          return item['Gd_gdbh'] === value ?? item
        })
      }
      ElMessage({
        type: 'success',
        message: '定位',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消',
      })
    })
}

const handleUpdate = async() => {
  if (multipleSelection.value.length === 0) {
    console.log('未选择行')
    return
  }
  const params = multipleSelection.value.map(item => ({
    UniqId: item['UniqId'],
    number: item['实际投料'],
  }))
  const res = await updateOrderFeed(params)
  if (res['code'] === 0) {
    ElMessage({
      type: 'success',
      message: '更新成功',
    })
  }
}
</script>

<style scoped>

</style>
