<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:7px;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="matClassNo" label="物料分类">
                <!-- <el-input v-model="baseData.matClassName" readonly disabled size="small"></el-input> -->
                <el-select v-model="baseData.matClassNo" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in baseDataTree" :key="index" :label="item.matClassName" :value="item.matClassNo">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="是否控制">
                <el-select v-model="baseData.isQuota" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in isTestType" :key="item.id" :label="item.isTest" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <!-- 是否要 -->
              <el-form-item prop="matNo" label="物料编码">
                <el-input v-model="baseData.matNo"  size="small"  @blur="myBlur" ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="matName" label="物料名称">
                <el-input v-model="baseData.matName"  size="small"  @blur="myBlur"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="matOrder" label="顺序号">
                <el-input v-model="baseData.matOrder"  size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="是否测控">
                <el-select v-model="baseData.isTest" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in isTestType" :key="item.id" :label="item.isTest" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item prop="matAbbr" label="质保书名称">
                <el-input v-model="baseData.matAbbr"  size="small"  @blur="myBlur" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="matEngAbbr" label="查询标识">
                <el-input v-model="baseData.matEngAbbr"  size="small"  @blur="myBlur" style="width: 100%;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="是否计量">
                <el-select v-model="baseData.isMater" filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="item in isTestType" :key="item.id" :label="item.isTest" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item 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);
import pinyinUtils from '../../../../utils/pinyin/pinyinUtils.js';
export default {
  props: ['showFlag', 'Params', 'Params2', 'Params2', 'baseDataTree'],
    data () {
       var rsvMaxVerify = (rule, value, callback) => {
	        if(this.baseData.rsvMax){
	          if(Number(this.baseData.rsvMax) > Number(this.baseData.rsvMin)){
	            callback();
	          }else{
	            callback(new Error('最高库存不能小于最低库存'));
	          }
	        }else{
	          callback();
          }
       };
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增物料信息',
            tableType: 0,
            seen: true,
            seenNo: true,
            beInvalid: false,
            stdNameTypeIndex: '',
            isTestTypeIndex: 0,
            isBatchTypeIndex: 0,
            isMaterTypeIndex: 0,
            isQuotaTypeIndex: 0,
            stdNameType: [],
            isTestType: [
              {id: '1', isTest: '是'},
              {id: '0', isTest: '否'}
            ],
            isBatchType: [
              {id: 1, isBatch: '是'},
              {id: 0, isBatch: '否'}
            ],
            isMaterType: [
              {id: 1, isMater: '是'},
              {id: 0, isMater: '否'}
            ],
            isQuotaType: [
              {id: 1, isQuota: '是'},
              {id: 0, isQuota: '否'}
            ],
            tableData: [],
            baseData: {
              matAbbr: '',
              matEngAbbr: '',
              matClassNo: '',
              matClassName: '',
              rsvMed: '',
              matNo: '',
              matName: '',
              matAbbr: '',
              matEngName: '',
              matEngAbbr: '',
              matMnemonic: '',
              curtempNo: '',
              matOrder: '',
              matBrand: '',
              matModel: '',
              matSources: '',
              rsvMed: '',
              rsvMax: '',
              matAbbrNo: '',
              rsvMin: '',
              isQuota:'0',
              isMater:'0',
              isTest:'0'
            },
            ruleValidate: {
              matClassNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              matName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // rsvMax: [{ required: true, message: '该项不能为空', trigger: ['blur', 'change']},{ required: true,validator: rsvMaxVerify, trigger: 'blur' }],
              // rsvMin: [{ required: true, message: '该项不能为空', trigger: ['blur', 'change']}]
              //dataTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              //matClassNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // dataTypeName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.getStdNameTypeList();
    },
    methods: {
      getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        return targetRow ? targetRow[nameKey] : '';
      },
      cancel () {
        let _this = this;
        _this.tableData = [];
        _this.stdNameTypeIndex = 0,
        _this.isTestTypeIndex = 0,
        _this.isBatchTypeIndex = 0,
        _this.isMaterTypeIndex = 0,
        _this.isQuotaTypeIndex = 0,
        _this.baseData = {
              matAbbr: '',
              matEngAbbr: '',
              matClassNo: '',
              matClassName: '',
              rsvMed: '',
              matNo: '',
              matName: '',
              matAbbr: '',
              matEngName: '',
              matEngAbbr: '',
              matMnemonic: '',
              curtempNo: '',
              matOrder: '',
              matBrand: '',
              matModel: '',
              matSources: '',
              rsvMed: '',
              rsvMax: '',
              matAbbrNo: '',
              rsvMin: '',
              isQuota:'0',
              isMater:'0',
              isTest:'0'
        }
        _this.dialogTableVisible = false
      },
      addData () {
        let _this = this;
         _this.tableData = [];
         _this.$refs['baseData'].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.baseDataTree,'matClassNo','matClassName')
             if (_this.isBatchTypeIndex === 0)  {
               _this.baseData.isBatch = '1'
             } else {
               _this.baseData.isBatch = '0'
             }
             _this.tableData.push(_this.baseData)
             console.log("新增物料",_this.tableData)
             _this.axios.post('pass/baseManagement/v1/limsbasematerialss/', _this.tableData, {individualType: 'json'}).then(res => {
              console.log("res",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.updateMan = userName;
        this.baseData.updateNo = userId;
        this.baseData.matClassName = this.getCode(this.baseData.matClassNo,this.baseDataTree,'matClassNo','matClassName')
        if (_this.isBatchTypeIndex === 0)  {
          _this.baseData.isBatch = '1'
        } else {
          _this.baseData.isBatch = '0'
        }
        _this.axios.put('pass/baseManagement/v1/limsbasematerialss/', _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 () {
       let _this = this;
       _this.tableData = [];
       _this.stdNameTypeIndex = 0,
       _this.isTestTypeIndex = 0,
       _this.isBatchTypeIndex = 0,
       _this.isMaterTypeIndex = 0,
       _this.isQuotaTypeIndex = 0,
       _this.baseData = {
             matAbbr: '',
             matEngAbbr: '',
             matClassNo: '',
             matClassName: '',
             rsvMed: '',
             matNo: '',
             matName: '',
             matAbbr: '',
             matEngName: '',
             matEngAbbr: '',
             matMnemonic: '',
             curtempNo: '',
             matOrder: '',
             matBrand: '',
             matModel: '',
             matSources: '',
             rsvMed: '',
             rsvMax: '',
             matAbbrNo: '',
             rsvMin: '',
            isQuota:'0',
            isMater:'0',
            isTest:'0'
       }
       _this.seen = true;
       _this.seenNo = true;
       _this.beInvalid = false;
     },
     getStdNameTypeList () {
       let obj= {
         object:{validFlag:1},
         pageIndex:1,
         pageSize:9999
       }
        this.axios.post('pass/baseManagement/v1/limsstdinfos/queryLimsStdInfoList/',obj,{individualType: 'json'}).then(res => {
          this.stdNameType = res.data.list;
        })
      },
      tableHeaderCellStyle () {
      return 'background-color: rgb(249,249,249) !important;color: rgb(73, 72, 72);border-bottom: 1px solid #DCDFE7;border-top:3px solid #3ac1ea;'
      },
       myBlur () {
        if (this.baseData.matName) {
          this.baseData.matMnemonic = pinyinUtils.initial(this.baseData.matName);
        }
      }
    },
    watch: {
      showFlag (arg) {
        let _this = this;
        _this.dialogTableVisible = true;
        if (_this.Params !== null) {
          _this.baseData = Object.assign({},this.Params[0])
          _this.tableType = 1;
          _this.tableTypeDsc = '修改物料信息';
          _this.seen = false;
          _this.beInvalid = true;
            if (_this.Params[0].isBatch === '0')  {
              _this.isBatchTypeIndex = 1
            } else {
              _this.isBatchTypeIndex = 0
            }
        } else {
          _this.tableType = 0;
          _this.seenNo = false;
          _this.baseData.matClassNo = _this.Params2.matClassNo;
          _this.baseData.matClassName = _this.Params2.matClassName;
          // _this.baseData.merchNo = _this.Params.merchNo;
          _this.tableTypeDsc = '新增物料信息';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_mat{
  width: 800px !important
}
</style>
