<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="projectName">
          <el-input v-model="queryParams.projectName" 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="materialName">
          <el-input v-model="queryParams.materialName" 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="ownerName">
          <el-input
            v-model="queryParams.ownerName"
            placeholder="请输入责任人"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="采购状态" prop="mplanStatus">
          <el-select v-model="queryParams.mplanStatus" placeholder="请选择标准件状态"  class="!w-240px">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_STANDARD_BUY_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="物料需求单号" prop="projectMaterialPlanNo">
          <el-input v-model="queryParams.projectMaterialPlanNo" placeholder="请输入物料需求单号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>


        <el-form-item style="margin-left:30px">
          <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-button
            type="success"
            plain
            @click="handleExportDetail"
            :loading="exportLoading"
          >
            <Icon icon="ep:download" 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="opearteForm(0)" type="success"  size="large">送 审</el-button>
        <el-button  style="margin-left: 20px" @click="opearteForm(1)" type="warning" 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
:row-key="getRowKeys" ref="multipleTable"
  @selection-change="handleSelectionChange" v-loading="loading" :data="list" :show-overflow-tooltip="true"  class="hl-table">
  <el-table-column
    type="selection"
    width="55"
    :reserve-selection="true"
  />
        <el-table-column fixed label="序号" align="center" type="index" width="60" />
        <el-table-column label="客户简称" align="center" prop="customerBriefName" min-width="120"  fixed/>
        <el-table-column label="项目名称" align="center" prop="projectName" min-width="150" fixed/>
        <el-table-column label="子项目名称" align="center" prop="projectSubName" min-width="150" fixed/>
        <el-table-column label="零件名称" align="center" prop="matName" min-width="150" />
        <el-table-column label="材质" align="center" prop="compositionName" min-width="150" />
        <el-table-column label="规格型号" align="center" prop="matSpec" min-width="150" />
        <el-table-column label="图号" align="center" prop="blueprintNo" min-width="80" />
        <el-table-column label="需求数量" align="center" prop="boomAmount" min-width="100" />

        <el-table-column label="单位" align="center" prop="matUnit" min-width="100">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_MATERIAL_UNIT" :value="scope.row.matUnit" />
          </template>
        </el-table-column>
        <el-table-column label="采购数量" align="center" prop="purchaseAmount" min-width="180" >
            <template #default="scope">
            <el-input-number @blur="updateRow(1,scope.row)" v-model="scope.row.purchaseAmount" :precision="2" :step="0.01" />
          </template>
        </el-table-column>
        <el-table-column label="物料编码" align="center" prop="matCode" min-width="180" >
                      <template #default="scope">
              <el-input v-model="scope.row.matCode"  disabled>
                <template #suffix>
                  <Icon @click="serachLog(scope.row)" icon="ep:search"  color="primary"/>
                </template>
              </el-input>
                     </template>
        </el-table-column>
        <el-table-column label="库存量" align="center" prop="matRest" min-width="120" />
        <el-table-column label="供应商" align="center" prop="supplierId" min-width="180" >
       <template #default="scope">
                  <el-select
            v-model="scope.row.supplierId"
            size="large"
            clearable
            @change="e => changeSupp(e,scope.row)"
            style="width: 165 px"
          >
            <el-option
              v-for="item in supplierList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
       </template>

        </el-table-column>
       <el-table-column label="预估总价" align="center" prop="estimatedPrice" min-width="180" >
           <template #default="scope">
            <el-input-number @blur="updateRow(3,scope.row)" v-model="scope.row.estimatedPrice" :precision="2" :step="0.01" />
          </template>
      </el-table-column>
        <el-table-column min-width="200px" align="center" fixed="right">
          <template #header><span class="hl-table_header">*</span>预计到货日期</template>
          <template #default="{ row, $index }">
            <el-form-item :prop="`${$index}.arriveTime`" class="mb-0px!" >
              <el-date-picker   @change="e => changeDate(e,row)" class="!w-265px" v-model="row.arriveTime" type="date"  value-format="x" placeholder="预计到货日期" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="mplanStatusName" min-width="80" />
        <el-table-column label="采购单号" align="center" prop="purchaseNo" min-width="180" fixed="right"/>
      </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>
    <div class="hl-footer text-center">

    </div>
  </el-card>
    <matLog ref="matLogRef" @success="getMat" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import * as materialPlanBoomApi from '@/api/heli/materialPlanBoom'
import { useCommonStateWithOut } from '@/store/modules/common'
import { useUserStore } from '@/store/modules/user'
import matLog from './storageLog.vue'
import * as supplierApi from '@/api/heli/supplier'
import UserSelect from "@/views/heli/materialplan/userSelectNew.vue";
import {inject, ref} from "vue";
import { update } from 'lodash-es'
import { Hash } from 'crypto'
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const userStore = useUserStore()
const username = userStore.getUser.nickname
defineOptions({ name: 'Standard' })
const reload: any = inject('reload')
const commonStore = useCommonStateWithOut()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const matLogRef = ref()
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  planCode: undefined,
  projectCode: undefined,
  customerName: undefined,
  projectName: undefined,
  projectSubName: undefined,
  version: undefined,
  bomStatus: undefined,
  remark: undefined,
  mplanStatus: 0,
  createTime: [],
  ownerName:username,
  materialName:undefined,
  projectMaterialPlanNo:undefined
})

const queryParams1 = reactive({
  pageNo: 1,
  pageSize: 999,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await materialPlanBoomApi.getStandardBuyPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const ids = ref([])
const multipleTable=ref()
const getRowKeys=(row)=>{
  return row.id;
}

const handleSelectionChange = (selection) => {
  ids.value = selection;
  console.log(ids.value)
}

/** 导出按钮操作 */
const handleExportDetail = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await materialPlanBoomApi.exportMaterialPlanBoom(queryParams)
    download.excel(data, '标准件物料需求计划.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const getMat = async (rowids,amount,ids) => {
  //formData.value.boomItemDOList = arrBoom
  for(var i = 0 ; i < list.value.length ; i ++){
    if(list.value[i].id == rowids){
      list.value[i].matRest = amount
      list.value[i].materialId = ids
      await updateRow(2,list.value[i]);
      break;
    }
  }
}
const changeSupp = async(e,row) => {
  if(e){
    await updateRow(4,row)
  }
}
const changeDate = async(e,row) => {
  if(e){
    await updateRow(5,row)
  }
}
const serachLog = (row) => {
  matLogRef.value.open(row.id,row.matCode)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const opearteForm = async(type) =>{
  if(ids.value != null && ids.value.length > 0){
  if(type == 0){
    //送审
      var isError = false;
      var sets = new Set();
      for(var i = 0 ; i<ids.value.length ;i++){
        var row = ids.value[i];
        if(!row.purchaseAmount ){
          message.error('零件：'+row.matName+ '采购数量不能为空！')
          return
        } else if( row.purchaseAmount == 0){
            message.error('零件：'+row.matName+ '采购数量不能为0！')
          return
        } else if(!row.supplierId){
           message.error('零件：'+row.matName+ '供应商不能为空！')
           return
        } else if(!row.estimatedPrice){
         message.error('零件：'+row.matName+ '预估总价不能为空')
          return
        } else if( row.estimatedPrice == 0){
            message.error('零件：'+row.matName+ '预估总价不能为0！')
          return
        }   else if(!row.arriveTime ){
         message.error('零件：'+row.matName+ '预计到货日期不能为空')
          return
        }
        if( !sets.has(row.projectId) || sets == null ){
            sets.add(row.projectId)
        }
      }
      if(sets != null && sets.size > 1){
         message.error('不通项目不能同时生成采购单');
          return
      }

      var form = {
        id:1,
        projectMaterialPlanId:1,
        boomDetailId:1,
        projectPlanSubId:1,
        materialPlanBoomDOList:ids.value,
        buyType:0
      }
      await materialPlanBoomApi.updateMaterialPlanBoom(form);
       message.success("送审成功")
  }else{
    //去库存
      for(var i = 0 ; i<ids.value.length ;i++){
          var row = ids.value[i];
          if(!row.materialId){
            message.error('请先查询库存量后进行操作')
          return
          }
      }
     var form = {
        id:1,
        projectMaterialPlanId:1,
        boomDetailId:1,
        projectPlanSubId:1,
        materialPlanBoomDOList:ids.value,
        buyType:2
      }
       await materialPlanBoomApi.updateMaterialPlanBoom(form);
              message.success("去库存成功")
  }
}else{
     message.error("至少选择一项后进行操作！")
    return ;
  }
}
const updateRow =  async(type,row) =>{
    if(type == 1){
      if(row.purchaseAmount != null){
         await  materialPlanBoomApi.updateMaterialPlanBoom(row)
      }

    }else if(type == 2){
      if(row.matRest != null){
         await  materialPlanBoomApi.updateMaterialPlanBoom(row)
      }
    }else if(type == 3){
      if(row.estimatedPrice != null){
         await  materialPlanBoomApi.updateMaterialPlanBoom(row)
      }
    }else if(type == 4){
      if(row.supplierId){
         await  materialPlanBoomApi.updateMaterialPlanBoom(row)
      }
    }else if(type == 5){
      if(row.arriveTime){
         await  materialPlanBoomApi.updateMaterialPlanBoom(row)
      }
    }

}


const supplierList = ref([])
const supplier = async() =>{
   var data  = await supplierApi.getSupplierPage(queryParams1);
   supplierList.value =data.list
   //console.log(supplierList/)
}
const handleSelectedUser = (currentIndex, newValue: any) => {
  list.value[currentIndex].duEmpId = newValue?.id
}
const submitForm = async () => {
  const filteredList = list.value.filter(item => item.duEmpId !== null && item.duEmpId !== undefined);
  if (filteredList.length <= 0) {
    message.error("提交明细不能为空，请确认");
    return;
  }
    const firstProjectSubId = filteredList[0].projectSubId;
    for (let i = 1; i < filteredList.length; i++) {
      if (filteredList[i].projectSubId !== firstProjectSubId) {
        message.error("标准件明细不属于同一个子项目，请确认");
        return;
      }
    }

  for (let i = 0; i < filteredList.length; i++) {
    if (filteredList[i].projectMaterialPlanNo!=null){
      message.error("零件"+filteredList[i].materialName+"已生成物料需求计划,请确认")
      return
    }
    if (filteredList[i].boomArriveDate==null){
      message.error("零件"+filteredList[i].materialName+"要求完成日期为空,请确认")
      return
    }
    if (filteredList[i].duEmpId==null){
      message.error("零件"+filteredList[i].materialName+"责任人为空,请确认")
      return
    }
  }
  formLoading.value = true
  try {
      await ProcessBomApi.submitForm(filteredList)
      message.success("提交成功")
    // 发送操作成功的事件
    getList()
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const withdraw = async () => {
  if (queryParams.projectMaterialPlanNo==null){
    message.error("物料需求单号为空,不允许撤回")
    return

  }
  await ProcessBomApi.withdraw(queryParams.projectMaterialPlanNo)
  message.success("撤回成功")
  // 发送操作成功的事件
  getList()
  emit('success')

}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}



/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProcessBomApi.exportProcessBom(queryParams)
    download.excel(data, '工艺bom.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}


/** 初始化 **/
onMounted( async() => {
  await supplier()
  await getList()

})
</script>
<style>
/* 占位样式 */
</style>
