<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" label-width="100px" label-position="left">
      <el-form-item label="站点">
        <el-select v-model="form.deviceCode" filterable placeholder="站点">
          <el-option
            v-for="item in devicesData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('maintenance.bxsbsj')">
        <el-select
          v-model="form.equipmentName"
          filterable
          placeholder="报修设备名称"
        >
          <el-option
            v-for="item in equipmentNameData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('maintenance.bxlx')">
        <el-select v-model="form.repairType" filterable placeholder="报修类型">
          <el-option
            v-for="item in repairTypeData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报修图片">
        <el-upload
          class="avatar-uploader"
          :action="fileUpdateUrl"
          :headers="{ token: token }"
          :multiple="true"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <div v-for="(item, index) in imageUrl" :key="index">
            <img v-if="item.filePath" :src="item.filePath" class="avatar" />
          </div>
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doAdd, doEdit, getDictData } from '@/api/repair';
import { getDevices } from '@/api/task';
import { baseURL } from '@/config';
import store from '@/store';
export default {
  data() {
    return {
      form: {
        id: '',
        deviceCode: '',
        equipmentName: '',
        repairType: '',
        repairImg: '',
      },
      projectId: '',
      fileUpdateUrl: '',
      clearable: true,
      title: '',
      token: '',
      imageUrl: [],
      dialogFormVisible: false,
      devicesData: [],
      equipmentNameData: [],
      repairTypeData: [],
    };
  },
  created() {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加在线保修';
      } else {
        this.title = '编辑在线保修';
        const form = Object.assign({}, row);
        const imageUrl = form.repairImg.split(',');
        for (let i = 0; i < imageUrl.length; i++) {
          var filePath = '/static/img/' + imageUrl[i];
          this.imageUrl.push({ filePath });
        }
        this.form.id = form.id;
        this.form.deviceCode = form.device.code;
        this.form.equipmentName = form.equipmentName.dataCode;
        this.form.repairType = form.repairType.dataCode;
        this.form.repairImg = form.repairImg;
      }
      this.dialogFormVisible = true;
      this.projectId = sessionStorage.getItem('projectId');
      this.setUploadUrl();
      this.getDevicesData();
      this.getDictData();
    },
    async getDictData() {
      const { data } = await getDictData();
      this.equipmentNameData = data.equipmentName;
      this.repairTypeData = data.repairType;
    },
    async getDevicesData() {
      const { data } = await getDevices({ projectId: this.projectId });
      this.devicesData = data;
    },
    setUploadUrl() {
      this.token = store.getters['user/token'];
      this.fileUpdateUrl = baseURL + '/file/uploadImg';
    },
    handleAvatarSuccess(res, file) {
      const imageUrl = this.imageUrl;
      const repairImg = this.form.repairImg;
      if (repairImg != '') {
        this.form.repairImg = repairImg + ',' + file.name;
      } else {
        this.form.repairImg = file.name;
      }
      const filePath = '/static/img/' + file.name;
      imageUrl.push({ filePath });
      this.imageUrl = imageUrl;
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    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) {
          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.$parent.fetchData();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
.el-dialog {
  .el-dialog__header {
    border-bottom: 1px solid #e3e3e3 !important;
    padding: 15px 20px;
  }
  .dialog-footer {
    text-align: right;
    .el button {
      height: 36px !important;
      min-height: 36px !important;
      line-height: 366px !important;
    }
  }
}
</style>
