<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="500px"
    :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="105px"
            class="supplierAdd"
          >
            <el-form-item label="活动名称" prop="name">
              <el-input
                v-model="ruleForm.name"
                style="width: 100%"
                :disabled="isDetail"
                placeholder="活动名称"
              />
            </el-form-item>
          </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>
</template>
<script>
import asyncRequest from "@/apis/service/operate/shopTrim/activity";
import { formItem, rules } from "./columns";
export default {
  name: "shop",
  props: ["showModel", "id", "isDetail", "sitem", "store_id"],
  data() {
    return {
      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();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          if (this.sitem && this.sitem.id) {
            const { id, name } = this.sitem;
            this.ruleForm = {
              id: id || "",
              name: name || "",
            };
          } 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.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;
        }
      });
    },
  },
};
</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>
