<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="90px">
      <el-form-item label="速比" prop="ratioValue">
        <el-input
          v-model="queryParams.ratioValue"
          placeholder="请输入速比(如: 10.50)"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="输入数量" prop="qty">
        <el-input-number
          v-model="queryParams.qty"
          :min="1"
          placeholder="齐套数量"
          controls-position="right"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div v-if="hasSearched">
      <el-card shadow="never" class="box-card">
        <div slot="header" class="clearfix" style="display: flex; align-items: center; flex-wrap: wrap; gap: 10px;">
          <span style="font-weight: bold; font-size: 15px;">速比齐套查询结果</span>
          <template v-if="reportData">
            <el-tag size="mini" type="info">速比: {{ reportData.ratioValue }}</el-tag>
            <el-tag size="mini" type="info">最大输出扭矩: {{ reportData.maxTorqueNm }} Nm</el-tag>
            <el-tag v-if="reportData.inputQty" size="mini" type="success">
              输入数量: {{ reportData.inputQty }}
            </el-tag>
            <el-tag size="mini" type="warning" style="font-weight: bold;">
              最大可齐套数量: {{ reportData.maxBuildQty || 0 }}
            </el-tag>
          </template>
        </div>
        <el-table v-loading="loading" :data="dataList" border size="small" max-height="650">
          <template slot="empty">
            <el-empty description="暂无数据" :image-size="80" />
          </template>
          <el-table-column label="配件类别" prop="partCategory" width="80" align="center" show-overflow-tooltip />
          <el-table-column label="物料编码" prop="materialCode" min-width="120" show-overflow-tooltip />
          <el-table-column label="物料名称" prop="materialName" min-width="150" show-overflow-tooltip />
          <el-table-column label="单重" prop="danzhong" width="60" align="center" show-overflow-tooltip />
          <el-table-column label="材质" prop="canzhi" width="80" align="center" show-overflow-tooltip />
          <el-table-column label="需求数量" prop="requiredQty" width="80" align="right" />

          <el-table-column label="即时库存" prop="onHandQty" width="80" align="right" />
          <el-table-column label="未领料" prop="moNotPickQty" width="70" align="right" />
          <el-table-column label="可用" prop="availableQty" width="70" align="right">
            <template slot="header">
              <span>可用</span>
              <el-tooltip class="item" effect="dark" content="不包含不良品和废品" placement="top">
                <i class="el-icon-question" style="margin-left: 4px; color: #909399; cursor: pointer;" />
              </el-tooltip>
            </template>
            <template slot-scope="scope">
              <span style="font-weight: bold;">{{ scope.row.availableQty || 0 }}</span>
            </template>
          </el-table-column>

          <el-table-column label="在途" align="center">
            <el-table-column label="采购在途" prop="poNotInQty" width="80" align="right">
              <template slot-scope="scope">
                <el-popover v-if="scope.row.poInTransit && scope.row.poInTransit.length > 0" trigger="hover" placement="top" width="300">
                  <div style="max-height: 200px; overflow-y: auto;">
                    <strong>采购在途明细：</strong>
                    <div v-for="(item, idx) in scope.row.poInTransit" :key="idx" style="margin-top: 5px; font-size: 12px; border-bottom: 1px solid #ebeef5; padding-bottom: 4px;">
                      单号: <span style="color: #409EFF">{{ item.billNo }}</span><br>
                      未入库: <span style="color: #E6A23C">{{ item.notInstockQty }}</span> |
                      交期: {{ item.deliveryDate ? item.deliveryDate.substring(0, 10) : '-' }}
                    </div>
                  </div>
                  <div slot="reference" style="cursor: pointer; color: #409EFF; font-weight: bold; text-decoration: underline;">
                    {{ scope.row.poNotInQty || 0 }}
                  </div>
                </el-popover>
                <span v-else>{{ scope.row.poNotInQty || 0 }}</span>
              </template>
            </el-table-column>

            <el-table-column label="生产在途" prop="moNotInQty" width="80" align="right">
              <template slot-scope="scope">
                <el-popover v-if="scope.row.moInTransit && scope.row.moInTransit.length > 0" trigger="hover" placement="top" width="300">
                  <div style="max-height: 200px; overflow-y: auto;">
                    <strong>生产在途明细：</strong>
                    <div v-for="(item, idx) in scope.row.moInTransit" :key="idx" style="margin-top: 5px; font-size: 12px; border-bottom: 1px solid #ebeef5; padding-bottom: 4px;">
                      单号: <span style="color: #67C23A">{{ item.billNo }}</span><br>
                      未入库: <span style="color: #E6A23C">{{ item.notInstockQty }}</span> |
                      计划完工: {{ item.planFinishDate ? item.planFinishDate.substring(0, 10) : '-' }}
                    </div>
                  </div>
                  <div slot="reference" style="cursor: pointer; color: #67C23A; font-weight: bold; text-decoration: underline;">
                    {{ scope.row.moNotInQty || 0 }}
                  </div>
                </el-popover>
                <span v-else>{{ scope.row.moNotInQty || 0 }}</span>
              </template>
            </el-table-column>
          </el-table-column>

          <el-table-column label="最终可用" prop="finalAvailableQty" width="80" align="right">
            <template slot-scope="scope">
              <span style="font-weight: bold; color: #409EFF;">{{ scope.row.finalAvailableQty || 0 }}</span>
            </template>
          </el-table-column>

          <el-table-column label="可支持数量" prop="canSupportQty" width="90" align="right" />

          <el-table-column v-if="queryParams.qty" label="齐套补差" prop="shortageQty" width="80" align="right" fixed="right">
            <template slot-scope="scope">
              <span :style="{ color: (scope.row.shortageQty || 0) > 0 ? '#f56c6c' : '#67c23a', fontWeight: 'bold' }">
                {{ scope.row.shortageQty || 0 }}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <el-empty v-else description="请输入速比并点击查询获取数据" />
  </div>
</template>

<script>
import { getRatioBuildCapacity } from '@/api/system/ratio'

export default {
  name: 'RatioBuildCapacity',
  data() {
    return {
      loading: false,
      hasSearched: false,
      queryParams: {
        ratioValue: undefined,
        qty: undefined
      },
      reportData: null,
      dataList: []
    }
  },
  methods: {
    handleQuery() {
      if (!this.queryParams.ratioValue) {
        this.$modal.msgWarning('请输入速比')
        return
      }
      this.hasSearched = true
      this.loading = true
      getRatioBuildCapacity(this.queryParams).then(res => {
        if (res.data) {
          this.reportData = res.data
          this.dataList = res.data.items || []
        } else {
          this.reportData = null
          this.dataList = []
        }
      }).catch(() => {
        this.reportData = null
        this.dataList = []
      }).finally(() => {
        this.loading = false
      })
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.reportData = null
      this.dataList = []
      this.hasSearched = false
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-top: 15px;
}
</style>
