<template>
  <div>
    <!-- 全局loading遮罩 -->
    <div v-if="globalLoading" class="global-loading-overlay">
      <el-loading-spinner></el-loading-spinner>
      <p>正在加载推荐数据...</p>
    </div>

    <!-- 采购计划主数据表单 -->
    <el-form :model="mainForm" :rules="mainFormRules" ref="mainFormRef" :inline="true" label-width="120px" style="margin-bottom: 20px;" v-loading="formLoading" element-loading-text="正在加载主数据...">
      <el-form-item label="计划编号" prop="planCode">
        <el-input v-model="mainForm.planCode" placeholder="请输入计划编号" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="负责人" prop="owner">
        <el-input v-model="mainForm.owner" placeholder="请输入负责人" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="mainForm.remark" placeholder="请输入备注" style="width: 300px;" />
      </el-form-item>
    </el-form>

    <el-form :inline="true" @submit.native.prevent>
    </el-form>
    <el-table 
      :data="tableData" 
      style="width: 100%; margin-top: 20px" 
      border 
      :loading="tableLoading" 
      element-loading-text="正在加载明细数据..."
      element-loading-spinner="el-icon-loading"
      :empty-text="tableLoading ? '正在加载数据...' : '暂无数据'"
    >
      <el-table-column prop="rawMaterialId" label="原材料ID" width="120">
        <template #default="scope">
          <RawMaterialSelect v-model="scope.row.rawMaterialId" size="small" :loading="selectLoading" />
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="负责人" width="120">
        <template #default="scope">
          <el-input v-model="scope.row.owner" size="small" />
        </template>
      </el-table-column>
      <el-table-column prop="quantity" label="计划采购数" width="120">
        <template #default="scope">
          <el-input-number v-model="scope.row.quantity" :min="0" size="small" />
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="单位" width="100">
        <template #default="scope">
          <el-input v-model="scope.row.unit" size="small" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="180">
        <template #default="scope">
          <el-input v-model="scope.row.remark" size="small" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button type="danger" size="small" @click="removeRow(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px; text-align: right;" v-loading="submitLoading" element-loading-text="正在提交数据...">
      <el-button type="primary" @click="confirm" :loading="submitLoading" :disabled="globalLoading || tableLoading || formLoading">
        {{ submitLoading ? '提交中...' : '确认' }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { recommendPurchasePlan, createPurchasePlan } from '@/api/wms/purchasePlan'
import RawMaterialSelect from '@/components/KLPService/RawMaterialSelect'
import { updateOrder } from '@/api/wms/order'

const EOrderStatus = {
  NEW: 0,
  PRODUCTIONING: 1,
  FINISH: 2,
  CANCEL: 3
}

export default {
  name: 'PurchasePlanClac',
  components: { RawMaterialSelect },
  props: {
    orderId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      tableData: [],
      // 细化的loading状态
      globalLoading: false, // 全局loading
      formLoading: false,   // 表单loading
      tableLoading: false,  // 表格loading
      selectLoading: false, // 选择器loading
      submitLoading: false, // 提交loading
      mainForm: {
         planCode: '',
         owner: '',
         remark: ''
       },
      mainFormRules: {
        planCode: [
          { required: true, message: '请输入计划编号', trigger: 'blur' }
        ],
        owner: [
          { required: true, message: '请输入负责人', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    orderId: {
      immediate: true,
      handler(newVal) {
        this.loadData(newVal)
      }
    },
    tableData: {
      handler() {
        this.ensureEmptyRow();
      },
      deep: true
    }
  },
  methods: {
    loadData(orderId) {
      // 设置全局loading
      this.globalLoading = true;
      this.formLoading = true;
      this.tableLoading = true;
      this.selectLoading = true;

      if (!orderId) {
        this.tableData = []
        this.clearAllLoading();
        this.ensureEmptyRow();
        return
      }

      recommendPurchasePlan(orderId).then(res => {
        console.log(res, '推荐数据')
        this.tableData = res.data.detailList;
        this.mainForm = res.data;
        this.ensureEmptyRow();
      }).finally(() => {
        this.clearAllLoading();
      })
    },
    clearAllLoading() {
      this.globalLoading = false;
      this.formLoading = false;
      this.tableLoading = false;
      this.selectLoading = false;
    },
    removeRow(index) {
      this.tableData.splice(index, 1)
      this.ensureEmptyRow();
    },
    confirm() {
      this.submitLoading = true;

      // 校验主数据表单
      this.$refs.mainFormRef.validate(async(valid) => {
        if (!valid) {
          this.$message.error('请完整填写主数据信息');
          this.submitLoading = false;
          return;
        }

        // 校验表格数据（除remark外都不能为空）
        const filtered = this.tableData.filter(row => row.rawMaterialId && row.rawMaterialId !== '');
        const invalid = filtered.some(row => {
          return !row.rawMaterialId || !row.owner || !row.unit || row.quantity === '' || row.quantity === null || row.quantity === undefined;
        });
        if (invalid) {
          this.$message.error('请完整填写所有必填项');
          this.submitLoading = false;
          return;
        }

        // 合并主数据和明细数据
        const submitData = {
          ...this.mainForm,
          orderId: this.orderId,
          detailList: filtered
        };
        await createPurchasePlan(submitData);
        await updateOrder({
          orderId: this.orderId,
          orderStatus: EOrderStatus.PRODUCTIONING
        })
        // this.$message.success('操作已确认');
        console.log(submitData);
        this.$emit('confirm', submitData);
        this.submitLoading = false;
      });
    },
    ensureEmptyRow() {
      // 如果tableData为空，或最后一行已填写原材料，则补充一个空行
      if (
        this.tableData.length === 0 ||
        (this.tableData[this.tableData.length - 1] && this.tableData[this.tableData.length - 1].rawMaterialId)
      ) {
        this.tableData.push({
          rawMaterialId: '',
          owner: '',
          quantity: 0,
          unit: '',
          remark: ''
        });
      }
    }
  }
}
</script>

<style scoped>
.el-form {
  margin-bottom: 20px;
}

.global-loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  border-radius: 4px;
}

.global-loading-overlay p {
  margin-top: 10px;
  color: #409EFF;
  font-size: 14px;
}

/* 表格loading样式优化 */
.el-table .el-loading-mask {
  background-color: rgba(255, 255, 255, 0.8);
}

.el-table .el-loading-spinner .el-loading-text {
  color: #409EFF;
  font-size: 14px;
  margin-top: 10px;
}
</style>
