
<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"
  >
    <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="name">
            <el-input
              v-model="formModel.name"
              placeholder="请输入企业名称"
              style="width: 300px"
            ></el-input>
          </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: 300px"
            >
              <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="tel">
            <el-input
              v-model="formModel.tel"
              placeholder="请输入联系电话"
              style="width: 300px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属行业" prop="industry">
            <el-select
              v-model="formModel.industry"
              filterable
              placeholder="请选择所属行业"
              style="width: 300px"
            >
              <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-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="企业地址" prop="address">
            <el-input
              type="textarea"
              :rows="4"
              v-model="formModel.address"
              placeholder="请输入企业地址"
              style="width: 300px"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="LOGO" prop="logo">
              <el-upload
                class="avatar-uploader"
                name="photoFile"
                :action="uploadUrl"
                :show-file-list="false"
                :headers="headers"
                :data="uploadData"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="formModel.logo" :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="12">
          <el-form-item label="排序号" prop="sortNo">
            <el-input-number
              v-model="formModel.sortNo"
              placeholder="请输入排序"
              style="width: 300px"
              :min="1"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否认证" prop="isCertification">
            <el-radio-group v-model="formModel.isCertification">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </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 companyApi from "@/api/base/company";
import dataDictionaryApi from "@/api/sys/dataDictionary";
import { getToken } from "@/utils/auth";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [{ required: true, message: "企业名称不能为空", trigger: "blur" },],
        scale: [{ required: true, message: "企业规模不能为空", trigger: "blur" },],
        industry: [{ required: true, message: "所属行业不能为空", trigger: "blur" },],
        address: [{ required: true, message: "企业地址不能为空", trigger: "blur" },],
        tel: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
        sortNo: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        isCertification: [{ required: true, message: "是否企业认证不能为空", trigger: "blur" },],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      uploadUrl: Constant.serverUrl + "/uploadPicture",
      uploadData: {
        subFolder: "personInfo"
      },
      fileUrl: "",
      headers: {
        Authorization: getToken()
      },
      industryResult: [],
      scaleResult: [],
    };
  },
  created() {
    dataDictionaryApi
    .findByCatalogName({
      catalogName: "所属行业",
    })
    .then((response) => {
      var jsonData = response.data;
      this.industryResult = jsonData.data;
    });

    dataDictionaryApi
    .findByCatalogName({
      catalogName: "企业规模",
    })
    .then((response) => {
      var jsonData = response.data;
      this.scaleResult = jsonData.data;
    });
  },
  methods: {
    handleAvatarSuccess(res, file) {
      var self = this;
      self.formModel.logo = res.data;
      self.fileUrl = res.data + "?x-oss-process=image/resize,m_lfit,h_100,w_100";
    },
    beforeAvatarUpload(file) {
      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 isJPG && isLt2M;
    },
    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 companyApi.add(self.formModel);
            } else {
              return companyApi.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);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

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

        if (jsonData.result) {
          self.formModel = jsonData.data;
          self.fileUrl = self.formModel.logo + "?x-oss-process=image/resize,m_lfit,h_100,w_100"
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>