<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="250px">
      <el-form-item label="公司名称" >
        <el-input v-model="form.companyname" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="备案号" >
        <el-input v-model="form.recordno" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="Logo" >
        <!-- <el-input v-model="form.logoadr" style="width: 370px;"/>-->
        <el-upload
          :show-file-list="false"
          :on-success="handleSuccess"
          :on-error="handleError"
          :headers="headers"
          :action="updateLogoAdrApi"
          class="avatar-uploader">
          <img :src="form.logoadr==='' ? Logo : baseApi + '/file/'+form.logoadr" title="点击上传Logo" class="avatar" style="background-color: grey">
        </el-upload>
        <span style="color: #C0C0C0;margin-left: 10px;">点击图片更换Logo，推荐尺寸288*63px</span>
      </el-form-item>
      <!--<el-form-item label="用户" >
        <el-input v-model="form.userid" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="创建时间" >
        <el-input v-model="form.createTime" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="更新时间" >
        <el-input v-model="form.updateTime" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input v-model="form.nt" style="width: 370px;"/>
      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer" style="padding-left: 250px;">
      <!--<el-button type="text" @click="cancel">取消</el-button>-->
      <el-button :loading="loading" type="primary" @click="doEdit" >保存</el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del, loadSysByUserid } from '@/api/sSysmanage'
import { parseTime } from '@/utils/index'
import { getToken } from '@/utils/auth'
import { edit } from '@/api/sSysmanage'
import eForm from './form'
// import store from '@/store'
import Logo from '@/assets/logo/logo.png'
export default {
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      headers: {
        'Authorization': 'Bearer ' + getToken()
      },
      Logo: Logo,
      imageUrl: '',
      reqUrl: '',
      form: {
        id: '',
        companyname: '',
        recordno: '',
        logoadr: '',
        userid: '',
        createTime: '',
        updateTime: '',
        nt: ''
      },
      rules: {
      },
      delLoading: false,
      queryTypeOptions: [
        { key: 'companyname', display_name: '公司名称' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'updateLogoAdrApi',
      'baseApi'
    ])
  },
  created() {
    this.$nextTick(() => {
      this.init()
      this.loadSysByUserid()
      console.log(this.baseApi + '/file/80622-2019111106123697.jpg', '121213333333')
      console.log(this.form.logoadr, '4444')
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'api/sSysmanage'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    loadSysByUserid() {
      loadSysByUserid().then(res => {
        console.log(res, '111')
        this.form.id = res.id
        this.form.companyname = res.companyname
        this.form.recordno = res.recordno
        this.form.logoadr = res.logoadr
        this.form.userid = res.userid
      })
    },
    doEdit() {
      this.loading = true
      console.log(this.form)
      if (this.form.companyname === '') {
        this.$notify({
          title: '请输入公司名称！',
          type: 'warning',
          duration: 2500
        })
        return
      }
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        /* this.$parent.init()*/
        this.loadSysByUserid()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        companyname: data.companyname,
        recordno: data.recordno,
        logoadr: data.logoadr,
        userid: data.userid,
        createTime: data.createTime,
        updateTime: data.updateTime,
        nt: data.nt
      }
      _this.dialog = true
    },
    handleSuccess(response, file, fileList) {
      this.$notify({
        title: 'Logo修改成功',
        type: 'success',
        duration: 2500
      })
      this.loadSysByUserid()
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
    }
  }
}
</script>

<style scoped>

</style>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 288px;
        height: 63px;
        display: block;
    }
</style>
