<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      label-position="left"
    >
      <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="物料编码" prop="materialCode">
        <el-input
          v-model.trim="form.materialCode"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="铭片模板" prop="nameplate">
        <el-upload
          v-if="dialogFormVisible"
          :action="updateAction"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="fileSuccess"
          :headers="headerData"
          name="files"
          :file-list="fileList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog title="大图" v-model="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model.trim="form.remark"
          size="small"
          autocomplete="off"
        ></el-input>
      </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 { addData, updateData } from '@/api/product';
import { baseURL } from '@/config';
import store from '@/store';
export default {
  name: 'GroupEdit',
  data() {
    return {
      updateAction: '',
      fileList: [],
      form: {
        id: '',
        name: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入产品名称' }],
        materialCode: [
          { required: true, trigger: 'blur', message: '请输入物料编码' },
        ],
        nameplate: [
          { required: true, trigger: 'blur', message: '请上传铭片模板' },
        ],
      },
      roleData: [],
      clearable: true,
      roleId: '',
      parentData: [],
      title: '',
      dialogVisible: false,
      dialogFormVisible: false,
    };
  },
  created() {
    this.setUpdateData();
  },
  methods: {
    setUpdateData() {
      this.updateAction = baseURL + '/file/uploadImg';
      this.headerData = { token: store.getters['user/token'] };
    },
    fileSuccess(response, file, fileList) {
      var img = this.form.nameplate;

      this.form.nameplate = response.data;

      console.log(this.form);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.form.nameplate = '';
    },
    handlePictureCardPreview(file) {
      console.log(file);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    showEdit(row) {
      if (!row) {
        this.title = '添加产品';
      } else {
        this.title = '编辑产品';
        row.nameplate &&
          this.fileList.push({
            url: row.nameplate,
          });
        this.form = Object.assign({}, row);
      }

      this.dialogFormVisible = true;
    },
    close() {
      this.fileList = [];
      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 } = this.form.id
            ? await updateData(this.form)
            : await addData(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">
.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>
