<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="90px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="12">
            <el-form-item prop="contentNo" label="标准名称">
              <el-input v-model="baseData.contentName" style="width:100%" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="stdItemNo" label="标准项目名称">
              <el-select v-model="baseData.stdItemNo" filterable @change="changeVal" style="width:100%" collapse-tags size="small">
                <el-option v-for="(item,index) in stdItemNameType" :key="index" :label="item.stdItemName" :value="item.stdItemNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="resultValue" label="结果值">
               <el-input v-model="baseData.resultValue" style="width:100%" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="dspOrder" label="显示顺序">
               <el-input v-model="baseData.dspOrder" 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="addData()">确定</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";
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ["showFlag", "Params"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "新增子内容",
      tableType:0,
      stdItemNameType: [],
      baseData: {},
      ruleValidate: {
        stdItemNo: [
          { required: true, message: "该项不能为空", trigger: "change" },
        ],
      },
    }
  },
  mounted() {
    this.axios.post("pass/baseManagement/v1/limsstditems/queryLimsStdItemPage",{ object: { validFlag: 1 }, pageIndex: 1, pageSize: 99999 },{ individualType: "json" }).then((res) => {
      this.stdItemNameType = res.data.list;
    });
  },
  methods: {
    getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : '';
    },
    cancel() {
      this.baseData = {}
      this.dialogTableVisible = false;
    },
    //改变值的时候获默认的结果值
    changeVal(val) {
      this.baseData.resultValue = this.getCode(this.baseData.stdItemNo,this.stdItemNameType,'stdItemNo','defaultValue')
    },
    addData() {
      this.$refs["baseData"].validate((valid) => {
        if (valid) {
          this.baseData.apprManName = userName;
          this.baseData.apprManNo = userId;
          this.baseData.dataTypeName = this.getCode(this.baseData.stdItemNo,this.stdItemNameType,'stdItemNo','dataTypeName')
          this.baseData.dataTypeCode = this.getCode(this.baseData.stdItemNo,this.stdItemNameType,'stdItemNo','dataTypeCode')
          this.baseData.stdItemName = this.getCode(this.baseData.stdItemNo,this.stdItemNameType,'stdItemNo','stdItemName')
          this.axios.post("pass/baseManagement/v1/limsstdcontentds/saveLimsStdContentD", 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 = {}
      this.$refs['baseData'].resetFields();
      this.dialogTableVisible = false;
    },
  },
  watch: {
    showFlag(arg) {
      let _this = this;
      this.baseData = Object.assign({}, this.Params);
      _this.dialogTableVisible = true;
      _this.tableType = 1;
      _this.tableTypeDsc = "修改标准内容子信息";
    }
  },
};
</script>
<style lang="less">
.customWidth_standar {
  width: 700px !important;
}
</style>
