
<!-- 新增物品信息 -->
<template>
  <div class="resource">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible">
     <div slot="title" class="titleBox">
        <i :class="formType === 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">{{formTypeDsc}}</span>
        </div>
        <!-- 表单 -->
         <!-- <div style="width: 640px;height: 500px;margin-bottom: 10px;margin-top: 15px;margin-left: 10px"> -->
       <el-form ref="formData" label-width="80px" size="mini" :model="formData" :rules="ruleValidate">
         <el-row>
        <!-- <el-col :span="8" class="tb-row">
          <el-form-item label="物品编码" prop="stuffNo">
            <el-input v-model="formData.stuffNo" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8" class="tb-row">
          <el-form-item label="物品名称" prop="stuffName">
            <el-input v-model="formData.stuffName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" class="tb-row">
          <el-form-item label="标样编号" prop="stuffNoUser">
            <el-input v-model="formData.stuffNoUser"></el-input>
          </el-form-item>
        </el-col>
          <el-col :span="8" class="tb-row">
          <el-form-item label="规格说明" style="white-space:nowrap" prop="stuffSpec">
            <el-input v-model="formData.stuffSpec"></el-input>
          </el-form-item>
         </el-col>
        </el-row>
        <el-row>
        <el-col :span="8" class="tb-row">
          <el-form-item label="物品类别" prop="stuffTypeCode">
           <el-select v-model="formData.stuffTypeCode" clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index) in stuffTypeNameList"
                    :key="index"
                    :label="item.baseName"
                    :value="item.baseCode">
                    </el-option>
                </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" class="tb-row">
          <el-form-item label="计量单位" prop="stuffUnit">
             <el-select v-model="formData.stuffUnit" clearable filterable collapse-tags size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in unitNameType" :key="index" :label="item.unitName"
                    :value="item.unitName" placeholder="请输入">
                  </el-option>
             </el-select>
            <!-- <el-input v-model="formData.stuffUnit"></el-input> -->
          </el-form-item>
        </el-col>
        <el-col :span="8" class="tb-row">
            <el-form-item label="库存预警" prop="stkAlarm">
                <el-input v-model="formData.stkAlarm"></el-input>
            </el-form-item>
        </el-col>
        </el-row>
         <el-row>
        <el-col :span="8" class="tb-row">
            <el-form-item label="标准单价（元）" prop="unitPrice" style="white-space:nowrap">
                <el-input v-model="formData.unitPrice"></el-input>
            </el-form-item>
        </el-col>
         <el-col :span="16" class="tb-row">
          <el-form-item label="备注" prop="memo">
            <el-input v-model="formData.memo"></el-input>
          </el-form-item>
        </el-col>
         </el-row>
        </el-form>
      <span slot="footer" class="dialog-footer">
       <el-button type="primary" icon="el-icon-check" size="mini" @click="formType === 0 ? addData('formData'):modifyData()">确定</el-button>
        <el-button size="mini" type="primary" 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: ['showFlag', 'Params'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '新增物品信息',
        formType: 0,
        seen: true,
        seenNo: true,
        beInvalid: false,
        formData : {
            unitNo: '',
            stuffNo:'',
            stuffName: '',
            stuffSpec: '',
            stuffTypeName: '',
            stuffTypeCode: '',
            stuffUnit: '',
            stkAlarm: '',
            unitPrice: '',
            memo: ''
         },
         stuffTypeNameList: [],//物品类别
         unitNameType: [],
         ruleValidate: {
            stuffName: [{
              required: true,
              message: '不能为空',
              trigger: 'change'
            }],
         }
    }
  },
  mounted () {
    this.getStuffTypeNameList ()
    this.getUnitNameTypeList ()
  },
    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] : '';
      },
       getStuffTypeNameList () {
         this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4813}, {individualType: 'json'})
            .then(res => {
              res.data.forEach(item => {
                if (item.baseCode !== '481311') { // 只查看非标准液类型的物料,标准液物料仅限在标准液界面查看
                  this.stuffTypeNameList.push(item)
                }
              })
            })
       },
       getUnitNameTypeList () {
         this.axios.post("pass/baseManagement/v1/limsbaseunits/queryLimsBaseUnitPage",{object:{validFlag:1},pageIndex:1,pageSize:9999}, {individualType: 'json'}).then(res => {
             this.unitNameType = res.data.list;
        })
       },
      cancel () {
        this.formData = {
          unitNo: '',
          stuffNo:'',
          stuffName: '',
          stuffSpec: '',
          stuffTypeName: '',
          stuffTypeCode: '',
          stuffUnit: '',
          stkAlarm: '',
          unitPrice: '',
          memo: ''
        };
        this.dialogTableVisible = false;
      },
      addData (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
             this.formData.createMan = userName;
             this.formData.createNo = userId;
            //  this.formData.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
             this.formData.stuffTypeName = this.getCode(this.formData.stuffTypeCode,this.stuffTypeNameList,'baseCode','baseName')
             this.axios.post('pass/baseManagement/v1/limslrcstuffs/', this.formData, {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 () {
       this.$refs['formData'].validate((valid) => {
        if (valid) {
        this.formData.updateMan = userName;
        this.formData.updateNo = userId;
        // this.formData.unitName = this.getCode(this.formData.unitNo,this.unitNameType,'unitNo','unitName')
        this.formData.stuffTypeName = this.getCode(this.formData.stuffTypeCode,this.stuffTypeNameList,'baseCode','baseName')
        this.axios.put('pass/baseManagement/v1/limslrcstuffs/', this.formData, {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.formData = {
            unitNo: '',
            stuffNo:'',
            stuffName: '',
            stuffSpec: '',
            stuffTypeName: '',
            stuffTypeCode: '',
            stuffUnit: '',
            stkAlarm: '',
            unitPrice: '',
            memo: ''
        };
       },
     },
   watch: {
      showFlag (arg) {
        this.dialogTableVisible = true;
        if (this.Params !== null) {
          this.formData = Object.assign({},this.Params)
          this.formType = 1;
          this.formTypeDsc = '修改物品信息';
          this.seen = false;
          this.beInvalid = true;
        } else {
          this.formType = 0;
          this.formTypeDsc = '新增物品信息';
           this.seenNo = false;
        }
      }
        }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
