<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="100px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="12">
            <el-form-item prop="matName" label="物料名称">
              <el-input v-model="baseData.matName" readonly style="width:100%" disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="dataSourceCode" label="对照系统名称">
             <el-select v-model="baseData.dataSourceCode" filterable style="width:100%" collapse-tags size="small" >
                <el-option v-for="(item,index) in dataSourceNameType" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
            <el-form-item prop="mtMat" label="对照物料编号">
              <el-input v-model="baseData.mtMat" style="width:100%" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item prop="mtMatName" label="对照物料名称">
              <el-input v-model="baseData.mtMatName"  style="width:100%" size="small"></el-input>
            </el-form-item>
           </el-col>
         </el-row>
          <el-row>
            <el-col :span="24">
             <el-form-item 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';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  props: ['showFlag2', 'Params', 'Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增第三方物料对照信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            dataSourceNameTypeIndex: 0,
            dataSourceNameType: [],
            tableData: [],
            baseData: {
              matNo: '',
              matName: '',
              memo: '',
              mtMatNo: '',
              mtMat: '',
              dataSourceCode: '',
              mtMatName: ''
            },
            ruleValidate: {
              //mtMatNo: [{ required: true, message: '该项不能为空', trigger: 'change' }]
              dataSourceCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              //matNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // dataSourceName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
       this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode",{validFlag:1,baseCode:4807}, {individualType: 'json'}).then(response => {
          this.dataSourceNameType = response.data;
        })
    },
    methods: {
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.dataSourceNameTypeIndex = 0,
        _this.baseData = {}
        _this.dialogTableVisible = false
      },
      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] : '';
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             this.baseData.dataSourceName = this.getCode(this.baseData.dataSourceCode,this.dataSourceNameType,'baseCode','baseName')
             _this.axios.post('pass/baseManagement/v1/limsbasematerialsmatchs/', [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.tableData = [];
        _this.$refs['baseData'].validate((valid) => {
        if (valid) {
          this.baseData.dataSourceName = this.getCode(this.baseData.dataSourceCode,this.dataSourceNameType,'baseCode','baseName')
        _this.axios.put('pass/baseManagement/v1/limsbasematerialsmatchs/', _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)
          })
        }
        })
      },
      renderHeader (h) {
         return (
             <div style="cursor: pointer ;line-height:20px" onClick={() => this.addRow()}>
             <i class="el-icon-plus" style="font-size:20px; color:rgb(18, 52, 243)"></i>
             </div>
         )
      },
     closeDialog () {
       let _this = this;
       _this.tableData = [];
       _this.dataSourceNameTypeIndex = 0,
        _this.baseData = {}
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params2 !== null) {
          _this.baseData = Object.assign({},_this.Params2[0])
          _this.tableType = 1;
          _this.tableTypeDsc = '修改第三方物料对照信息';
          _this.seen = false;
          _this.beInvalid = true;
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.matNo = _this.Params[0].matNo;
          _this.baseData.matName = _this.Params[0].matName;
          // _this.baseData.merchNo = _this.Params.merchNo;
          _this.tableTypeDsc = '新增第三方物料对照信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>

