<template>
  <el-dialog title="炉号修改" v-model="visible" width="400px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="furnaceRef" v-loading="loading" class="master-container" :model="furnaceInfo" :rules="rules"
        label-width="80px">
        <el-form-item label="炉号" prop="furnaceNumber">
          <el-select-v2 v-model="furnaceInfo.furnaceNumber" :options="furnaceList" placeholder="请选择炉号" style="width: 200px"
            @change="handleFurnaceChange" />
        </el-form-item>
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="计划单号" prop="productionPlanNo">
          <span>{{ furnaceInfo.productionPlanNo? furnaceInfo.productionPlanNo:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="序号" prop="lineNumber">
          <span>{{ furnaceInfo.lineNumber? furnaceInfo.lineNumber:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="厂家" prop="factory">
          <span>{{ furnaceInfo.factory? furnaceInfo.factory:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="牌号" prop="brandNumber">
          <span>{{ furnaceInfo.brandNumber? furnaceInfo.brandNumber:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="规格" prop="spec">
          <span>{{ furnaceInfo.spec? furnaceInfo.spec:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="形状" prop="shape">
          <span>{{ furnaceInfo.shape? furnaceInfo.shape:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="材料直径" prop="diameter">
          <span>{{ furnaceInfo.diameter? furnaceInfo.diameter:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="来料编码" prop="rawMaterialCode">
          <span>{{ furnaceInfo.rawMaterialCode? furnaceInfo.rawMaterialCode:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="来料日期" prop="incomingDate">
          <span>{{ furnaceInfo.incomingDate? furnaceInfo.incomingDate:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="材质1" prop="firstTechnicalRequirement">
          <span>{{ furnaceInfo.firstTechnicalRequirement? furnaceInfo.firstTechnicalRequirement:'-' }}</span>
        </el-form-item> 
        <el-form-item v-if="Object.keys(furnaceInfo).length > 0" label="材质2" prop="secondTechnicalRequirement">
          <span>{{ furnaceInfo.secondTechnicalRequirement? furnaceInfo.secondTechnicalRequirement:'-' }}</span>
        </el-form-item> 
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" icon="Check" @click="handleSave">确 定</el-button>
      <el-button icon="Close" @click="handleCancel">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { getFurnaceNoInfo } from "@/api/business/furnaceNoInfo.js";
import {updateDayworkFurnace} from '@/api/business/daywork.js'
const { proxy } = getCurrentInstance();
const loading = ref(false)
const visible = ref(false);
const furnaceInfoList = ref([])
const furnaceList = ref([])
const detailInfo= ref({})
const furnaceInfo = ref(null)
const emit = defineEmits(["handleSaveSuccess"]);
const data = reactive({
  rules: {
    furnaceNumber: [{ required: true, message: "炉号不能为空", trigger: "change" }],
  },
});
const {rules } = toRefs(data);

/***********************  方法区  ****************************/
/** 打开抽屉 */
function open(data) {
  reset();
  visible.value = true;
 detailInfo.value = data
 console.log(detailInfo.value)
 console.log(furnaceInfo.value)
 getFunaceList()
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false;
  reset();
}
//获取炉号
function getFunaceList(){
  loading.value = true;
  furnaceList.value = [];
//   furnaceInfoList.value = [{furnaceNumber:"777",lineNumber:'2',incomingDate:'2024-06-05'},{furnaceNumber:"888",lineNumber:'3',incomingDate:'2024-06-07'}]
//   furnaceList.value = furnaceInfoList.value.map(item => {
//   return {
//     label: item.furnaceNumber,
//     value: `${item.furnaceNumber}%${item.incomingDate}` // 假设您希望value是furnaceNumber
//   };
// });
  getFurnaceNoInfo({
    productionPlanNo: detailInfo.value.productionPlanNo,
    lineNumber: detailInfo.value.lineNumber,
  }).then((res) => {
    if (res.code === 200 && res.data.length > 0) {
      for (let i = 0; i < res.data.length; i++) {
        furnaceInfoList.value = res.data
        furnaceList.value = furnaceInfoList.value.map(item => {
        return {
          label: item.furnaceNumber,
          value: `${item.furnaceNumber}%${item.incomingDate}` // 假设您希望value是furnaceNumber
        };
      });
						}
    } else {
      furnaceInfoList.value = []
      furnaceList.value = [];
    }
  });
  loading.value = false;
  visible.value = true;
}

/** 表单重置 */
function reset() {
  furnaceInfo.value = {};
  proxy.resetForm("furnaceRef");
}
//切换炉号
function handleFurnaceChange(){
  let parts = furnaceInfo.value.furnaceNumber.split('%');
let furnaceNumber = parts[0];
let incomingDate = parts[1];
console.log(parts)
   furnaceInfo.value = furnaceInfoList.value.filter(item => item.furnaceNumber == furnaceNumber && item.incomingDate == incomingDate)[0]
  console.log(furnaceInfo.value)
}

/** 提交按钮 */
function handleSave() {
  proxy.$refs["furnaceRef"].validate((valid) => {
    if (valid) {
      updateDayworkFurnace({dayworkIds:detailInfo.value.dayworkIds,furnaceNoInfo:furnaceInfo.value}).then(res=>{
        if(res.code == 200){
          proxy.$message.success("修改成功");
          visible.value = false;
          emit("handleSaveSuccess");
        }
      })
    }
  })
 
}


/** 暴露给父组件的方法 */
defineExpose({
  open,
});
</script>
