<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>
        <span style="margin-left: 10px; font-size: 18px">{{
          tableTypeDsc
        }}</span>
      </div>
      <el-form
        ref="baseData"
        label-width="80px"
        :rules="rules"
        :model="baseData"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item prop="appName" label="名称">
              <el-input
                v-model="baseData.appName"
                size="mini"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="appDesc" label="描述">
              <el-input
                v-model="baseData.appDesc"
                size="mini"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="路径" prop="memo1">
              <el-input
                v-model="baseData.fileName"
                size="small"
                readonly
                disabled
                style="width: 192px; display: inline-block"
              ></el-input>
              <el-upload
                class="upload"
                action=""
                v-if="baseData.fileName - 0 === 0"
                style="width: 40px; display: inline-block"
                :multiple="false"
                :show-file-list="false"
                accept=""
                :http-request="httpRequestNew"
              >
                <el-button
                  type="goon"
                  size="mini"
                  icon="el-icon-upload2"
                ></el-button>
              </el-upload>
              <el-button
                v-else
                type="primary"
                size="mini"
                class="el-icon-circle-close"
                @click="dropFile"
              ></el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="appVer" label="版本号">
              <el-input
                v-model="baseData.appVer"
                size="mini"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="baseData.memo"
                size="mini"
                style="width: 100%"
              />
            </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"
          :loading="addLoading"
          @click="addData()"
          >确定</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'],
  data () {
    return {
      dialogTableVisible: false,
      tableTypeDsc: '新增app信息',
      rules: {
        appName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        appDesc: [{ required: true, message: '不能为空', trigger: 'blur' }],
        appVer: [{ required: true, message: '不能为空', trigger: 'blur' }],
      },
      baseData: {
        fileName: '',
        appName: '',
        appDesc: '',
        appVer: '',
        appPath: '',
        memo: ''
      },
      saveFlie: '',
      fileList: [],
      addLoading: false,
      tableType: 0,
      cretTypeNameType: [],
      uploadFile: {}
    }
  },
  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 = {
        fileName: '',
        appName: '',
        appDesc: '',
        appVer: '',
        appPath: '',
        memo: ''
      }
      this.fileList = []
      this.addLoading = false
      this.dialogTableVisible = false
    },
    addData () {
      this.$refs['baseData'].validate((valid) => {
        if (valid) {
          this.addLoading = true
          this.$refs.upload.submit()
        }
      })
    },
    savaData () {
      this.axios.post('pass/baseManagement/v1/limsbaseapps/uploadapp', this.baseData, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.dialogTableVisible = false;
          this.$message.success(res.message)
          this.$emit('refresh')
        } else {
          this.$message.error(res.message)
        }
        this.addLoading = false
      }).catch(e => {
        this.addLoading = false
        this.$message.error(e.message)
      })
    },
    fileChange (file, fileList) {
      this.saveFlie = file.raw
      this.fileList = [fileList[fileList.length - 1]]
      this.baseData.fileName = file.name
    },
    httpRequest: function (e) {
      if (!e.file) {
        return false
      }
      let file = new FormData()
      file.append('file', e.file)
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      this.axios.post('pass/baseManagement/v1/limsbaseapps/uploadapk', file, config).then(res => {
        if (res.code === '0' || res.code === '200') {
          this.baseData.appPath = ''
          this.baseData.appPath = res.data
          this.savaData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    // 文件上传
    async httpRequestNew (e) {
        let _this = this;
        if (!e.file) {
          return false
        }
        let file = new FormData()
        file.append('file', e.file)
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
        this.axios.post('pass/baseManagement/v1/file/upload', file, config).then(res =>{
          if (res.code === '0' || res.code === '200') {
            this.$message.success('上传成功')
            this.formData.memo1 = res.data[0].fileName
          } else {
            this.$message.error(res.message)
          }
        }).catch(e => {
          return this.$message.error(e.message)
      })
  },
    // 删除已有文件
    async dropFile () {
      let result = await this.$confirm('确认删除该文件重新上传吗？').catch(() => 'cancel')
      if (result == 'cancel') return
      var data
      try {
        if(this.formData.memo1){
           const data = await this.axios.get('pass/baseManagement/v1/file/delFile/'+ this.formData.memo1, { individualType: 'json' })
          if (data === '1') {
            this.formData.memo1 = ''
            this.$message.success('文件删除成功')
          } else {
            this.$message.error(data.message)
          }
        }else{
          this.formData.memo1 = ''
          this.$message.success('文件删除成功')
        }
      } catch (e) {
      }
    },
    closeDialog () {
      this.baseData = {
        appName: '',
        fileName: '',
        appDesc: '',
        appVer: '',
        appPath: '',
        memo: ''
      }
      this.fileList = []
      this.fileUpload = {}
      this.addLoading = false
    }
  },
  watch: {
    showFlag (arg) {
      if (this.Params !== null) {
        this.baseData = Object.assign({}, this.Params[0])
        this.tableType = 1;
        this.tableTypeDsc = '修改app信息';
      } else {
        this.tableType = 0;
        this.tableTypeDsc = '新增app信息';
      }
      this.dialogTableVisible = true;
    }
  }
}
</script>
<style lang="less">
</style>
