<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="60px">
      <el-form-item label="名字" prop="name">
        <el-input
          v-model.trim="form.name"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('alarm.bz')" prop="remark">
        <el-input
          v-model.trim="form.remark"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('sensor.tp')" prop="icon">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :headers="headers"
          name="files"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img :src="filePath" class="avatar" />
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" size="small" @click="save">确 定</el-button>
      <el-button size="small" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doAdd } from '@/api/material';
import { baseURL } from '@/config';
import store from '@/store';
export default {
  data() {
    return {
      form: {
        name: '',
        icon: '',
        remark: '',
      },
      action: baseURL + '/file/uploadImg',
      headers: { token: store.getters['user/token'] },
      filePath: '',
      clearable: true,
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    materialEdit(row) {
      if (!row) {
        this.title = '添加图片';
      } else {
        this.title = '编辑图片';
        this.roleId = row.id;
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 0) {
        this.form.icon = res.data;
        this.filePath = baseURL + '/static/img/' + res.data;
      } else {
        this.$message.error(res.msg);
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    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) {
          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>
