<template>
  <div class="sys-container">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="网站名称">
        <el-input
          v-model="form.name"
          size="small"
          placeholder="请输入网站名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input
          v-model="form.phone"
          size="small"
          placeholder="请输入联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item label="LOGO">
        <el-upload
          class="avatar-uploader"
          :action="fileUpdata"
          name="files"
          :headers="{ token: token }"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.logo1" :src="form.logo1" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input
          v-model="form.keyword"
          type="textarea"
          size="small"
          placeholder="请输入关键字"
        ></el-input>
      </el-form-item>
      <el-form-item label="统计代码">
        <el-input
          v-model="form.statsCode"
          type="textarea"
          size="small"
          placeholder="请输入统计代码"
        ></el-input>
      </el-form-item>
      <el-form-item label="备案号">
        <el-input
          v-model="form.record"
          type="textarea"
          size="small"
          placeholder="请输入备案号"
        ></el-input>
      </el-form-item>
      <el-form-item label="版权">
        <el-input
          v-model="form.copyright"
          size="small"
          placeholder="请输入版权"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="small" @click="save">保存</el-button>
  </div>
</template>

<script>
import { doEdit, doAdd, getData } from '@/api/sys';
import { baseURL } from '@/config';
import { mapGetters } from 'vuex';
import store from '@/store';
export default {
  name: 'SysConfig',
  data() {
    return {
      form: {
        id: '',
        logo: '',
        logo1: '',
        name: '',
        phone: '',
        keyword: '',
        statsCode: '',
        record: '',
        copyright: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入网站名称' }],
      },
      fileUpdata: '',
      token: '',
      sysData: [],
      clearable: true,
      title: '',
      dialogFormVisible: false,
    };
  },
  computed: {
    ...mapGetters(['fileUpload']),
  },
  created() {
    this.showConfig();
    this.getWebData();
  },
  methods: {
    async getWebData() {
      const { data } = await getData();
      if (data) {
        if (data.logo) {
          this.form.logo1 = baseURL + '/static/img/' + data.logo;
        }
        this.form = data;
      }
    },
    showConfig() {
      this.fileUpdata = baseURL + '/file/uploadImg';
      this.token = store.getters['user/token'];
      console.log(this.token);
    },
    handleAvatarSuccess(res, file) {
      this.form.logo1 = baseURL + '/static/img/' + res.data;
      this.form.logo = res.data;
    },
    beforeAvatarUpload(file) {
      //const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2;
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      // return isJPG && isLt2M
      return isLt2M;
    },
    showEdit(row) {
      this.form.id = row.id;
      this.form = Object.assign({}, row);
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          delete this.form.logo1;
          if (this.form.id) {
            const { msg } = await doEdit(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          } else {
            delete this.form.id;
            const { msg } = await doAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          // this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 10px !important;
}
.sys-container {
  width: 30%;
}
.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>
