
<template>
    <el-dialog
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="true"
      style="text-align:left;"
      :close-on-click-modal="true"
      @close="closeDialog"
      :append-to-body="true"
      width="600px"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'130px'"
          :inline="true"
        >
          <el-form-item label="选择园区" prop="companyId">
            <el-select v-model="formModel.companyId" placeholder="请选择" style="width: 300px">
                <el-option
                v-for="item in companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.isBindingUser">
                </el-option>
            </el-select>
          </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 userCompanyApi from "@/api/sys/userCompany"; 
  import dataDictionaryApi from "@/api/sys/dataDictionary";
  
  
  export default {
    props: ["userId","title"],
    data() {
      return {
          ruleValidate: {
            parkingId: [
            { required: true, message: "停车场不能为空", trigger: "blur" },
            ],

          },
          roleList: [],
          formModel: {},
          showDialog: true,
          loading: false,
          submitting: false,
          showMapDialog:false,
          typeList:[],
          statusList:[],
          stationSubInfoList: [],
          selectedRow: {},
          companyList:[],
      };
    },
    created() {
        var self = this;

        var formData = new FormData();

        formData.append("userId", self.userId);

        userCompanyApi.findByUserId(formData).then(function (response) {
            var jsonData = response.data;
            self.companyList = jsonData.data;   
        });
    },
    methods: {
      closeDialog() {
        this.$emit("close", false);
      },
      handleSubmit() {
        var self = this;
  
        this.$refs["form"].validate((valid) => {
          if (valid) {
            (function () {
                self.formModel.userId = self.userId;
                return userCompanyApi.add(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);
              }
            });
          }
        });
      }
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey != null && self.businessKey.length > 0) {
          return userCompanyApi.edit(self.businessKey);
        } else {
          return userCompanyApi.create();
        }
      })()
        .then((response) => {
          var jsonData = response.data;
  
          if (jsonData.result) {
              self.formModel = jsonData.data;
  
              let image = self.formModel.image;
              if (image != null) {
                  self.fileUrl = image 
              }
  
              self.showModal = true;
          } else {
            self.$message.error(jsonData.message + "");
          }
  
          self.loading = false;
        })
        .catch((error) => {
          self.$message.error(error + "");
          self.loading = false;
        });
    },
    components: {
    },
  };
  </script>
  <style scoped>
  .user-panel {
    margin: 10px auto;
  }
  .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  </style>