<template>
  <div class="examination">
    <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
      <div slot="title" class="titleBox">
        <i
          :class="tableType === 0 ? 'el-icon-document' : 'el-icon-edit-outline'"
          style="font-size: 20px"
        ></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left: 10px; font-size: 18px">{{
          tableTypeDsc
        }}</span>
      </div>
      <el-form
        ref="baseData"
        label-width="80px"
        :rules="ruleValidate"
        :model="baseData"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item prop="testItemNo" label="任务名称">
              <el-select
                v-model="baseData.testItemNo"
                filterable
                @change="selectfx"
                clearable
                collapse-tags
                style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="(item, index) in testItemNameType"
                  :key="index"
                  :label="item.testItemName"
                  :value="item.testItemNo"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="maxSignTypeIndex" label="上限符">
              <el-select
                v-model="baseData.maxSign"
                filterable
                collapse-tags
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in maxSignType"
                  :key="index"
                  :label="item.maxSign"
                  :value="item.maxSign"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="minSignTypeIndex" label="下限符">
              <el-select
                v-model="baseData.minSign"
                filterable
                collapse-tags
                size="small"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in minSignType"
                  :key="index"
                  :label="item.minSign"
                  :value="item.minSign"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="anlyItemNo" label="分析项目">
              <el-select
                v-model="baseData.anlyItemNo"
                filterable
                clearable
                style="width: 100%"
                collapse-tags
                size="small"
              >
                <el-option
                  v-for="(item, index) in anlyItemNameType"
                  :key="index"
                  :label="item.anlyItemName"
                  :value="item.anlyItemNo"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大值">
              <el-input
                v-model="baseData.maxValue"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最小值">
              <el-input
                v-model="baseData.minValue"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="memo" label="备注">
              <el-input
                v-model="baseData.memo"
                style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          @click="tableType === 0 ? addData() : modifyData()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancel"
          >取消</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCookie } from "@/utils/util.js";
import { cookieUserId, cookieUserName } from "@/config/config.js";
import { HTML5_FMT } from "moment";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ["showFlag2", "Params", "Params2"],
  data() {
    var maxValueVerify = (rule, value, callback) => {
      if (this.baseData.maxValue) {
        if (Number(this.baseData.maxValue) > Number(this.baseData.minValue)) {
          callback();
        } else {
          callback(new Error("上限不能小于下限"));
        }
      } else {
        callback();
      }
    };
    return {
      dialogTableVisible: false,
      tableTypeDsc: "新增计划检验项目信息",
      tableType: 0,
      testItemNameType: [],
      anlyItemNameType: [],
      minSignType: [{ minSign: ">=" }, { minSign: ">" }],
      maxSignType: [{ maxSign: "<=" }, { maxSign: "<" }],
      baseData: { anlyItemNo: "" },
      ruleValidate: {
        // maxValue: [{required: true,message: "该项不能为空",trigger: ["blur", "change"],},{ required: true, validator: maxValueVerify, trigger: "blur" },],
        // minValue: [{required: true,message: "该项不能为空",trigger: ["blur", "change"],},],
        testItemNo: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
        anlyItemNo: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.axios
      .post(
        "pass/baseManagement/v1/limsstdtestitems/queryLimsStdTestItemInfoAll",
        { individualType: "json" }
      )
      .then((res) => {
        this.testItemNameType = res.data;
      });
    // this.axios.post('pass/baseManagement/v1/limsstdanlyitems/queryLimsStdAnlyItemList',{itemTypeCode:''}, {individualType: 'json'}).then(res => {
    //   this.anlyItemNameType = res.data;
    // });
  },
  methods: {
    selectfx(val) {
      this.anlyItemNameType = [];
      this.testItemNameType.find((item) => {
        if (val === item.testItemNo) {
          this.anlyItemNameType = item.lstidList;
          this.baseData.clearFlag ? "" : this.baseData.anlyItemNo;
        }
      });
    },
    getCode(code, arr, codeKey, nameKey) {
      if (!code || !arr) {
        return "";
      }
      let targetRow = arr.find((item) => {
        return item[codeKey] === code;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : "";
    },
    cancel() {
      this.baseData = { anlyItemNo: "" };
      this.dialogTableVisible = false;
    },
    addData() {
      let _this = this;
      this.$refs["baseData"].validate((valid) => {
        if (valid) {
          this.baseData.createMan = userName;
          this.baseData.createNo = userId;
          this.baseData.testItemName = this.getCode(
            this.baseData.testItemNo,
            this.testItemNameType,
            "testItemNo",
            "testItemName"
          );
          this.baseData.anlyItemName = this.getCode(
            this.baseData.anlyItemNo,
            this.anlyItemNameType,
            "anlyItemNo",
            "anlyItemName"
          );
          this.axios
            .post(
              "pass/testManagement/v1/limstestentrustds/addLimsTestEntrustD",
              _this.baseData,
              { individualType: "json" }
            )
            .then((res) => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success("添加成功");
                _this.$emit("refresh");
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        }
      });
    },
    modifyData() {
      let _this = this;
      _this.$refs["baseData"].validate((valid) => {
        if (valid) {
          _this.baseData.updateMan = userName;
          _this.baseData.updateNo = userId;
          this.baseData.testItemName = this.getCode(
            this.baseData.testItemNo,
            this.testItemNameType,
            "testItemNo",
            "testItemName"
          );
          this.baseData.anlyItemName = this.getCode(
            this.baseData.anlyItemNo,
            this.anlyItemNameType,
            "anlyItemNo",
            "anlyItemName"
          );
          _this.axios
            .post(
              "pass/testManagement/v1/limstestentrustds/addLimsTestEntrustD",
              _this.baseData,
              { individualType: "json" }
            )
            .then((res) => {
              if (res.succeed) {
                this.dialogTableVisible = false;
                this.$message.success("修改成功");
                _this.$emit("refresh");
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        }
      });
    },
    closeDialog() {
      this.baseData = { anlyItemNo: "" };
    },
  },
  watch: {
    showFlag2(arg) {
      let _this = this;
      if (_this.Params2 !== null) {
        _this.baseData = Object.assign({}, this.Params2);
        _this.tableType = 1;
        _this.tableTypeDsc = "修改计划检验项目信息";
      } else {
        this.tableType = 0;
        this.baseData.entrId = this.Params.entrId;
        this.tableTypeDsc = "新增计划检验项目信息";
      }
      this.dialogTableVisible = true;
    },
  },
};
</script>
<style lang="less">
.customWidth_syst {
  width: 600px !important;
}
</style>
