<template>
  <el-dialog
    title="选择钢卷"
    :visible.sync="dialogVisible"
    width="900px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" class="search-form">
      <el-form-item label="卷号">
        <el-input
          v-model="queryParams.currentCoilNo"
          placeholder="请输入卷号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钢种">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入钢种"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="coilList"
      @row-click="handleRowClick"
      highlight-current-row
      height="400px"
      style="width: 100%"
    >
      <el-table-column type="index" width="50" align="center" label="序号" />
      <el-table-column label="卷号" align="center" prop="currentCoilNo" :show-overflow-tooltip="true" />
      <el-table-column label="钢种" align="center" prop="grade" width="100" />
      <el-table-column label="厚度(mm)" align="center" prop="thickness" width="100" />
      <el-table-column label="宽度(mm)" align="center" prop="width" width="100" />
      <el-table-column label="重量(t)" align="center" prop="weight" width="100" />
      <el-table-column label="库区" align="center" prop="warehouseName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click.stop="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listMaterialCoil } from '@/api/wms/coil';

export default {
  name: 'CoilSelector',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 过滤条件（可以预设一些查询条件）
    filters: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      loading: false,
      coilList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        currentCoilNo: null,
        grade: null,
        dataType: 1 // 只查询当前数据，不查询历史数据
      }
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.resetQuery();
        this.getList();
      }
    }
  },
  methods: {
    // 获取钢卷列表
    async getList() {
      try {
        this.loading = true;
        const params = { ...this.queryParams, ...this.filters };
        const response = await listMaterialCoil(params);
        if (response.code === 200) {
          this.coilList = response.rows || [];
          this.total = response.total || 0;
        }
      } catch (error) {
        console.error('获取钢卷列表失败', error);
        this.$message.error('获取钢卷列表失败');
      } finally {
        this.loading = false;
      }
    },

    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 重置
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        currentCoilNo: null,
        grade: null,
        dataType: 1
      };
      this.getList();
    },

    // 点击行
    handleRowClick(row) {
      this.handleSelect(row);
    },

    // 选择钢卷
    handleSelect(row) {
      this.$emit('select', row);
      this.handleClose();
    },

    // 关闭对话框
    handleClose() {
      this.dialogVisible = false;
    }
  }
};
</script>

<style scoped lang="scss">
.search-form {
  margin-bottom: 20px;
}

::v-deep .el-dialog__body {
  padding: 20px;
}
</style>

