<template>
  <el-dialog
    title="修改个人签名"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">
      <upload-component :title="'个人签名'" :accept="'.png,.jpeg,.jpg'" :file-obj-list="fileList" @uploadSuccess="uploadSuccess"/>
    </el-form>
    <span slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getMyInfo } from '@/api/my'
  import UploadComponent from './modules/common/upload-component'
  export default {
    components: {
      UploadComponent
    },
    data () {
      return {
        visible: false,
        fileList: [],
        dataForm: {},
        dataRule: {
        }
      }
    },
    computed: {
      userName: {
        get () { return this.$store.state.user.name }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      }
    },
    methods: {
      // 初始化
      init () {
        // 获取用户信息
        getMyInfo().then(({data}) => {
          if (data && data.code === '200' && data.data) {
            this.dataForm = data.data
            this.fileList = []
            data.data.attachList.forEach((item) => {
              this.fileList.push({
                name: item.fileName,
                url: item.url,
                id: item.url
              })
            })
          }
        })
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit () {
        let fList = this.fileList
        if (!fList || fList.length === 0) {
          this.$message.error('请上传文件')
          return
        }
        if (fList.length > 1) {
          this.$message.error('只能上传一个附件')
          return
        } else {
          this.dataForm.attachList = []
          for (let i = 0; i < fList.length; i++) {
            this.dataForm.attachList.push({
              fileName: fList[i].name,
              url: fList[i].url
            })
          }
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/biz-service/personal/setSignInfo'),
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      uploadSuccess (fileList) {
        this.fileList = fileList
      }
    }
  }
</script>

