
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    @close="closeDialog"
    :close-on-click-modal="false"
    width="900px"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'100px'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="companyName">
              <el-input
                v-model="formModel.companyName"
                placeholder="请输入企业名称"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社会信用代码" prop="creditCode">
              <el-input
                v-model="formModel.creditCode"
                placeholder="请输入社会信用代码"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所在地区" prop="region">
              <el-select
                v-model="formModel.region"
                filterable
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="region in regionResult"
                  :key="region.value"
                  :label="region.name"
                  :value="region.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="企业类型" prop="type">
              <el-select
                v-model="formModel.type"
                filterable
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="type in typeResult"
                  :key="type.value"
                  :label="type.name"
                  :value="type.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属行业" prop="industry">
              <el-select
                v-model="formModel.industry"
                filterable
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="industry in industryResult"
                  :key="industry.value"
                  :label="industry.name"
                  :value="industry.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业规模" prop="scale">
              <el-select
                v-model="formModel.scale"
                filterable
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="scale in scaleResult"
                  :key="scale.value"
                  :label="scale.name"
                  :value="scale.value"
                ></el-option> </el-select></el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="businessLicenseUrl">
              <el-upload
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :show-file-list="false"
                :headers="headers"
                :data="uploadData"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                accept="image/png,image/jpeg"
              >
                <img v-if="fileUrl" :src="fileUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业地址" prop="address">
              <el-input
                v-model="formModel.address"
                placeholder="请输入企业地址"
                style="width: 80%"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业介绍" prop="companyIntroduction">
              <el-input
                v-model="formModel.companyIntroduction"
                placeholder="请输入企业介绍"
                style="width: 80%"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业家介绍" prop="enterpriserIntroduction">
              <el-input
                v-model="formModel.enterpriserIntroduction"
                placeholder="请输入企业家介绍"
                style="width: 80%"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import companyInfoApi from "@/api/base/companyInfo";
import dataDictionaryApi from "@/api/sys/dataDictionary";

import { getToken } from "@/utils/auth"; // get token from cookie

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        companyName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        type: [
          {
            required: true,
            message: "企业类型不能为空",
            trigger: "blur",
          },
        ],
        scale: [
          {
            required: true,
            message: "企业规模不能为空",
            trigger: "blur",
          },
        ],
        industry: [
          {
            required: true,
            message: "所属行业不能为空",
            trigger: "blur",
          },
        ],
        region: [
          {
            required: true,
            message: "所在地区不能为空",
            trigger: "blur",
          },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      industryResult: [],
      regionResult: [],
      scaleResult: [],
      typeResult: [],
      //上传图片start
      uploadUrl: Constant.serverUrl + "/uploadPicture",
      uploadData: {
        subFolder: "companyInfo",
      },
      fileUrl: "",
      headers: {
        Authorization: getToken(),
      },
      //上传图片end
    };
  },
  created() {
    var self = this;

    dataDictionaryApi
      .findByCatalogName({
        catalogName: "企业类型",
      })
      .then((response) => {
        var jsonData = response.data;
        this.typeResult = jsonData.data;
      });

    dataDictionaryApi
      .findByCatalogName({
        catalogName: "企业规模",
      })
      .then((response) => {
        var jsonData = response.data;
        this.scaleResult = jsonData.data;
      });

    dataDictionaryApi
      .findByCatalogName({
        catalogName: "所属行业",
      })
      .then((response) => {
        var jsonData = response.data;
        this.industryResult = jsonData.data;
      });

    dataDictionaryApi
      .findByCatalogName({
        catalogName: "区域",
      })
      .then((response) => {
        var jsonData = response.data;
        this.regionResult = jsonData.data;
      });
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return companyInfoApi.add(self.formModel);
            } else {
              return companyInfoApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.loading = false;
      var self = this;
      self.formModel.businessLicenseUrl = res.data;
      self.fileUrl =
        res.data + "?x-oss-process=image/resize,m_lfit,w_200";
    },
    beforeAvatarUpload(file) {
      this.loading = true;
      // const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isJPG) {
      //   this.$message.error("上传图片只能是 JPG 格式!");
      // }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
  },
  mounted: function () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return companyInfoApi.create();
      } else {
        return companyInfoApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;

          let businessLicenseUrl = self.formModel.businessLicenseUrl;
          if (businessLicenseUrl != null) {
            self.fileUrl =
              businessLicenseUrl +
              "?x-oss-process=image/resize,m_lfit,h_200";
          }
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>