<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="12">
            <el-form-item prop="postName" label="岗位名称">
              <el-input v-model="baseData.postName" size="small"  :disabled="true"></el-input>
            </el-form-item>
          </el-col>  
         </el-row>
         <el-row>
          <el-col :span="12">
            <el-form-item prop="shiftName" label="项目名称">
              <el-input v-model="baseData.shiftName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="shiftTypeCode" label="项目类型">
              <el-select v-model="baseData.shiftTypeCode" filterable collapse-tags size="small">
                <el-option v-for="(item,index) in anlyItemNameType" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
           <el-col :span="24">
            <el-form-item prop="shiftDesc" label="项目描述">
              <el-input v-model="baseData.shiftDesc" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
            <el-col :span="24">
            <el-form-item prop="defaultValue" label="默认值">
              <el-input v-model="baseData.defaultValue" size="small"></el-input>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row>
          <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: ['showFlag', 'Params','Params2'],
    data () {
        return {
            dialogTableVisible: false,
            tableTypeDsc: '新增交接班项目',
            tableType: 0,
            anlyItemNameType: [],
            anlyItemName :[],
              page: {
              pageSize: 50,
              pageNum: 1,
              totalPage: 0
            },
            baseData: {
              shiftNo: '',
              postName: '',
              postNo:'',
              shiftName: '',
              shiftDesc: '',
              defaultValue: '',
              memo: '',
              validFlag:'',
              createMan:'',
              createNo:'',
              shiftTypeName:'',
              shiftTypeCode:'',
              updateNo: '',
              updateMan:''
            },
            ruleValidate: {
              shiftName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              shiftTypeCode: [{ required: true, message: '该项不能为空', trigger: 'change' }],
              // reviseRuleName: [{ required: true, message: '该项不能为空', trigger: 'change' }]
             }
        }
    },
    mounted () {
      this.axios.get('pass/baseManagement/v1/limsbaseinfos/like/'+ '?baseCode=4812')
         .then(res => {
         this.anlyItemNameType=res.data.list;
      })
    },
    methods: {
      getCode(code, arr, codeKey, nameKey) {
        if (!code) {
          return "";
        }
        let targetRow = arr.find((item) => {
          return item[codeKey] === code;
        });
        return targetRow ? targetRow[nameKey] : "";
      }, 
      cancel () {
        this.baseData = {
             shiftNo: '',
              postName: '',
              postNo:'',
              shiftName: '',
              shiftDesc: '',
              defaultValue: '',
              memo: '',
              validFlag:'',
              createMan:'',
              createNo:'',
              shiftTypeName:'',
              shiftTypeCode:''
        },
        this.dialogTableVisible=false
      },
      addData () {
         this.$refs['baseData'].validate((valid) => {
          if (valid) {
             this.baseData.createMan = userName;
             this.baseData.createNo = userId;
             this.baseData.shiftTypeName = this.getCode(this.baseData.shiftTypeCode,this.anlyItemNameType,'baseCode','baseName')
             this.baseData.validFlag=1;
             console.log(this.baseData)
              this.axios.post('pass/baseManagement/v1/limsbaseshiftitems/', 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 () {
        this.$refs['baseData'].validate((valid) => {
        if (valid) {
        this.baseData.updateMan = userName;
        this.baseData.updateNo = userId;
        this.baseData.shiftTypeName = this.getCode(this.baseData.shiftTypeCode,this.anlyItemNameType,'baseCode','baseName')
        console.log(this.baseData)
        this.axios.put('pass/baseManagement/v1/limsbaseshiftitems/'+this.baseData.shiftNo, 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 () {
       this.baseData = {
              shiftNo: '',
              postName: '',
              postNo:'',
              shiftName: '',
              shiftDesc: '',
              defaultValue: '',
              memo: '',
              validFlag:'',
              createMan:'',
              createNo:'',
              shiftTypeName:'',
              shiftTypeCode:''
       }
     },
    },
    watch: {
      showFlag (arg) {
        this.dialogTableVisible = true;
        if (this.Params2 !== null) {
          this.baseData = Object.assign({},this.Params2);
          this.tableType = 1;
          this.tableTypeDsc = '修改交接班项目';
        } else {
          this.tableType = 0;
          this.baseData.postName = this.Params.postName;//岗位名称名称
          this.baseData.postNo=this.Params.postNo;//岗位编号
          this.baseData.cretNoM = '';//项目类型
        //  this.baseData.merchNo = this.Params.merchNo;
          this.tableTypeDsc = '新增交接班项目';
        }
      }
    }
}
</script>
<style lang="less">
.customWidth_syst{
  width: 600px !important
}
</style>
