
<!-- 新增文件信息弹窗 -->
<template>
  <div class="resource" style="height: 100%">
      <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>
        <!-- 表单 -->
      <el-form ref="formData" label-width="65px" size="mini" :model="formData">
        <el-row>
        <!-- <el-col :span="8">
          <el-form-item label="文件Id" prop="fileId">
            <el-input v-model="formData.fileId" :disabled="true"></el-input>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="8">
          <el-form-item label="文件编号" prop="fileNo">
            <el-input v-model="formData.fileNo"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="文件名称" prop="fileName">
            <el-input v-model="formData.fileName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
            <el-form-item label="文件级别" prop="fileLevel">
               <el-select v-model="formData.fileLevel" clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index) in fileLevelList"
                    :key="index"
                    :label="item.baseName"
                    :value="item.baseName">
                    </el-option>
                </el-select>
                </el-form-item>
        </el-col>
         <el-col :span="8">
            <el-form-item label="文件来源" prop="fileSource">
                <el-select v-model="formData.fileSource" clearable placeholder="请选择">
                    <el-option
                    v-for="(item,index) in fileSourceList"
                    :key="index"
                    :label="item.fileSourceName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
          <el-form-item label="文件类别" prop="fileTypeCode">
           <el-select v-model="formData.fileTypeCode" clearable placeholder="请选择">
                    <el-option
                    v-for="(item, index) in fileTypeNameList"
                    :key="index"
                    :label="item.baseName"
                    :value="item.baseCode">
                    </el-option>
                </el-select>
          </el-form-item>
        </el-col>
         <el-col :span="8">
            <el-form-item label="是否受控" prop="udrCtrl">
                <el-select v-model="formData.udrCtrl" clearable placeholder="请选择">
                    <el-option
                    v-for="(item,index) in udrCtrlList"
                    :key="index"
                    :label="item.udrCtrlName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-col>
          <el-col :span="8">
          <el-form-item label="备注" prop="memo">
            <el-input size="mini" 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():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 : {
           fileId: '',
           // fileNo: '',
           fileTypeCode: '',
           fileName: '',
           fileLevel: '',
           fileSource: '',
           fileTypeName: '',
           udrCtrl: '',
           memo: ''
        },
        fileLevelList: [],//文件级别
        fileTypeNameList: [],//文件分类
        udrCtrlList: [
          {id: '0', udrCtrlName: '否'},
          {id: '1', udrCtrlName: '是'}
        ],
        fileSourceList: [
          {id: '0', fileSourceName: '内部'},
          {id: '1', fileSourceName: '外部'}
        ]
    }
  },
   mounted () {
    this.getFileLevelList ()
    this.getFileTypeNameList ()
    // this.udrCtrlList ()
    // this.fileSourceList ()
  },
     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] : '';
        },
        getFileTypeNameList () {
         this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4809}, {individualType: 'json'})
            .then(res => {
              this.fileTypeNameList= res.data;
            })
       },
        getFileLevelList () {
         this.axios.post('pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode',{validFlag:1,baseCode:4839}, {individualType: 'json'})
            .then(res => {
              this.fileLevelList = res.data;
            })
       },
       cancel () {
        this.formData = {
            fileId: '',
            // fileNo: '',
            fileTypeCode: '',
            fileName: '',
            fileLevel: '',
            fileSource: '',
            fileTypeName: '',
            udrCtrl: '',
            memo: ''
        };
        this.dialogTableVisible = false;
      },
       addData () {
         this.$refs['formData'].validate((valid) => {
           if (valid) { 
             this.formData.createMan = userName;
             this.formData.createNo = userId;
             this.formData.fileTypeName = this.getCode(this.formData.fileTypeCode,this.fileTypeNameList,'baseCode','baseName')
             this.axios.post('pass/baseManagement/v1/limslrcfiles/', 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.fileTypeName = this.getCode(this.formData.fileTypeCode,this.fileTypeNameList,'baseCode','baseName')
            this.axios.put('pass/baseManagement/v1/limslrcfiles/', 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 = {
             fileId: '',
             // fileNo: '',
             fileTypeCode: '',
             fileName: '',
             fileLevel: '',
             fileSource: '',
             fileTypeName: '',
             udrCtrl: '',
             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">
</style>
