<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="120px">
      <el-form-item label="平台LOGO：" prop="system_logo">
        <el-col :span="12">
          <el-upload
            class="avatar-uploader"
            :action="baseUrl+upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="formData.system_logo" :src="baseUrl+formData.system_logo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-col>
      </el-form-item>
      <el-form-item label="版本号：" prop="system_version">
        <el-col :span="12">
          <el-input v-model="formData.system_version" placeholder="请输入版本号"/>
        </el-col>
      </el-form-item>
      <el-form-item label="平台介绍：" prop="system_message">
        <el-col :span="12">
          <el-input type="textarea" :rows="6" v-model="formData.system_message" placeholder="请输入平台介绍"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话：" prop="system_phone">
        <el-col :span="12">
          <el-input v-model="formData.system_phone" placeholder="请输入联系电话"/>
        </el-col>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkRqInput, checkTel } from '@/utils/validate'
import { detailSystem, updateSystem, upload  } from '@/api/system/index'
import { baseUrl } from '@/utils/env.js'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {},
      // 校验规则
      formRules: {
        system_logo: [ checkRqInput ],
        system_version: [ checkRqInput ],
        system_message: [ checkRqInput ],
        system_phone: [ checkRqInput, checkTel ],
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
      baseUrl, 
      upload,
    }
  },
  created() {
    this.fetchDetail()
  },
  methods: {
    // 获取设置详情
    fetchDetail() {
      this.$http(detailSystem(), res => {
        if (res.code == 0) {
          this.formData = res.data
        }
      })
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          this.$http(updateSystem(this.formData), res => {
            if (res.code == 0) {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.fetchDetail()
            }
          })
        } else {
          return false
        }
      })
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 0) {
        this.formData.system_logo = res.data
      } else {
        this.$message({
          message: '请重新上传',
          type: 'warning'
        })
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return isLt2M;
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .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: 178px;
    height: 178px;
    display: block;
  }
</style>
