<template>
  <div class="app-container">
    <el-alert
      v-if="receiptNo"
      :title="`入库单号: ${receiptNo}`"
      type="info"
      :closable="false"
      show-icon
      style="margin-bottom: 15px"
    >
      <el-button size="mini" type="text" @click="handleBack">返回入库单列表</el-button>
    </el-alert>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="入库单号" prop="receiptNo" v-if="!receiptNo">
        <el-input
          v-model="queryParams.receiptNo"
          placeholder="请输入入库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品型号" prop="sampleModel">
        <el-input
          v-model="queryParams.sampleModel"
          placeholder="请输入样品型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品SN号" prop="sampleSn">
        <el-input
          v-model="queryParams.sampleSn"
          placeholder="请输入样品SN号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外测状态" prop="externalStatus">
        <el-select v-model="queryParams.externalStatus" placeholder="请选择外测状态" clearable>
          <el-option label="PCBA" value="PCBA" />
          <el-option label="点板板" value="点板板" />
          <el-option label="建板板" value="建板板" />
        </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="1" />
          <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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warehouse:sample:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:sample:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库单号" align="center" prop="receiptNo" width="120"/>
      <el-table-column label="样品型号" align="center" prop="sampleModel" width="100"/>
      <el-table-column label="样品SN号" align="center" prop="sampleSn" width="120"/>
      <el-table-column label="硬件版本号" align="center" prop="hardwareVersion" width="120"/>
      <el-table-column label="外测状态" align="center" prop="externalStatus" width="100"/>
      <el-table-column label="计划测试项" align="center" prop="testItems" :show-overflow-tooltip="true"/>
      <el-table-column label="测试截止日期" align="center" prop="testDeadline" width="120"/>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="warning">待测试</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="primary">测试中</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="success">已完成</el-tag>
          <el-tag v-else-if="scope.row.status === '3'" type="danger">已退回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:sample:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:sample:remove']"
          >删除</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"
    />

    <!-- 添加或修改样品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库单号" prop="receiptNo" v-if="!receiptId">
              <el-input v-model="form.receiptNo" placeholder="请输入入库单号" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样品型号" prop="sampleModel">
              <el-input v-model="form.sampleModel" placeholder="请输入样品型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="样品SN号" prop="sampleSn">
              <el-input v-model="form.sampleSn" placeholder="请输入样品SN号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="硬件版本号" prop="hardwareVersion">
              <el-input v-model="form.hardwareVersion" placeholder="请输入硬件版本号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="样品外测状态" prop="externalStatus">
              <el-select v-model="form.externalStatus" placeholder="请选择外测状态" style="width: 100%">
                <el-option label="PCBA" value="PCBA" />
                <el-option label="点板板" value="点板板" />
                <el-option label="建板板" value="建板板" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划测试截止日期" prop="testDeadline">
              <el-date-picker
                v-model="form.testDeadline"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择测试截止日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="计划测试项" prop="testItems">
              <el-input v-model="form.testItems" type="textarea" placeholder="请输入计划测试项" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
                <el-option label="待测试" value="0" />
                <el-option label="测试中" value="1" />
                <el-option label="已完成" value="2" />
                <el-option label="已退回" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSample, getSample, delSample, addSample, updateSample } from "@/api/warehouse/sample"

export default {
  name: "WarehouseSample",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样品表格数据
      sampleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 入库单ID（从路由参数获取）
      receiptId: null,
      // 入库单号（从路由参数获取）
      receiptNo: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptId: null,
        receiptNo: null,
        sampleModel: null,
        sampleSn: null,
        externalStatus: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiptId: [
          { required: true, message: "入库单ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // 从路由参数获取入库单信息
    this.receiptId = this.$route.query.receiptId;
    this.receiptNo = this.$route.query.receiptNo;
    if (this.receiptId) {
      this.queryParams.receiptId = this.receiptId;
    }
    this.getList();
  },
  methods: {
    /** 查询样品列表 */
    getList() {
      this.loading = true;
      listSample(this.queryParams).then(response => {
        this.sampleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sampleId: null,
        receiptId: this.receiptId || null,
        receiptNo: this.receiptNo || null,
        sampleModel: null,
        sampleSn: null,
        hardwareVersion: null,
        externalStatus: null,
        testItems: null,
        testDeadline: null,
        status: '0',
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sampleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (!this.receiptId) {
        this.$modal.msgWarning("请先选择入库单");
        return;
      }
      this.open = true;
      this.title = "添加样品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sampleId = row.sampleId || this.ids
      getSample(sampleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改样品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sampleId != null) {
            updateSample(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSample(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sampleIds = row.sampleId || this.ids;
      this.$modal.confirm('是否确认删除样品编号为"' + sampleIds + '"的数据项？').then(function() {
        return delSample(sampleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 返回入库单列表 */
    handleBack() {
      this.$router.push('/warehouse/receipt');
    }
  }
};
</script>


