<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-col :span="24">
                <el-form-item label="行业" prop="industry_id">
                  <el-select
                    v-model="ruleForm.industry_id"
                    style="width: 100%"
                    placeholder="请选择行业"
                    @change="industry_id_change"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                      :disabled="String(item.status) == '0'"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="资质情况" prop="need_permits">
                  <el-select
                    v-model="ruleForm.need_permits"
                    style="width: 100%"
                    placeholder="资质情况"
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in reqOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="资质图片"
                  prop="industry_img"
                  class="activity-upload"
                  style="margin: 0 0 22px 0"
                >
                  <div v-if="ruleForm.industry_img" class="img-find">
                    <img
                      v-viewer
                      :src="ruleForm.industry_img"
                      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="network: 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-row>
          </el-form>
        </el-col>
        <el-col :span="24" class="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/serviceParam/industry";
// import { formItem, rules, parmValue } from "./columns";
// import MapModal from "./mapModal.vue"
const formItem = {
  industry_id: "",
  industry_id_name: "",
  need_permits: "1",
  industry_img: "",
};
export default {
  name: "shop",
  props: ["showModel", "editSitem", "editIndex"],
  data() {
    return {
      parmValue: {
        limit: 15,
        name: "",
        page: 1,
        size: 15,
        status: "",
      },
      loading: false,
      visible1: false,
      toponym: "",
      options: [],
      reqOptions: [
        {
          value: "1",
          label: "需要上传资质",
        },
        {
          value: "0",
          label: "不需要上传资质",
        },
      ],
      title: "添加客房",
      showModelThis: this.showModel,
      ruleForm: {},

      rules: {
        industry_id: [{ required: true, message: "请选择行业", trigger: "change" }],
        need_permits: [{ required: true, message: "请选择资质情况", trigger: "change" }],
        industry_img: [{ required: true, message: "请上传资质图片", trigger: "change" }],
      },
      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.editIndex === -1) {
        this.title = "添加行业";
      } else {
        this.title = "修改行业";
      }
      this.rulesThis = this.rules;
      await this.resetForm();
      await this.searchTypeList();
      this.loading = false;
    },
    industry_id_change() {
      const { industry_id } = this.ruleForm;
      let index = this.options.findIndex((s) => String(s.id) === String(industry_id));
      if (index !== -1) {
        this.ruleForm.need_permits = String(this.options[index].need_permits);
        this.ruleForm.industry_id_name = this.options[index].name;
      } else {
        this.ruleForm.need_permits = "1";
        this.ruleForm.industry_id_name = "";
      }
      this.rulesThis.industry_img[0].required =
        String(this.ruleForm.need_permits) === "1";
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          // console.log(this.editSitem);
          if (this.editSitem && this.editSitem.id) {
            const { industry_id, need_permits, industry_img } = this.editSitem;
            this.ruleForm = {
              industry_id: industry_id || "",
              need_permits: need_permits || "1",
              industry_img: industry_img || "",
            };
          } 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.editIndex = this.editIndex;
            this.loading = false;
            this.$emit("refresh", model);
            this.showModelThis = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 刷新表格
    async searchTypeList() {
      const { code, data } = await asyncRequest.list(this.parmValue);
      this.options = code === 1 ? data.list : [];
    },

    deleteimg() {
      this.ruleForm.industry_img = "";
      this.$refs.ruleForm.validateField("industry_img");
    },

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

    // 图片上传失败
    imgUploadError(res) {
      if (res !== "break") {
        this.$message.error("图片上传失败!");
        this.$refs.ruleForm.validateField("industry_img");
      }
    },
    // 判断图片规格
    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>
