<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="standardCode">
        <el-input v-model="queryParams.standardCode" placeholder="请输入标准编号" clearable />
      </el-form-item>
      <el-form-item label="标准名称" prop="standardName">
        <el-input v-model="queryParams.standardName" 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:inspectionStandard: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:inspectionStandard: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:inspectionStandard: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:inspectionStandard:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="inspectionStandardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标准编号" align="center" prop="standardCode" />
      <el-table-column label="标准名称" align="center" prop="standardName" />
      <el-table-column label="产品编号" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="检验类型" align="center" prop="inspectionType" />
      <el-table-column label="检验项目" align="center" prop="inspectionItems" />
      <el-table-column label="检验方法" align="center" prop="inspectionMethod" />
      <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:inspectionStandard:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['base:inspectionStandard: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="standardCode">
          <el-input v-model="form.standardCode" placeholder="请输入标准编号" />
        </el-form-item>
        <el-form-item label="标准名称" prop="standardName">
          <el-input v-model="form.standardName" 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="inspectionType">
          <el-input v-model="form.inspectionType" placeholder="请输入检验类型" />
        </el-form-item>
        <el-form-item label="检验项目" prop="inspectionItems">
          <el-input v-model="form.inspectionItems" placeholder="请输入检验项目" />
        </el-form-item>
        <el-form-item label="检验方法" prop="inspectionMethod">
          <el-input v-model="form.inspectionMethod" placeholder="请输入检验方法" />
        </el-form-item>
        <el-form-item label="标准值" prop="standardValue">
          <el-input v-model="form.standardValue" placeholder="请输入标准值" />
        </el-form-item>
        <el-form-item label="公差范围" prop="toleranceRange">
          <el-input v-model="form.toleranceRange" placeholder="请输入公差范围" />
        </el-form-item>
        <el-form-item label="抽样方案" prop="samplingPlanCode">
          <el-select v-model="form.samplingPlanCode" placeholder="请选择抽样方案" clearable filterable>
            <el-option
              v-for="item in samplingPlanList"
              :key="item.planCode"
              :label="item.planCode + ' - ' + item.planName"
              :value="item.planCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listInspectionStandard, getInspectionStandard, delInspectionStandard, addInspectionStandard, updateInspectionStandard, getInspectionStandardList } from "@/api/base/inspectionStandard";
import { getSamplingPlanList } from "@/api/base/samplingPlan";

export default {
  name: "InspectionStandard",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      inspectionStandardList: [],
      samplingPlanList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        standardCode: null,
        standardName: null,
        productCode: null,
        productName: null,
        inspectionType: null,
        isFlag: null
      },
      form: {},
      rules: {
        standardCode: [{ required: true, message: "标准编号不能为空", trigger: "blur" }],
        standardName: [{ required: true, message: "标准名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    this.loadSamplingPlanList();
  },
  methods: {
    getList() {
      this.loading = true;
      listInspectionStandard(this.queryParams).then(response => {
        this.inspectionStandardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        objId: null,
        standardCode: null,
        standardName: null,
        productCode: null,
        productName: null,
        inspectionType: null,
        inspectionItems: null,
        inspectionMethod: null,
        standardValue: null,
        toleranceRange: null,
        samplingPlanCode: null,
        remark: null,
        isFlag: 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;
      getInspectionStandard(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) {
            updateInspectionStandard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspectionStandard(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 delInspectionStandard(objIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('base/inspectionStandard/export', { ...this.queryParams }, `inspectionStandard_${new Date().getTime()}.xlsx`);
    },
    loadSamplingPlanList() {
      getSamplingPlanList().then(response => {
        this.samplingPlanList = response.rows || [];
      });
    }
  }
};
</script>
