<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            v-loading="loading"
            :model="ruleForm"
            status-icon
            size="small"
            :rules="rulesThis"
            label-width="90px"
            class="supplierAdd"
          >
            <el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="设施名称" prop="name">
                    <el-input
                      v-model="ruleForm.name"
                      style="width: 100%"
                      :disabled="isDetail"
                      placeholder="设施名称"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-col :span="8">
                <el-form-item
                  label="设施主图"
                  prop="image"
                  class="activity-upload"
                  style="margin: 0 0 22px 0"
                >
                  <div v-if="ruleForm.image" class="img-find">
                    <img
                      v-viewer
                      :src="ruleForm.image"
                      class="avatar hover fl"
                      style="width: 86px; height: 86px"
                    />
                    <i
                      class="el-icon-close img-close"
                      v-if="!isDetail"
                      @click="deleteimg()"
                    />
                  </div>
                  <div v-else class="btnupload" style="remark: relative">
                    <i class="el-icon-plus avatar-uploader-icon" />
                    <file-upload
                      v-if="type !== 'view'"
                      class="Upload"
                      :disabled="isDetail"
                      :accept="'.jpg,.png,.jpeg'"
                      :multiple="true"
                      :uploadcondition="beforeAvatarUpload"
                      @UploadErrorEvent="imgUploadError"
                      @UploadSuccessEvent="UploadSuccessEvent"
                    />
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="单位" prop="unit">
                  <el-input
                    v-model="ruleForm.unit"
                    :disabled="isDetail"
                    placeholder="单位"
                    maxlength="20"
                  />
                </el-form-item>
                <el-form-item label="参考价" prop="price">
                  <el-input
                    v-model="ruleForm.price"
                    :disabled="isDetail"
                    placeholder="参考价"
                    maxlength="20"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input
                    v-model="ruleForm.remark"
                    :disabled="isDetail"
                    placeholder="备注"
                    maxlength="20"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" class="bottom-btn tr">
          <el-button v-if="!isDetail" type="primary" @click="submitForm" :size="'mini'"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false" :size="'mini'">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
  <!-- <map-modal :visible.sync="visible1" :toponym="toponym" @save="handleMapSelection" /> -->
</template>
<script>
import asyncRequest from "@/apis/service/operate/shopTrim/conference";
import asyncRequestStoreType from "@/apis/service/serviceParam/storeType";
import asyncRequestSupplier from "@/apis/service/operate/supplier";
import { formItem, rules, parmValue } from "./columns";
// import MapModal from "./mapModal.vue"
export default {
  name: "shop",
  props: ["showModel", "id", "isDetail", "sitem","store_id"],
  // components:{ MapModal },
  data() {
    return {
      parmValue,
      loading: false,
      visible1: false,
      toponym: "",
      storeTypeOptions: [],
      supplierOptions: [],
      title: "添加会议服务设施",
      showModelThis: this.showModel,
      ruleForm: {},
      rules,
      rulesThis: this.rules,
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {
    this.ruleForm = {
      ...formItem,
    };
  },
  methods: {
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.title = "添加会议服务设施";
        this.rulesThis = this.rules;
      } else {
        if (this.isDetail) {
          this.title = "会议服务设施详情";
          this.rulesThis = {};
        } else {
          this.title = "修改会议服务设施";
          this.rulesThis = this.rules;
        }
      }
      await this.resetForm();
      await this.searchTypeList();
      await this.searchSupplierList();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          console.log(this.sitem);
          if (this.sitem && this.sitem.id) {
            const { length, unit, id, name, image, remark, price } = this.sitem;
            this.ruleForm = {
              id: id || "",
              name: name || "",
              image: image || "",
              length: length || "",
              unit: unit || "",
              remark: remark || "",
              price: price || "",
            };
          } else {
            this.ruleForm = {
              ...formItem,
            };
          }
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            let model = JSON.parse(JSON.stringify(this.ruleForm));
            model.type="会议服务设施"
            model.store_id = this.store_id;
            let res = {};
            if (this.id === "add") {
              delete model["id"];
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
            }
            this.loading = false;
            if (res && res.code === 1) {
              const title = this.id === "add" ? "添加成功" : "修改成功";
              this.$notify.success({
                title,
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 刷新表格
    async searchTypeList() {
      const { code, data } = await asyncRequestStoreType.list(this.parmValue);
      this.storeTypeOptions = code === 1 ? data.list : [];
    },
    // 刷新表格
    async searchSupplierList() {
      const { code, data } = await asyncRequestSupplier.list(this.parmValue);
      this.supplierOptions = code === 1 ? data.list : [];
    },
    deleteimg() {
      this.ruleForm.image = "";
      this.$refs.ruleForm.validateField("image");
    },

    // 图片上传成功
    async UploadSuccessEvent(data) {
      const { url } = data;
      if (url === "noToken") {
        await this.logout();
      } else {
        this.ruleForm.image = url;
        this.$refs.ruleForm.validateField("image");
        
      }
    },

    // 图片上传失败
    imgUploadError(res) {
      if (res !== "break") {
        this.$message.error("图片上传失败!");
        this.$refs.ruleForm.validateField("image");
      }
    },
    // 判断图片规格
    beforeAvatarUpload(file) {
      let isJPG = false;
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/jpeg"
      ) {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("图片格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.btnupload {
  width: 86px;
  height: 86px;
  line-height: 86px;
}
.avatar-uploader-icon {
  width: 86px;
  height: 86px;
  line-height: 86px;
}
.img-find {
  position: relative;
  width: 86px;
  height: 86px;
  line-height: 86px;
  &:hover {
    .img-close {
      color: #409eff;
    }
  }
}
.img-close {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 18px;
  font-weight: lighter;
  z-index: 999999;
  &:hover {
    cursor: pointer;
  }
}
</style>
