<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="60px">
      <el-form-item label="计划号" prop="planNo">
        <el-input v-model="queryParams.planNo" placeholder="请输入计划号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="目标炉" prop="targetFurnaceId">
        <el-select v-model="queryParams.targetFurnaceId" placeholder="请选择" clearable filterable>
          <el-option v-for="item in furnaceOptions" :key="item.furnaceId" :label="item.furnaceName"
            :value="item.furnaceId" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option label="未开始" :value="0" />
          <el-option label="进行中" :value="2" />
          <el-option label="已完成" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <KLPTable v-loading="loading" :data="planList" @row-click="handleRowClick">
      <el-table-column label="计划号" align="center" prop="planNo" />
      <el-table-column label="计划时间" align="center" prop="planStartTime" width="160">
        <template slot-scope="scope">
          {{ parseTime(scope.row.planStartTime, '{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>
      <el-table-column label="目标炉" align="center" prop="targetFurnaceName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="statusTag(scope.row.status)">{{ statusLabel(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="actualStartTime" width="160">
        <template slot-scope="scope">
          {{ parseTime(scope.row.actualStartTime, '{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="160">
        <template slot-scope="scope">
          {{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click.stop="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)">删除</el-button>
          <el-button v-if="scope.row.status === 2" size="mini" type="text" icon="el-icon-check"
            @click.stop="handleComplete(scope.row)">完成</el-button>
        </template>
      </el-table-column>
    </KLPTable>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-row :gutter="20" class="mt16">
      <el-col :span="12">
        <div class="custom-panel">
          <div class="panel-header">
            <span class="panel-title">领料列表</span>
            <el-button size="mini" icon="el-icon-refresh" @click="getMaterialCoils">刷新</el-button>
          </div>
          <div class="panel-content left-content">
            <el-form :model="materialQueryParams" ref="materialQueryForm" size="small" :inline="true" class="mb8">
              <el-form-item label="入场钢卷号" prop="enterCoilNo">
                <el-input v-model="materialQueryParams.enterCoilNo" placeholder="请输入入场钢卷号" clearable
                  style="width: 160px" />
              </el-form-item>
              <el-form-item label="当前钢卷号" prop="currentCoilNo">
                <el-input v-model="materialQueryParams.currentCoilNo" placeholder="请输入当前钢卷号" clearable
                  style="width: 160px" />
              </el-form-item>
              <el-form-item label="逻辑库区" prop="warehouseId">
                <WarehouseSelect v-model="materialQueryParams.warehouseId" placeholder="请选择逻辑库区" clearable
                  style="width: 160px" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleMaterialQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetMaterialQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <div v-loading="materialLoading" class="material-grid">
              <div v-if="materialList.length === 0" class="empty-tip">
                <el-empty description="暂无待领物料" />
              </div>
              <div v-for="item in materialList" :key="item.coilId" class="material-card">
                <div class="material-header">
                  <div class="material-text-wrap">
                    <div class="material-title">{{ item.currentCoilNo || '-' }}</div>
                    <div class="material-sub">入场：{{ item.enterCoilNo || '-' }}</div>
                  </div>
                  <el-button type="primary" size="mini" @click="handleAddToPlan(item)"
                    :disabled="!currentPlan.planId || currentPlan.status !== 0">加入计划</el-button>
                </div>
                <div class="material-body">
                  <div class="material-row">厂家：{{ item.supplierCoilNo || '-' }}</div>
                  <div class="material-row">库位：{{ item.actualWarehouseName || '-' }}</div>
                  <div class="material-row">重量：{{ item.netWeight || '-' }}t</div>
                </div>
              </div>
            </div>
            <pagination v-show="materialTotal > 0" :total="materialTotal" :page.sync="materialQueryParams.pageNum"
              :limit.sync="materialQueryParams.pageSize" @pagination="getMaterialCoils" />
          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="custom-panel">
          <div class="panel-header">
            <span class="panel-title">退火计划</span>
            <div>
              <el-button size="mini" icon="el-icon-refresh" @click="loadPlanCoils"
                :disabled="!currentPlan.planId">刷新</el-button>
            </div>
          </div>
          <div class="panel-content">
            <div v-if="!currentPlan.planId" class="empty-tip">
              <el-empty description="请点击上方选择一个计划" />
            </div>
            <div v-else>
              <div class="plan-summary">
                <div>计划号：{{ currentPlan.planNo }}</div>
                <div>目标炉：{{ currentPlan.targetFurnaceName || '-' }}</div>
                <div>状态：{{ statusLabel(currentPlan.status) }}</div>
              </div>
              <KLPTable :data="coilList" v-loading="coilLoading" class="light-table" :floatLayer="true"
                :floatLayerConfig="floatLayerConfig">
                <el-table-column label="钢卷层级" align="center" width="80" prop="action">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.furnaceLevel" placeholder="请输入钢卷层级"
                      @change="handlePLanCoilChange(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column label="入场钢卷号" align="center" prop="enterCoilNo" />
                <el-table-column label="创建时间" align="center" prop="createTime">
                  <template slot-scope="scope">
                    {{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}
                  </template>
                </el-table-column>
                <el-table-column label="实际库位" align="center" width="220">
                  <template slot-scope="scope">
                    <span>{{ scope.row.coil.actualWarehouseName || '-' }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="100" prop="action">
                  <template slot-scope="scope">
                    <el-button v-if="currentPlan.status === 0" size="mini" type="text" icon="el-icon-delete"
                      @click="handleUnbind(scope.row)">
                      {{ unbindLabel(currentPlan.status) }}
                    </el-button>
                  </template>
                </el-table-column>
              </KLPTable>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="计划号" prop="planNo">
          <el-input v-model="form.planNo" placeholder="请输入计划号" />
        </el-form-item>
        <el-form-item label="计划开始" prop="planStartTime">
          <el-date-picker clearable v-model="form.planStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择计划时间" />
        </el-form-item>
        <el-form-item label="目标炉" v-if="!form.planId">
          <el-select v-model="targetFurnaces" placeholder="请选择" filterable clearable multiple key="mu">
            <el-option v-for="item in furnaceOptions" :key="item.furnaceId" :label="item.furnaceName"
              :value="item.furnaceId" />
          </el-select>
        </el-form-item>
        <el-form-item label="目标炉" prop="targetFurnaceId" v-else>
          <el-select v-model="form.targetFurnaceId" placeholder="请选择" filterable clearable key="si">
            <el-option v-for="item in furnaceOptions" :key="item.furnaceId" :label="item.furnaceName"
              :value="item.furnaceId" />
          </el-select>
        </el-form-item>
        <el-form-item label="实际开始" prop="actualStartTime" v-if="form.planId">
          <el-date-picker clearable v-model="form.actualStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择实际开始时间" />
        </el-form-item>
        <el-form-item label="实际结束" prop="endTime" v-if="form.planId">
          <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择实际结束时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAnnealPlan, getAnnealPlan, updateAnnealPlanCoil, addAnnealPlan, updateAnnealPlan, delAnnealPlan, changeAnnealPlanStatus, inFurnace, completeAnnealPlan, listAnnealPlanCoils, bindAnnealPlanCoils, unbindAnnealPlanCoil } from "@/api/wms/annealPlan";
import { listAnnealFurnace } from "@/api/wms/annealFurnace";
import { listMaterialCoil } from "@/api/wms/coil";
import WarehouseSelect from "@/components/KLPService/WarehouseSelect";
import { addAnnealOperateEvent } from "@/api/wms/annealOperateEvent";

export default {
  name: "AnnealPlan",
  components: {
    WarehouseSelect
  },
  data() {
    return {
      buttonLoading: false,
      loading: true,
      coilLoading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      planList: [],
      furnaceOptions: [],
      targetFurnaces: [],
      title: "",
      open: false,
      statusOpen: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        planNo: undefined,
        targetFurnaceId: undefined,
        status: undefined,
      },
      form: {},
      rules: {
        planNo: [{ required: true, message: "计划号不能为空", trigger: "blur" }],
        targetFurnaceId: [{ required: true, message: "目标炉子不能为空", trigger: "change" }],
      },
      currentPlan: {},
      coilList: [],
      materialLoading: false,
      materialTotal: 0,
      materialList: [],
      materialQueryParams: {
        pageNum: 1,
        pageSize: 20,
        enterCoilNo: undefined,
        currentCoilNo: undefined,
        status: 0,
        dataType: 1,
      },
      statusForm: {
        planId: undefined,
        status: undefined,
      },
      completeOpen: false,
      completeLoading: false,
      completePlanId: null,
      completeCoils: [],
      floatLayerConfig: {
        columns: [
          { label: '入场钢卷号', prop: 'enterCoilNo' },
          { label: '当前钢卷号', prop: 'coil.currentCoilNo' },
          { label: '厂家卷号', prop: 'coil.supplierCoilNo' },
          { label: '逻辑库位', prop: 'coil.warehouseName' },
          { label: '实际库位', prop: 'coil.actualWarehouseName' },
          { label: '物料类型', prop: 'coil.materialType' },
          { label: '班组', prop: 'coil.team' },
          { label: '净重', prop: 'coil.netWeight' },
          { label: '毛重', prop: 'coil.grossWeight' },
          { label: '备注', prop: 'coil.remark' },
          { label: '质量状态', prop: 'coil.qualityStatus' },
          { label: '原料材质', prop: 'coil.packingStatus' },
          { label: '切边要求', prop: 'coil.edgeRequirement' },
          { label: '包装要求', prop: 'coil.packagingRequirement' },
          { label: '厂家', prop: 'coil.itemManufacturer' },
          { label: '调制度', prop: 'coil.temperGrade' },
          { label: '镀层种类', prop: 'coil.coatingType' },
          { label: '实测长度(m)', prop: 'coil.actualLength' },
          { label: '实测宽度(mm)', prop: 'coil.actualWidth' },
          { label: '实测厚度(mm)', prop: 'coil.actualThickness' },
        ],
        title: '详细信息'
      },
    };
  },
  created() {
    this.getList();
    this.loadFurnaces();
    this.getMaterialCoils();
  },
  methods: {
    getList() {
      this.loading = true;
      listAnnealPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    loadFurnaces() {
      listAnnealFurnace({ pageNum: 1, pageSize: 999, status: 1 }).then(response => {
        this.furnaceOptions = response.rows || [];
      });
    },
    handleRowClick(row) {
      this.currentPlan = row;
      this.loadPlanCoils();
    },
    getMaterialCoils() {
      this.materialLoading = true;
      listMaterialCoil(this.materialQueryParams).then(response => {
        this.materialList = response.rows || [];
        this.materialTotal = response.total || 0;
        this.materialLoading = false;
      });
    },
    handleMaterialQuery() {
      this.materialQueryParams.pageNum = 1;
      this.getMaterialCoils();
    },
    resetMaterialQuery() {
      this.resetForm("materialQueryForm");
      this.resetMaterialForm();
      this.handleMaterialQuery();
    },
    handlePLanCoilChange(row) {
      updateAnnealPlanCoil(row).then(() => {
        this.$message.success('已更新');
      }).finally(() => {
        this.loadPlanCoils();
      });
    },
    handleAddToPlan(item) {
      if (!this.currentPlan.planId) {
        this.$message.warning('请先选择计划');
        return;
      }
      if (this.currentPlan.status === 2) {
        this.$message.warning('当前计划进行中，无法再领料');
        return;
      }
      this.coilLoading = true;
      bindAnnealPlanCoils({
        planId: this.currentPlan.planId,
        coilId: item.coilId
      }).then(() => {
        this.$message.success('已加入计划');
        // 查找对应id退火炉的名称
        const targetFurnaceName = this.furnaceOptions.find(item => item.furnaceId === this.currentPlan.targetFurnaceId)?.furnaceName || '';
        // anneal-todo: 新增操作事件
        addAnnealOperateEvent({
          annealFurnaceId: this.currentPlan.targetFurnaceId,
          operateType: 'ADD',
          operateContent: '钢卷号' + item.enterCoilNo + '加入退火炉' + targetFurnaceName,
          coilId: item.coilId,
        })
        this.loadPlanCoils();
      }).finally(() => {
        this.coilLoading = false;
      });
    },
    loadPlanCoils() {
      if (!this.currentPlan.planId) {
        this.coilList = [];
        return;
      }
      this.coilLoading = true;
      listAnnealPlanCoils(this.currentPlan.planId).then(response => {
        this.coilList = response.data || [];
        this.coilLoading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        planId: undefined,
        planNo: undefined,
        planStartTime: undefined,
        targetFurnaceId: undefined,
        status: 0,
        remark: undefined,
      };
      this.targetFurnaces = [];
      this.resetForm("form");
    },
    resetMaterialForm() {
      this.materialQueryParams = {
        pageNum: 1,
        pageSize: 10,
        enterCoilNo: undefined,
        currentCoilNo: undefined,
        status: 0,
        dataType: 1,
      };
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退火计划";
    },
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const planId = row.planId || this.ids;
      getAnnealPlan(planId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改退火计划";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.planId != null) {
            updateAnnealPlan(this.form).then(() => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            if (this.targetFurnaces.length === 0) {
              this.$message.warning('请选择至少一个目标炉');
              return;
            }
            Promise.all(this.targetFurnaces.map(id => {
              return addAnnealPlan({
                ...this.form,
                targetFurnaceId: id,
              }).then(() => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            })).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.planId || this.ids;
      this.$modal.confirm('是否确认删除计划编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delAnnealPlan(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    openStatusDialog(row) {
      this.statusForm = {
        planId: row.planId,
        status: row.status,
      };
      this.statusOpen = true;
    },
    submitStatus() {
      changeAnnealPlanStatus(this.statusForm).then(() => {
        this.$modal.msgSuccess("状态已更新");
        this.statusOpen = false;
        this.getList();
      });
    },
    async handleInFurnace(row) {
      await this.$confirm('点击入炉后钢卷将会被释放库位，是否确认入炉', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      });
      this.loading = true;
      await inFurnace({ planId: row.planId });
      // anneal-todo: 新增操作事件
      this.loading = false;
      row.status = 2;
      row.actualStartTime = new Date();
      if (this.currentPlan.planId === row.planId) {
        this.currentPlan.status = 2;
        this.currentPlan.actualStartTime = row.actualStartTime;
        this.loadPlanCoils();
      }
      this.getList();
      this.$message.success('已入炉');
    },
    handleUnbind(row) {
      this.$confirm('确定解绑该钢卷吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return unbindAnnealPlanCoil({
          planId: this.currentPlan.planId,
          coilId: row.coilId
        });
      }).then(() => {
        this.$message.success('解绑成功');
        // 查找对应id退火炉的名称
        const targetFurnaceName = this.furnaceOptions.find(item => item.furnaceId === this.currentPlan.targetFurnaceId)?.furnaceName || '';
        // anneal-todo: 新增操作事件
        addAnnealOperateEvent({
          annealFurnaceId: this.currentPlan.targetFurnaceId,
          operateType: 'UNBIND',
          operateContent: '钢卷号' + row.enterCoilNo + '解绑退火炉' + targetFurnaceName,
          coilId: row.coilId,
        })
        this.loadPlanCoils();
      });
    },
    statusLabel(status) {
      if (status === 2) {
        return '进行中';
      }
      if (status === 3) {
        return '已完成';
      }
      return '未开始';
    },
    statusTag(status) {
      if (status === 2) {
        return 'success';
      }
      if (status === 3) {
        return 'info';
      }
      return 'warning';
    },
    unbindLabel(status) {
      if (status === 2) {
        return '取消';
      }
      if (status === 3) {
        return '撤回';
      }
      return '解绑';
    }
  }
};
</script>

<style scoped>
.mt16 {
  margin-top: 16px;
}

.empty-tip {
  margin-top: 10px;
}

/* 自定义卡片样式 */
.custom-panel {
  border: 1px solid #f0f2f5;
  border-radius: 4px;
  background: #ffffff;
  /* 避免溢出隐藏，让浮层可以正常显示 */
  overflow: visible;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid #f0f2f5;
}

.panel-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.panel-content {
  padding: 16px 18px;
  /* 避免溢出隐藏 */
  overflow: visible;
}

.panel-content.left-content {
  height: calc(100vh - 400px);
  overflow-y: auto;
}

/* ========== 修复在这里 ========== */
.material-grid {
  display: grid;
  /* 核心修复：去掉固定 4 列，改用自动填充，实现真正自适应 */
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  min-height: 120px;
  /* 必须加，让 grid 不受父级弹性压缩影响 */
  width: 100%;
  box-sizing: border-box;
}

/* 媒体查询只需要控制最小宽度即可，不用写死列数 */
@media (max-width: 768px) {
  .material-grid {
    grid-template-columns: 1fr;
  }
}

/* =============================== */

.material-card {
  border: 1px solid #e9ecf2;
  border-radius: 8px;
  padding: 12px;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}

.material-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  /* 新增：让文字区域和按钮分行适配小宽度 */
  flex-wrap: wrap;
}

/* 新增：文字容器，限制宽度并溢出省略 */
.material-text-wrap {
  flex: 1;
  min-width: 0;
  /* 关键：让flex子元素遵守宽度限制 */
}

.material-title {
  font-weight: 600;
  color: #303133;
  /* 新增：标题溢出省略 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
  /* 可根据需要调整最大宽度 */
}

.material-sub {
  font-size: 12px;
  color: #909399;
  /* 核心：入场钢卷号溢出省略 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
  /* 限制最大长度，避免挤压按钮 */
}

.material-body {
  font-size: 12px;
  color: #606266;
  display: grid;
  gap: 4px;
}

.material-row {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.plan-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: 8px;
  font-size: 12px;
  color: #606266;
  margin-bottom: 12px;
}

:deep(.el-table th),
:deep(.el-table td) {
  border-bottom: 1px solid #f0f2f5;
}

:deep(.el-table::before) {
  background-color: transparent;
}

/* 为浮层提供更好的展示空间 */
.app-container {
  min-height: 100vh;
  overflow-y: auto;
  padding-bottom: 40px;
}
</style>
