<template>
  <div>
    <el-card>
      <div class="search-container">
        <span class="show-text">物料编号:</span>
        <el-select
          v-model="物料编号"
          placeholder="请选择物料编号"
          clearable
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option
            v-for="item in partNoList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span class="show-text">工序名称:</span>
        <el-select
          v-model="工序名称编号"
          placeholder="请选择工序"
          clearable
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option
            v-for="item in processList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span class="show-text">检测类别:</span>
        <el-select
          v-model="检测类别编号"
          placeholder="请选择检测类别"
          clearable
          style="width: 150px; margin-right: 10px"
          filterable
        >
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          :disabled="loading"
          plain
          type="primary"
          @click="searchTable()"
          >查询</el-button
        >
        <el-button
          :disabled="loading"
          plain
          type="primary"
          @click="addCheckType()"
          >新增标准</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :height="'67vh'"
        border
        element-loading-background="rgba(0, 0, 0, 0.2)"
        element-loading-spinner="el-icon-loading"
        highlight-current-row
        size="mini"
        style="width: 100%; margin-top: 20px"
        tooltip-effect="dark"
      >
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="50"
          fixed
        />
        <el-table-column align="center" label="物料编号" width="240">
          <template slot-scope="scope">
            {{ scope.row.物料编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工序名称" width="100">
          <template slot-scope="scope">
            {{ getProcessLabel(scope.row.工序名称编号) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="检测类别" width="100">
          <template slot-scope="scope">
            {{ getDetectionTypeLabel(scope.row.检测类别编号) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="检测项目" width="120">
          <template slot-scope="scope">
            {{ scope.row.检测项目 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="上限值" width="80">
          <template slot-scope="scope">
            {{ scope.row.上限值 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="下限值" width="80">
          <template slot-scope="scope">
            {{ scope.row.下限值 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="测量单位" width="80">
          <template slot-scope="scope">
            {{ scope.row.测量单位 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="检验比例" width="80">
          <template slot-scope="scope">
            {{ scope.row.检验比例 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="检具" width="100">
          <template slot-scope="scope">
            {{ scope.row.检具 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" width="150">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <div class="operation-cell">
              <el-button
                type="text"
                size="mini"
                @click="editInspectionStandard(scope.row)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="mini"
                @click="deleteInspectionStandard(scope.row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增标准弹窗 -->
      <el-dialog
        :visible.sync="dialogVisibleAdd"
        :title="dialogTitle"
        center=""
        width="850px"
        style="text-align: center"
      >
        <el-form
          ref="checkForm"
          :model="checkForm"
          label-position="right"
          label-width="140px"
          size="mini"
          :rules="formRules"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="物料编号" prop="物料编号">
                <el-select
                  v-model="checkForm.物料编号"
                  placeholder="请选择物料编号"
                  clearable
                  style="width: 200px"
                  filterable
                >
                  <el-option
                    v-for="item in partNoList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工序名称" prop="工序名称编号">
                <el-select
                  v-model="checkForm.工序名称编号"
                  placeholder="请选择工序名称"
                  clearable
                  style="width: 200px"
                  filterable
                >
                  <el-option
                    v-for="item in processList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="检测类别" prop="检测类别编号">
                <el-select
                  v-model="checkForm.检测类别编号"
                  placeholder="请选择检测类别"
                  clearable
                  style="width: 200px"
                  filterable
                >
                  <el-option
                    v-for="item in typeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检具">
                <el-input
                  v-model="checkForm.检具"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="检测项目">
                <el-input
                  v-model="checkForm.检测项目"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="理论值">
                <el-input
                  v-model="checkForm.理论值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上限值">
                <el-input
                  v-model="checkForm.上限值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下限值">
                <el-input
                  v-model="checkForm.下限值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="测量单位">
                <el-input
                  v-model="checkForm.测量单位"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检验比例">
                <el-input
                  v-model="checkForm.检验比例"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input
                  v-model="checkForm.备注"
                  type="textarea"
                  :rows="3"
                  resize="vertical"
                  size="mini"
                  style="width: 400px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <div class="dialog-footer">
            <div style="margin-bottom: 20px">
              <el-button type="primary" @click="submitInspection('checkForm')"
                >确定</el-button
              >
              <el-button @click="dialogVisibleAdd = false">取 消</el-button>
            </div>
          </div>
        </el-form>
      </el-dialog>
      <!-- 修改标准弹窗 -->
      <el-dialog
        :visible.sync="dialogVisibleChange"
        :title="dialogTitle"
        center=""
        width="850px"
        style="text-align: center"
      >
        <el-form
          ref="checkForm"
          :model="checkForm"
          label-position="right"
          label-width="140px"
          size="mini"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="物料编号" prop="物料编号">
                <el-input
                  v-model="checkForm.物料编号"
                  size="mini"
                  readonly=""
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工序名称" prop="工序名称">
                <el-input
                  :value="getProcessLabel(checkForm.工序名称编号)"
                  size="mini"
                  readonly=""
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="检测类别编号" prop="检测类别编号">
                <el-input
                  :value="getDetectionTypeLabel(checkForm.检测类别编号)"
                  size="mini"
                  readonly=""
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检验比例">
                <el-input
                  v-model="checkForm.检验比例"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="检测项目">
                <el-input
                  v-model="checkForm.检测项目"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="理论值">
                <el-input
                  v-model="checkForm.理论值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上限值">
                <el-input
                  v-model="checkForm.上限值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下限值">
                <el-input
                  v-model="checkForm.下限值"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="测量单位">
                <el-input
                  v-model="checkForm.测量单位"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检具">
                <el-input
                  v-model="checkForm.检具"
                  size="mini"
                  style="width: 200px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input
                  v-model="checkForm.备注"
                  type="textarea"
                  :rows="3"
                  resize="vertical"
                  size="mini"
                  style="width: 400px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <div class="dialog-footer">
            <div style="margin-bottom: 20px">
              <el-button type="primary" @click="updateInspection('checkForm')"
                >确定</el-button
              >
              <el-button @click="dialogVisibleChange = false">取 消</el-button>
            </div>
          </div>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      物料编号: "", // 搜索条件
      工序名称编号: "", // 搜索条件
      检测类别编号: "", // 搜索条件
      partNoList: [],
      processList: [],
      typeList: [],
      dialogVisibleAdd: false,
      dialogVisibleChange: false,
      dialogTitle: "新增检验标准",
      checkForm: {
        AID: "",
        物料编号: "",
        工序名称编号: "",
        检测类别编号: "",
        检测项编号: "",
        检测项目: "",
        理论值: "",
        上限值: "",
        下限值: "",
        测量单位: "",
        检验比例: "",
        检具: "",
        备注: "",
        阶段标识: "",
      },
      formRules: {
        物料编号: [
          { required: true, message: "请选择物料编号", trigger: "change" },
        ],
        工序名称编号: [
          { required: true, message: "请选择工序名称", trigger: "change" },
        ],
        检测类别编号: [
          { required: true, message: "请选择检测类别", trigger: "change" },
        ],
      },
      currentEditRow: null,
      loading: false,
      tableData: [],
    };
  },

  created() {
    this.getNoList();
    this.getProcessList();
    this.getTypeList();
    this.searchTable();
  },
  methods: {
    getNoList() {
      this.partNoList = [];
      var Data = this.CreateData("11", "MES_ProcessManagement_PartGet");
      this.ExecDatabase(Data).then((response) => {
        // 使用对象来去重
        const uniqueObj = {};
        const uniqueList = [];

        response.data.forEach((item) => {
          const materialNo = item.物料编号;
          if (!uniqueObj[materialNo]) {
            uniqueObj[materialNo] = true;
            uniqueList.push({
              label: item.物料名称 || item.物料编号,
              value: materialNo,
            });
          }
        });

        this.partNoList = uniqueList;
      });
    },
    getProcessList() {
      this.processList = [];
      var Data = this.CreateData("11", "Prog_MES_LoadProcess");
      this.ExecDatabase(Data).then((response) => {
        for (let i = 0; i < response.data.length; i++) {
          this.processList.push({
            label: response.data[i].工序名称,
            value: response.data[i].阶段标识 || response.data[i].工艺编号,
          });
        }
      });
    },
    getTypeList() {
      this.typeList = [];
      var Data = this.CreateData("11", "查询字典");
      this.ExecDatabase(Data).then((response) => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data[i].数据类别 === "检测类型") {
            this.typeList.push({
              label: response.data[i].词条名称,
              value: response.data[i].词条编号,
            });
          }
        }
      });
    },
    getDetectionTypeLabel(value) {
      const type = this.typeList.find((item) => item.value === value);
      return type ? type.label : value;
    },
    getProcessLabel(value) {
      const process = this.processList.find((item) => item.value === value);
      return process ? process.label : value;
    },
    addCheckType() {
      this.checkForm = {
        AID: "",
        物料编号: "",
        工序名称编号: "",
        检测类别编号: "",
        检测项目: "",
        理论值: "",
        上限值: "",
        下限值: "",
        测量单位: "",
        检验比例: "",
        检具: "",
        备注: "",
      };
      this.dialogTitle = "新增检验标准";
      this.dialogVisibleAdd = true;
    },
    submitInspection(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = [];

          param.push(["物料编号", this.checkForm.物料编号]);
          param.push(["工序名称编号", this.checkForm.工序名称编号]);
          param.push(["检测类别编号", this.checkForm.检测类别编号]);
          param.push(["检测项目", this.checkForm.检测项目]);
          param.push(["理论值", this.checkForm.理论值]);
          param.push(["上限值", this.checkForm.上限值]);
          param.push(["下限值", this.checkForm.下限值]);
          param.push(["测量单位", this.checkForm.测量单位]);
          param.push(["检验比例", this.checkForm.检验比例]);
          param.push(["检具", this.checkForm.检具]);
          param.push(["备注", this.checkForm.备注]);

          const data = this.CreateData("12", "质量管理_检验标准表_新增", param);
          this.ExecDatabase(data)
            .then((response) => {
              if (response.data[0].result !== "1") {
                this.$message({
                  message: "检验标准提交失败！" + response.data[0].message,
                  type: "error",
                });
                return;
              }
              this.$message({
                message: "检验标准提交成功",
                type: "success",
              });
              this.dialogVisibleAdd = false;
              this.searchTable();
            })
            .catch((error) => {
              this.$message({
                message: "检验标准提交失败: " + error,
                type: "error",
              });
            });
        }
      });
    },
    searchTable() {
      this.loading = true;
      this.tableData = [];
      var param = [];
      if (this.物料编号) {
        param.push(["物料编号", this.物料编号]);
      }
      if (this.工序名称编号) {
        param.push(["工序名称编号", this.工序名称编号]);
      }
      if (this.检测类别编号) {
        param.push(["检测类别编号", this.检测类别编号]);
      }

      var Data = this.CreateData("11", "质量管理_检验标准表_查询", param);
      this.ExecDatabase(Data)
        .then((response) => {
          this.loading = false;
          if (response.data.length !== 0) {
            this.tableData = response.data;
          }
        })
        .catch((error) => {
          this.loading = false;
          this.$message.error("查询失败");
        });
    },
    editInspectionStandard(row) {
      this.checkForm = {
        AID: row.AID,
        物料编号: row.物料编号,
        工序名称编号: row.工序名称编号,
        检测类别编号: row.检测类别编号,
        检测项编号: row.检测项编号,
        检测项目: row.检测项目,
        理论值: row.理论值,
        上限值: row.上限值,
        下限值: row.下限值,
        测量单位: row.测量单位,
        检验比例: row.检验比例,
        检具: row.检具,
        备注: row.备注,
      };
      this.currentEditRow = row;
      this.dialogTitle = "修改检验标准";
      this.dialogVisibleChange = true;
    },
    updateInspection(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 构建更新参数 - 使用与存储过程一致的参数名
          const param = [];
          param.push(["AID", this.checkForm.AID]);
          param.push(["物料编号", this.checkForm.物料编号]);
          param.push(["工序名称编号", this.checkForm.工序名称编号]);
          param.push(["检测项目", this.checkForm.检测项目]);
          param.push(["理论值", this.checkForm.理论值]);
          param.push(["上限值", this.checkForm.上限值]);
          param.push(["下限值", this.checkForm.下限值]);
          param.push(["测量单位", this.checkForm.测量单位]);
          param.push(["检验比例", this.checkForm.检验比例]);
          param.push(["检具", this.checkForm.检具]);
          param.push(["备注", this.checkForm.备注]);
          param.push(["检测类别编号", this.checkForm.检测类别编号]);

          // 调用更新存储过程
          const data = this.CreateData("12", "质量管理_检验标准表_修改", param);
          this.ExecDatabase(data)
            .then((response) => {
              if (response.data[0].result !== "1") {
                this.$message({
                  message:
                    "检验标准更新失败！" + (response.data[0].message || ""),
                  type: "error",
                });
                return;
              }
              this.$message({
                message: "检验标准更新成功",
                type: "success",
              });
              this.dialogVisibleChange = false;
              this.searchTable(); // 刷新表格数据
            })
            .catch((error) => {
              this.$message({
                message: "检验标准更新失败: " + error,
                type: "error",
              });
            });
        }
      });
    },
    deleteInspectionStandard(row) {
      this.$confirm("此操作将永久删除该检验标准, 是否继续?", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          // 构建删除参数
          const param = [];
          param.push(["AID", row.AID]); // 使用传入row参数中的AID，而不是currentEditRow

          // 调用删除存储过程
          const data = this.CreateData("12", "质量管理_检验标准表_删除", param);
          this.ExecDatabase(data)
            .then((response) => {
              if (response.data[0].result !== "1") {
                this.$message({
                  message:
                    "检验标准删除失败！" + (response.data[0].message || ""),
                  type: "error",
                });
                return;
              }
              this.$message({
                message: "检验标准删除成功",
                type: "success",
              });
              this.searchTable(); // 刷新表格数据
            })
            .catch((error) => {
              this.$message({
                message: "检验标准删除失败: " + error,
                type: "error",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang="scss">
.search-container {
  margin-bottom: 20px;
}
.dialog-footer {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>