
<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-form-item label="" prop="id">
          <el-input
            v-model="formModel.id"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input
            v-model="formModel.name"
            placeholder="请输入企业名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业类型" prop="type">
          <el-input
            v-model="formModel.type"
            placeholder="请输入企业类型"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业规模" prop="scale">
          <el-input
            v-model="formModel.scale"
            placeholder="请输入企业规模"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-input
            v-model="formModel.industry"
            placeholder="请输入所属行业"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业地址" prop="address">
          <el-input
            v-model="formModel.address"
            placeholder="请输入企业地址"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业介绍" prop="introduction">
          <el-input
            v-model="formModel.introduction"
            placeholder="请输入企业介绍"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input
            v-model="formModel.contactPerson"
            placeholder="请输入联系人"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input
            v-model="formModel.tel"
            placeholder="请输入联系电话"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="longtitude">
          <el-input
            v-model="formModel.longtitude"
            placeholder="请输入经度"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input
            v-model="formModel.latitude"
            placeholder="请输入纬度"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="企业邮箱" prop="email">
          <el-input
            v-model="formModel.email"
            placeholder="请输入企业邮箱"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sortNo">
          <el-input
            v-model="formModel.sortNo"
            placeholder="请输入排序"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="createBy">
          <el-input
            v-model="formModel.createBy"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="createTime">
          <el-input
            v-model="formModel.createTime"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="updateBy">
          <el-input
            v-model="formModel.updateBy"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="updateTime">
          <el-input
            v-model="formModel.updateTime"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="删除标示" prop="delFlag">
          <el-input
            v-model="formModel.delFlag"
            placeholder="请输入删除标示"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否企业认证" prop="isCertification">
          <el-input
            v-model="formModel.isCertification"
            placeholder="请输入是否企业认证"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="信用代码" prop="creditCode">
          <el-input
            v-model="formModel.creditCode"
            placeholder="请输入信用代码"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="法人代表" prop="legalPerson">
          <el-input
            v-model="formModel.legalPerson"
            placeholder="请输入法人代表"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="法人联系方式" prop="legalTel">
          <el-input
            v-model="formModel.legalTel"
            placeholder="请输入法人联系方式"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicenseUrl">
          <el-input
            v-model="formModel.businessLicenseUrl"
            placeholder="请输入营业执照"
            style="width: 300px"
          ></el-input>
        </el-form-item>
      </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";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        id: [{ required: true, message: "不能为空", trigger: "blur" }],
        name: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "企业类型不能为空", trigger: "blur" },
        ],
        scale: [
          { required: true, message: "企业规模不能为空", trigger: "blur" },
        ],
        industry: [
          { required: true, message: "所属行业不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "企业地址不能为空", trigger: "blur" },
        ],
        introduction: [
          { required: true, message: "企业介绍不能为空", trigger: "blur" },
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        tel: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
        longtitude: [
          { required: true, message: "经度不能为空", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" },
        ],
        email: [
          { required: true, message: "企业邮箱不能为空", trigger: "blur" },
        ],
        sortNo: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        createBy: [{ required: true, message: "不能为空", trigger: "blur" }],
        createTime: [{ required: true, message: "不能为空", trigger: "blur" }],
        updateBy: [{ required: true, message: "不能为空", trigger: "blur" }],
        updateTime: [{ required: true, message: "不能为空", trigger: "blur" }],
        delFlag: [
          { required: true, message: "删除标示不能为空", trigger: "blur" },
        ],
        isCertification: [
          { required: true, message: "是否企业认证不能为空", trigger: "blur" },
        ],
        creditCode: [
          { required: true, message: "信用代码不能为空", trigger: "blur" },
        ],
        legalPerson: [
          { required: true, message: "法人代表不能为空", trigger: "blur" },
        ],
        legalTel: [
          { required: true, message: "法人联系方式不能为空", trigger: "blur" },
        ],
        businessLicenseUrl: [
          { required: true, message: "营业执照不能为空", trigger: "blur" },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
    };
  },
  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 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;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>