
<!-- 新增物品信息 -->
<template>
  <div class="resource">
      <el-dialog @close="closeDialog" :visible.sync="dialogTableVisible" width="60%">
      <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>
        <span style="padding-left: 330px;font-size: 16px; color: #fff">测试项目名称</span>
        <el-input
          v-model="testItemName"
          size="small"
          @keyup.enter.native="type"
          style="padding-left: 10px;width: 150px "
        ></el-input>
        <el-button
          icon="el-icon-search"
          type="goon"
          size="mini"
          @click="type"
          >查询</el-button>
      </div>
        <!-- 表单 -->
        <el-form ref="formData" label-width="90px" size="mini" :model="formData" :rules="ruleValidate">
          <el-col :span="10">
            <el-row>
              <el-form-item label="溶液编号" prop="stuffNoUser">
                <el-input v-model="formData.stuffNoUser"></el-input>
              </el-form-item>
              <el-form-item label="溶液名称" prop="stuffName">
                <el-input v-model="formData.stuffName"></el-input>
              </el-form-item>
              <el-form-item label="溶液溶度/单位" prop="stuffSpec">
                <el-input v-model="formData.stuffSpec"></el-input>
              </el-form-item>
              <el-form-item label="配制日期" prop="memo1" style="white-space:nowrap">
                <el-date-picker v-model="formData.memo1" style="width: 100%;" type="datetime" placeholder="选择时间" size="small" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
              <el-form-item label="有效期至" prop="memo2" style="white-space:nowrap">
                <el-date-picker v-model="formData.memo2" style="width: 100%;" type="datetime" placeholder="选择时间" size="small" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
              <el-form-item label="库存数量" prop="stkAlarm">
                <el-input v-model="formData.stkAlarm"></el-input>
              </el-form-item>
              <el-form-item label="备注" prop="memo">
                <el-input v-model="formData.memo"></el-input>
              </el-form-item>
            </el-row>
          </el-col>
          <el-col :span="14">
            <div style="padding: 0 0 10px 10px">
              <el-table ref="dataTable2" border :height="300"
                :data="listLog" @selection-change="selectListLog">
                <el-form-item label="测试项目" prop="testItemName">
                  <el-input v-model="formData.testItemName"></el-input>
                  <el-button icon="el-icon-search" type="goon" size="mini" @click="type">查询</el-button>
                </el-form-item>
                <el-table-column type="selection" width="45" fixed="left" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="testItemName" label="测试项目名称" align="center"></el-table-column>
                <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="itemTypeName" label="项目类型" align="center"></el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-form>
      <span slot="footer" class="dialog-footer">
       <el-button type="primary" icon="el-icon-check" size="mini" :loading="addLoading" @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: ['showFlag1', 'Params'],
  data () {
    return {
        dialogTableVisible: false,
        formTypeDsc: '新增物品信息',
        formType: 0,
        listLog: [],//任务列表
        selectList: [],
        seen: true,
        seenNo: true,
        addLoading: false,
        beInvalid: false,
        testItemName:'',
        formData : {
          stuffNoUser: '',
            unitNo: '',
            stuffNo:'',
            stuffName: '',
            stuffSpec: '',
            stuffTypeName: '',
            stuffTypeCode: '',
            stuffUnit: '',
            stkAlarm: '',
            unitPrice: '',
            testItemName:'',
            memo: '',
            memo1: '',      // 配制日期
            memo2: ''       // 有效期至
         },
         stuffTypeNameList: [],//物品类别
         ruleValidate: {
           stuffNoUser: [{required: true, message: '不能为空', trigger: 'change'}],
            stuffName: [{required: true, message: '不能为空', trigger: 'change'}]
         }
    }
  },
  mounted () {
    this.type()
    this.getStuffTypeNameList ()
  },
    methods: {
      type(){
        let obj = {
          object:{
              validFlag:'1',
              itemTypeCode: '480408',
              testItemName:this.testItemName,
              },
              pageIndex:1,
              pageSize:9999,
          }
          this.axios.post('pass/baseManagement/v1/limsstdtestitems/queryLimsStdTestItemPage',obj, {individualType: 'json'}).then(res => {
              if (res.code == 0 || res.code == 200) {
                this.listLog = res.data.list;
              } else {
                return this.$message.warning(res.message)
              }
          }).catch(e => {
            return this.$message.error(e.message)
          });
      },
      getYNM (date) {
        let year = date.getFullYear().toString();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let min = date.getMinutes();
        let sec = date.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        hours = hours < 10 ? "0" + hours : hours;
        min = min < 10 ? "0" + min : min;
        sec = sec < 10 ? "0" + sec : sec;
        return `${year}-${month}-${day} ${hours}:${min}:${sec}`;
      },
       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 => {
              this.stuffTypeNameList= res.data;
              this.formData.stuffTypeCode = '481311'
            })
       },
      cancel () {
        this.formData = {
          stuffNoUser: '',
          unitNo: '',
          stuffNo:'',
          stuffName: '',
          stuffSpec: '',
          stuffTypeName: '',
          stuffTypeCode: '',
          stuffUnit: '',
          stkAlarm: '',
          unitPrice: '',
          memo: '',
          memo1: '',      // 配制日期
          memo2: ''       // 有效期至
        };
        this.dialogTableVisible = false;
      },
      selectListLog(val){
        this.selectList = val;
      },
      addData (formName) {
        if(this.selectList.length < 1){
          this.$message.warning("请先勾选一条数据");
          return;
        }
        this.addLoading = true
        this.$refs[formName].validate((valid) => {
          if (valid) {
             this.formData.createMan = userName;
             this.formData.createNo = userId;
             this.formData.stuffTypeName = this.getCode(this.formData.stuffTypeCode,this.stuffTypeNameList,'baseCode','baseName')
             let obj = {
               object : this.formData,
               list : this.selectList
             }
             this.axios.post('pass/testManagement/v1/limslrcstuffs/addList', obj, {individualType: 'json'}).then(res => {
                if (res.succeed) {
                  this.dialogTableVisible = false;
                  this.$message.success('添加成功')
                  this.$emit('refresh')
                } else {
                 this.$message.error(res.message)
                }
                this.addLoading = false
                }).catch(e => {
                  this.addLoading = false
                 this.$message.error(e.message)
               })
              }
           })
      },
     modifyData () {
       this.$refs['formData'].validate((valid) => {
        if (valid) {
        this.formData.updateMan = userName;
        this.formData.updateNo = userId;
        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 = {
            stuffNoUser: '',
            unitNo: '',
            stuffNo:'',
            stuffName: '',
            stuffSpec: '',
            stuffTypeName: '',
            stuffTypeCode: '',
            stuffUnit: '',
            stkAlarm: '',
            unitPrice: '',
            memo: '',
            memo1: '',      // 配制日期
            memo2: ''       // 有效期至
        };
       },
     },
   watch: {
      showFlag1 (arg) {
        this.dialogTableVisible = true;
        if (this.Params == null) {
          this.formData = Object.assign({},this.Params)
          this.formType =1;
          this.formData.stuffTypeCode = '481311'
          this.formTypeDsc = '修改物品信息';
          this.seen = false;
          this.beInvalid = true;
        } else {
          this.formType = 0;
          this.formData = Object.assign({},this.Params)
          this.formData.stuffTypeCode = '481311'
          this.formTypeDsc = '新增物品信息';
          this.formData.memo1 = this.getYNM(new Date());
          this.seenNo = false;
        }
      }
        }
}
</script>
<style lang="less">
.tb-row {
    margin-bottom: 2px !important;
}
</style>
