<!-- 物料出库弹窗 -->
<template>
  <el-dialog
    title="物料出库"
    :visible.sync="visible"
    width="900px"
    append-to-body
    destroy-on-close
  >
    <div class="outbound-table">
      <el-table :data="tableData" size="small" border>
        <el-table-column label="物料编码" width="280">
          <template slot-scope="scope">
            <el-autocomplete
              v-model="scope.row.materialCode"
              :fetch-suggestions="searchMaterials"
              :trigger-on-focus="false"
              @select="(item) => handleMaterialSelect(item, scope.$index)"
              placeholder="请输入物料编码或名称"
              clearable
            >
              <template slot-scope="{ item }">
                <div class="material-item">
                  <div class="material-code">{{ item.code }}</div>
                  <div class="material-desc">{{ item.description }}</div>
                </div>
              </template>
            </el-autocomplete>
          </template>
        </el-table-column>
        <el-table-column label="物料描述" prop="materialDescription">
          <template slot-scope="scope">
            <el-input v-model="scope.row.materialDescription" readonly placeholder="物料描述" />
          </template>
        </el-table-column>
        <el-table-column label="出库数量" width="150">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.quantity"
              :min="1"
              controls-position="right"
              placeholder="请输入数量"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="removeRow(scope.$index)"
              :disabled="tableData.length === 1"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-footer">
        <el-button type="primary" icon="el-icon-plus" @click="addRow">新增行</el-button>
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'OutboundDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableData: [
        {
          id: Date.now(),
          materialCode: '',
          materialDescription: '',
          quantity: 1
        }
      ]
    }
  },
  methods: {
    // 搜索物料
    async searchMaterials(query, callback) {
      if (query.length < 2) {
        callback([])
        return
      }

      try {
        // 这里替换为实际的后端API调用
        // const { data } = await getMaterialList({ keyword: query })
        // callback(data.map(item => ({
        //   code: item.materialCode,
        //   description: item.materialName,
        //   value: item.materialCode
        // })))

        // 模拟数据，实际使用时替换为上面的API调用
        const mockData = [
          { code: 'MAT001', description: '钢板', value: 'MAT001' },
          { code: 'MAT002', description: '铝板', value: 'MAT002' },
          { code: 'MAT003', description: '铜线', value: 'MAT003' }
        ].filter(item => 
          item.code.toLowerCase().includes(query.toLowerCase()) ||
          item.description.includes(query)
        )
        callback(mockData)
      } catch (error) {
        this.$message.error('获取物料列表失败')
        callback([])
      }
    },

    // 选择物料
    handleMaterialSelect(item, index) {
      this.$set(this.tableData[index], 'materialCode', item.code)
      this.$set(this.tableData[index], 'materialDescription', item.description)
    },

    // 新增行
    addRow() {
      this.tableData.push({
        id: Date.now(),
        materialCode: '',
        materialDescription: '',
        quantity: 1
      })
    },

    // 删除行
    removeRow(index) {
      if (this.tableData.length > 1) {
        this.tableData.splice(index, 1)
      }
    },

    // 取消
    handleCancel() {
      this.$emit('update:visible', false)
      this.resetForm()
    },

    // 提交
    handleSubmit() {
      // 验证数据
      const invalidRow = this.tableData.find(item => 
        !item.materialCode || 
        !item.materialDescription || 
        !item.quantity || 
        item.quantity < 1
      )

      if (invalidRow) {
        this.$message.warning('请填写完整的出库信息')
        return
      }

      // 提交数据
      this.$emit('submit', this.tableData)
      this.handleCancel()
    },

    // 重置表单
    resetForm() {
      this.tableData = [
        {
          id: Date.now(),
          materialCode: '',
          materialDescription: '',
          quantity: 1
        }
      ]
    }
  }
}
</script>

<style lang="scss" scoped>
.outbound-table {
  .material-item {
    .material-code {
      font-weight: bold;
    }
    .material-desc {
      font-size: 12px;
      color: #666;
    }
  }

  .table-footer {
    margin-top: 15px;
    display: flex;
    justify-content: flex-start;
  }
}

.el-autocomplete {
  width: 100%;
}

.el-input-number {
  width: 100%;
}
</style>