<template>
    <div class="examination">
     <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" customClass="customWidth_syst">
        <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="itemName" label="项目名称">
              <el-input v-model="baseData.itemName" readonly disabled size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="matClassNo" label="物料类型">
               <el-select v-model="baseData.matClassNo" @change="selctMatName" clearable style="width:100%" filterable collapse-tags size="small" >
                  <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-col :span="8">
            <el-form-item prop="matNo" label="物料名称">
               <el-select v-model="baseData.matNo" clearable  style="width:100%" filterable collapse-tags size="small" >
                  <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="10">
            <el-form-item prop="evaLevelName" label="评价等级">
             <el-select v-model="evaLevelNameTypeIndex" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in evaLevelNameType" :key="index" :label="item.baseName" :value="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" 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,
            matClassNameTypeIndex: 0,
            matNameTypeIndex: 0,
            //evaLevelNameTypeIndex: 0,
            matClassNameType: [],
            matNameType: [],
            //evaLevelNameType: [],
            tableData: [],
            baseData: {
              itemNo: '',
              itemName: '',
              memo: '',
              icNo: ''
            },
            ruleValidate: {
              itemName: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
              matClassNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              matNo: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    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;
      });
      // console.log(targetRow, 'targetRow');
      return targetRow ? targetRow[nameKey] : '';
    },
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.matClassNameTypeIndex = 0,
        _this.matNameTypeIndex = 0,
       // _this.evaLevelNameTypeIndex = 0,
        _this.baseData = {
              itemNo: '',
              itemName: '',
              memo: '',
              icNo: ''
        }
        _this.dialogTableVisible = false
        _this.$emit('refresh')
      },
      selctMatName(val){
      this.matNameType = []
      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;
    })
    },
      addData () {
        let _this = this;
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.matName = this.getCode(this.baseData.matNo,this.matNameType,'matNo','matName')
              _this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.matClassNameType,'matClassNo','matClassName')
             _this.axios.post('pass/baseManagement/v1/limsbaseitemcostds/addLimsBaseItemCostD', _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.matName = this.getCode(this.baseData.matNo,this.matNameType,'matNo','matName')
        _this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.matClassNameType,'matClassNo','matClassName')
        _this.axios.post('pass/baseManagement/v1/limsbaseitemcostds/addLimsBaseItemCostD', _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.baseData = {}
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
    },
    watch: {
      showFlag2 (arg) {
        let _this = this;
        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.itemNo = _this.Params[0].itemNo;
          _this.baseData.itemName = _this.Params[0].itemName;
          _this.baseData.icNo = _this.Params[0].icNo;
          _this.tableTypeDsc = '新增物料价格信息';
        }
        _this.dialogTableVisible = true;
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
