<template>
  <div class="examination">
    <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
      <div slot="title" class="titleBox" style="display: flex;">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
      </div>
      <el-form :model="baseData" ref="baseData" :rules="rules" label-width="80px" :inline="false" size="normal">
        <el-row>
          <el-col :span="12">
            <el-form-item label="证书名称">
              <el-input v-model="baseData.cretName" readonly disabled size="small" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='matClassNo' label="物料类型">
              <el-select v-model="baseData.matClassNo" filterable @change="selctMatName" collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in matClassNameType" :key="index" :label="item.matClassName"
                  :value="item.matClassNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
          <el-col :span="12">
            <el-form-item label="打印次数">
              <el-input v-model="baseData.prtNum" size="small" style="width: 100%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop='matNo' label="物料名称">
              <el-select v-model="baseData.matNo" filterable collapse-tags size="small" style="width: 100%;">
                <el-option v-for="(item,index) in matNameType" :key="index" :label="item.matName"
                  :value="item.matNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="baseData.memo" size="small" style="width: 100%;"></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" :loading='addLoading' @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: ["showFlag2", "Params", "Params2"],
  data() {
    return {
      dialogTableVisible: false,
      tableTypeDsc: "新增物料配置信息",
      tableType: 0,
      baseData:{
        matNo:''
      },
      rules:{
        matClassNo: [{ required: true, message: '不能为空', trigger: 'change' }],
        matNo: [{ required: true, message: '不能为空', trigger: 'change' }]
      },
      addLoading:false,
      matClassNameType: [],
      matNameType: []
    };
  },
  mounted() { 
    this.axios.get("pass/baseManagement/v1/limsbasematerialsclasss/" + "?validFlag=1").then((res) => {
      this.matClassNameType = res.data.list;
    });
  },
  methods: {
    getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        return targetRow ? targetRow[nameKey] : '';
    },
    selctMatName(val){
        this.matNameType = []
        this.baseData.clearFlag ? '' : this.baseData.matNo = ''
        if (!val) {
          return 
      }
      //物料名称
      this.axios.post('pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage/',{object:{validFlag:1,matClassNo:val},pageIndex:1,pageSize:99999},{individualType: 'json'}).then(res => {
        this.matNameType = res.data.list;
      })
    },
    cancel() {
      this.baseData = {matNo:''}
      this.addLoading = false
      this.dialogTableVisible = false;
    },
    addData() {
      this.$refs['baseData'].validate((valid) => {
        if (valid) {
          this.addLoading = true
          this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.matClassNameType,'matClassNo','matClassName')
          this.baseData.matName = this.getCode(this.baseData.matNo,this.matNameType,'matNo','matName')
          this.axios.post( "pass/baseManagement/v1/limscretmats/insert",this.baseData,{ individualType: "json" }).then((res) => {
            if (res.code == 0 || res.code == 200) {
              this.dialogTableVisible = false;
              this.$message.success(res.message);
              this.$emit("refresh");
            } else {
              this.$message.error(res.message);
            }
            this.addLoading = false
          }).catch((e) => {
            this.addLoading = false
            this.$message.error(e.message);
          });
        }
      })
    },
    modifyData() {
      this.$refs['baseData'].validate((valid) => {
        if (valid) {
          this.baseData.updataMan = userName
          this.baseData.updataNo = userId
          this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.matClassNameType,'matClassNo','matClassName')
          this.baseData.matName = this.getCode(this.baseData.matNo,this.matNameType,'matNo','matName')
          this.axios.put("pass/baseManagement/v1/limscretmats/",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 = {matNo:''}
      this.addLoading = false
    }
  },
  watch: {
    showFlag2(arg) {
      if (this.Params2 !== null) {
        this.tableType = 1;
        this.tableTypeDsc = "修改物料配置信息";
        this.baseData = Object.assign({},this.Params2)
      } else {
        this.tableType = 0;
        this.baseData.cretNo = this.Params.cretNo;
        this.baseData.cretName = this.Params.cretName;
        this.tableTypeDsc = "新增物料配置信息";
      }
      this.dialogTableVisible = true;
    },
  },
};
</script>
<style lang="less">
</style>

