<template>
    <div class="examination">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width='70%'>
        <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="rules" :model="baseData">
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="contractNo" label='合同号'>
                <el-input v-model="baseData.contractNo" size="small" clearable style="width: 100%;" @blur="huoqu"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item prop="suppNo" label="供应商">
              <el-select v-model="baseData.suppNo" filterable style="width:100%" clearable collapse-tags size="small">
                <el-option v-for="(item,index) in suppNameType" :key="index" :label="item.merchName" :value="item.merchNo">
                </el-option>
              </el-select>
            </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop='matShapeCode' label='试样形状'>
                  <el-select v-model="baseData.matShapeCode" clearable filterable collapse-tags size="small" style="width: 100%;">
                    <el-option v-for="item in matShapeNameType" :key="item.baseCode" :label="item.baseName"
                      :value="item.baseCode">
                    </el-option>
                  </el-select>
                </el-form-item>
            </el-col>
            <el-col :span='12'>
              <el-form-item prop='testItemNo' label='测试项目'>
                <el-select v-model="baseData.testItemNo" filterable clearable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in anlyType" :key="index" :label="item.testItemName"
                    :value="item.testItemNo">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-form-item prop="lotsNo" label='到货批次号'>
                <el-input v-model="baseData.lotsNo" size="small" clearable style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span='12'>
              <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='addLoding' @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,
  formatDate
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
import { momentImmutableMethods } from 'moment-immutable-methods'
import moment from 'moment'
momentImmutableMethods(moment)
export default {
  props: ['showFlag', 'Params'],
    data () {
      return {
        dialogTableVisible: false,
        tableTypeDsc: '新增',
        tableType: 0,
        baseData:{
          suppNo: '',
          contractNo: '',
          matShapeCode: '',
          testItemNo: '',
          memo: ''
        },
        anlyType:[],
        matShapeNameType:[],
        suppNameType:[],
        addLoding:false,
        rules: {
          contractNo:[{ required: true, message: '不能为空', trigger: 'blur' },],
          matShapeCode:[{ required: true, message: '不能为空', trigger: 'change' },],
          suppNo: [{ required: true, message: '不能为空', trigger: 'change' },],
          testItemNo: [{ required: true, message: '不能为空', trigger: 'change' },]
        },
      }
    },
    mounted () {
      // //获取公司
      this.axios.post("pass/baseManagement/v1/limsmerchantsinfos/querysupcu",{merchTypeCode:483201,merchTypeName:'供应商'}, {individualType: 'json'}).then(res => {
        this.suppNameType = res.data;
      });
      this.axios.post("pass/baseManagement/v1/limsstdtestitems/queryLimsStdTestItemPage",{object:{validFlag:1},pageIndex:1,pageSize:9999}, {individualType: 'json'}).then(response => {
        this.anlyType = response.data.list
      });
      this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4819}, {individualType: 'json'}).then(res => {
        this.matShapeNameType = res.data;
      });
    },
    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.addLoding = false
        this.dialogTableVisible = false;
      },
      huoqu(){
        this.axios.post("pass/testManagement/v1/limstestplans/querySuppNameToContractNo?contractNo="+this.baseData.contractNo, {individualType: 'json'}).then(response => {
          if(response.data[0] && response.data[0].suppNo){
            this.baseData.suppNo = response.data[0].suppNo;
          }
        });
      },
      addData () {
        this.$refs['baseData'].validate((valid) => {
          if (valid) {
            this.addLoding = true
            this.baseData.suppName = this.getCode(this.baseData.suppNo,this.suppNameType,'merchNo','merchName')
            this.baseData.testItemName = this.getCode(this.baseData.testItemNo,this.anlyType,'testItemNo','testItemName')
            this.baseData.matShapeName = this.getCode(this.baseData.matShapeCode,this.matShapeNameType,'baseCode','baseName')
            this.axios.post('pass/testManagement/v1/limstestdatas/insertupdate', 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.addLoding = false
                this.$message.error(res.message)
              }
            }).catch(e => {
              this.addLoding = false
              this.$message.error(e.message)
            })
          }
       })
      },
      closeDialog () {
        this.baseData = {
          suppNo: '',
          contractNo: '',
          matShapeCode: '',
          testItemNo: '',
          memo: ''
        }
        this.$refs['baseData'].clearValidate();
        this.addLoding = false
      },
    },
    watch: {
      showFlag(arg) {
        let _this = this;
        if (_this.Params !== null) {
          this.baseData = Object.assign({clearFlag:true},this.Params)
          _this.tableType = 1;
          _this.tableTypeDsc = '修改';
        } else {
          _this.tableType = 0;
          _this.tableTypeDsc = '新增';
        }
        this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
</style>
