<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库单号" prop="outStoreOrderNumber">
        <el-input
          v-model="queryParams.outStoreOrderNumber"
          placeholder="请输入出库单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标检单号" prop="productStandardInspectionNumber">
        <el-input
          v-model="queryParams.productStandardInspectionNumber"
          placeholder="请输入主体标检单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="handleSyncMESData"
        >获取</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column label="出库单号" align="center" prop="outStoreOrderNumber" />
      <el-table-column label="标检单号" align="center" prop="productStandardInspectionNumber" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <!-- 如果 calibrationGas 是单个值 -->
          <span v-if="!Array.isArray(scope.row.status)">
              {{ getStatusLabel(scope.row.status) }}
          </span>

          <!-- 如果 calibrationGas 是数组 -->
          <div v-else>
            <el-tag
              v-for="(item, index) in scope.row.status"
              :key="index"
              style="margin: 2px;"
            >
              {{ getStatusLabel(item) }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="报检人" align="center" prop="createBy" />
      <el-table-column label="报检部门" align="center" prop="inspectionDep" />
      <el-table-column label="报检时间" align="center" prop="createTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料代码" align="center" prop="materialCode" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="领料数量" align="center" prop="issuedNum" />
      <el-table-column label="申请数量" align="center" prop="quantity" />
      <el-table-column label="合格数量" align="center" prop="qualifiedNum" />
      <el-table-column label="不合格数量" width="110px" align="center" prop="unQualifiedNum" />
      <el-table-column label="老化时长" align="center" prop="agingDuration" />
      <el-table-column label="标定气体" align="center" prop="calibrationGas">
        <template slot-scope="scope">
          <!-- 如果 calibrationGas 是单个值 -->
          <span v-if="!Array.isArray(scope.row.calibrationGas)">
              {{ getCalibrationGasLabel(scope.row.calibrationGas) }}
          </span>

          <!-- 如果 calibrationGas 是数组 -->
          <div v-else>
            <el-tag
              v-for="(item, index) in scope.row.calibrationGas"
              :key="index"
              style="margin: 2px;"
            >
              {{ getCalibrationGasLabel(item) }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="报警值" align="center" prop="alarmValue" />
      <el-table-column label="量程" align="center" prop="range" />
      <el-table-column label="读取模式" align="center" prop="readingMode">
        <template slot-scope="scope">
          <!-- 如果 calibrationGas 是单个值 -->
          <span v-if="!Array.isArray(scope.row.readingMode)">
              {{ getReadingModeLabel(scope.row.readingMode) }}
          </span>

          <!-- 如果 calibrationGas 是数组 -->
          <div v-else>
            <el-tag
              v-for="(item, index) in scope.row.readingMode"
              :key="index"
              style="margin: 2px;"
            >
              {{ getReadingModeLabel(item) }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否带上传" align="center" prop="hasUploadDesc" />
    </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库单号" prop="outStoreOrderNumber">
          <el-input v-model="form.outStoreOrderNumber" placeholder="请输入出库单号" />
        </el-form-item>
        <el-form-item label="主体标检单号" prop="productStandardInspectionNumber">
          <el-input v-model="form.productStandardInspectionNumber" placeholder="请输入主体标检单号" />
        </el-form-item>
        <el-form-item label="确认标检人" prop="confirmQualityInspector">
          <el-input v-model="form.confirmQualityInspector" placeholder="请输入确认标检人" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态 (PENDING_STANDARD_INSPECTION-待标检;DURING_STANDARD_INSPECTION-标检中;STANDARD_INSPECTION_COMPLETED-标检完成) ">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报检部门" prop="inspectionDep">
          <el-input v-model="form.inspectionDep" placeholder="请输入报检部门" />
        </el-form-item>
        <el-form-item label="删除标志，默认0，删除1" prop="deleteFlag">
          <el-input v-model="form.deleteFlag" placeholder="请输入删除标志，默认0，删除1" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料代码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料代码" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input v-model="form.specification" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="领料数量" prop="issuedNum">
          <el-input v-model="form.issuedNum" placeholder="请输入领料数量" />
        </el-form-item>
        <el-form-item label="申请数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入申请数量" />
        </el-form-item>
        <el-form-item label="合格数量" prop="qualifiedNum">
          <el-input v-model="form.qualifiedNum" placeholder="请输入合格数量" />
        </el-form-item>
        <el-form-item label="不合格数量" prop="unQualifiedNum">
          <el-input v-model="form.unQualifiedNum" placeholder="请输入不合格数量" />
        </el-form-item>
        <el-form-item label="老化时长" prop="agingDuration">
          <el-input v-model="form.agingDuration" placeholder="请输入老化时长" />
        </el-form-item>
        <el-form-item label="标定气体" prop="calibrationGas">
          <el-input v-model="form.calibrationGas" placeholder="请输入标定气体" />
        </el-form-item>
        <el-form-item label="报警值" prop="alarmValue">
          <el-input v-model="form.alarmValue" placeholder="请输入报警值" />
        </el-form-item>
        <el-form-item label="量程" prop="range">
          <el-input v-model="form.range" 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 { listInspection, getInspection, delInspection, addInspection, updateInspection, exportInspection ,syncMESData} from "@/api/system/standardInspection";

export default {
  name: "Inspection",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品主体标检单表格数据
      inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outStoreOrderNumber: null,
        productStandardInspectionNumber: null,
        confirmQualityInspector: null,
        remarks: null,
        status: null,
        createBy: null,
        inspectionDep: null,
        createTime: null,
        deleteFlag: null,
        materialName: null,
        materialCode: null,
        specification: null,
        issuedNum: null,
        quantity: null,
        qualifiedNum: null,
        unQualifiedNum: null,
        agingDuration: null,
        calibrationGas: null,
        alarmValue: null,
        range: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productStandardInspectionNumber: [
          { required: true, message: "主体标检单号不能为空", trigger: "blur" }
        ],
      },

      statusStyle:{
        "PENDING_STANDARD_INSPECTION":"color:#C40202",
        "DURING_STANDARD_INSPECTION":"color:#E34B19",
        "STANDARD_INSPECTION_COMPLETED":"color:#0C6BD1",
      },

      // 标定气体
      calibrationGasOptions:[],

      // status
      statusOptions:[],
      // 读取方式
      readingModeOptions:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("calibration_gas").then((response) => {
      this.calibrationGasOptions = response.data;
    });
    this.getDicts("standard_inspection_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("reading_mode").then((response) => {
      this.readingModeOptions = response.data;
    });
  },
  methods: {
    // 获取标定气体的显示标签
    getCalibrationGasLabel(dictValue) {
      if (!dictValue) return '';

      const item = this.calibrationGasOptions.find(option => option.dictValue === dictValue);
      return item ? item.dictLabel : dictValue; // 如果找不到对应的选项，显示原始值
    },
    getReadingModeLabel(dictValue) {
      if (!dictValue) return '';

      const item = this.readingModeOptions.find(option => option.dictValue === dictValue);
      return item ? item.dictLabel : dictValue; // 如果找不到对应的选项，显示原始值
    },

    getStatusLabel(dictValue) {
      if (!dictValue) return '';

      const item = this.statusOptions.find(option => option.dictValue === dictValue);
      return item ? item.dictLabel : dictValue; // 如果找不到对应的选项，显示原始值
    },

    /** 查询产品主体标检单列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productStandardInspectionId: null,
        outStoreOrderNumber: null,
        productStandardInspectionNumber: null,
        confirmQualityInspector: null,
        remarks: null,
        status: "0",
        createBy: null,
        inspectionDep: null,
        createTime: null,
        deleteFlag: null,
        materialName: null,
        materialCode: null,
        specification: null,
        issuedNum: null,
        quantity: null,
        qualifiedNum: null,
        unQualifiedNum: null,
        agingDuration: null,
        calibrationGas: null,
        alarmValue: null,
        range: 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.productStandardInspectionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品主体标检单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productStandardInspectionId = row.productStandardInspectionId || this.ids
      getInspection(productStandardInspectionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品主体标检单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productStandardInspectionId != null) {
            updateInspection(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productStandardInspectionIds = row.productStandardInspectionId || this.ids;
      this.$confirm('是否确认删除产品主体标检单编号为"' + productStandardInspectionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInspection(productStandardInspectionIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    handleSyncMESData() {
      this.$confirm('是否确认同步MES数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
        this.exportLoading = true;
          return syncMESData();
        }).then(response => {
          this.msgSuccess("同步成功");
          this.exportLoading = false;
          this.getList();
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品主体标检单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInspection(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
