<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80">
      <el-form-item label="报表编号" prop="reportNo">
        <el-input v-model="queryParams.reportNo" placeholder="请输入报表编号" clearable />
      </el-form-item>
      <el-form-item label="报表名称" prop="reportName">
        <el-input v-model="queryParams.reportName" placeholder="请输入报表名称" clearable />
      </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="['base:inspectionReport:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['base:inspectionReport:edit']">修改</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="['base:inspectionReport:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['base:inspectionReport:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="inspectionReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报表编号" align="center" prop="reportNo" />
      <el-table-column label="报表名称" align="center" prop="reportName" />
      <el-table-column label="产品编号" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="工单编号" align="center" prop="workOrderNo" />
      <el-table-column label="检验类型" align="center" prop="inspectionType" />
      <el-table-column label="检验工单总数" align="center" prop="totalInspections" />
      <el-table-column label="通过数量" align="center" prop="passedQty" />
      <el-table-column label="不通过数量" align="center" prop="failedQty" />
      <el-table-column label="未检验数量" align="center" prop="pendingQty" />
      <el-table-column label="合格率" align="center" prop="passRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:inspectionReport:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['base:inspectionReport: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="报表编号" prop="reportNo">
          <el-input v-model="form.reportNo" placeholder="请输入报表编号" />
        </el-form-item>
        <el-form-item label="报表名称" prop="reportName">
          <el-input v-model="form.reportName" placeholder="请输入报表名称" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="工单编号" prop="workOrderNo">
          <el-input v-model="form.workOrderNo" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="检验类型" prop="inspectionType">
          <el-input v-model="form.inspectionType" placeholder="请输入检验类型" />
        </el-form-item>
        <el-form-item label="检验工单总数" prop="totalInspections">
          <el-input-number v-model="form.totalInspections" :min="0" />
        </el-form-item>
        <el-form-item label="通过数量" prop="passedQty">
          <el-input-number v-model="form.passedQty" :min="0" />
        </el-form-item>
        <el-form-item label="不通过数量" prop="failedQty">
          <el-input-number v-model="form.failedQty" :min="0" />
        </el-form-item>
        <el-form-item label="未检验数量" prop="pendingQty">
          <el-input-number v-model="form.pendingQty" :min="0" />
        </el-form-item>
        <el-form-item label="合格率" prop="passRate">
          <el-input v-model="form.passRate" placeholder="请输入合格率" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间" />
        </el-form-item>
      </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 { listInspectionReport, getInspectionReport, delInspectionReport, addInspectionReport, updateInspectionReport } from "@/api/base/inspectionReport";

export default {
  name: "InspectionReport",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      inspectionReportList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportNo: null,
        reportName: null,
        productCode: null,
        productName: null,
        workOrderNo: null,
        inspectionType: null
      },
      form: {},
      rules: {
        reportNo: [{ required: true, message: "报表编号不能为空", trigger: "blur" }],
        reportName: [{ required: true, message: "报表名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listInspectionReport(this.queryParams).then(response => {
        this.inspectionReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        objId: null,
        reportNo: null,
        reportName: null,
        productCode: null,
        productName: null,
        workOrderNo: null,
        inspectionType: null,
        totalInspections: null,
        passedQty: null,
        failedQty: null,
        pendingQty: null,
        passRate: null,
        startTime: null,
        endTime: null,
        createdBy: null,
        createdTime: 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.objId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检验报表";
    },
    handleUpdate(row) {
      this.reset();
      const objId = row.objId || this.ids;
      getInspectionReport(objId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检验报表";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.objId != null) {
            updateInspectionReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspectionReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const objIds = row.objId || this.ids;
      this.$modal.confirm('是否确认删除检验报表编号为"' + objIds + '"的数据项？').then(function() {
        return delInspectionReport(objIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('base/inspectionReport/export', { ...this.queryParams }, `inspectionReport_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
