
<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="companyId">
          <el-input v-model="formModel.companyId" placeholder="请输入单位编号" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="位置1名称" prop="position1Name">
          <el-input v-model="formModel.position1Name" placeholder="请输入位置1名称" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="位置2名称" prop="position2Name">
          <el-input v-model="formModel.position2Name" placeholder="请输入位置2名称" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="位置3名称" prop="position3Name">
          <el-input v-model="formModel.position3Name" placeholder="请输入位置3名称" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="位置4名称" prop="position4Name">
          <el-input v-model="formModel.position4Name" placeholder="请输入位置4名称" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="位置5名称" prop="position5Name">
          <el-input v-model="formModel.position5Name" placeholder="请输入位置5名称" 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>
    </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 companyPositionApi from "@/api/base/companyPosition";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      ruleValidate: {
        id: [{ required: true, message: "不能为空", trigger: "blur" }],
        companyId: [
          { required: true, message: "单位编号不能为空", trigger: "blur" }
        ],
        position1Name: [
          { required: true, message: "位置1名称不能为空", trigger: "blur" }
        ],
        position2Name: [
          { required: true, message: "位置2名称不能为空", trigger: "blur" }
        ],
        position3Name: [
          { required: true, message: "位置3名称不能为空", trigger: "blur" }
        ],
        position4Name: [
          { required: true, message: "位置4名称不能为空", trigger: "blur" }
        ],
        position5Name: [
          { required: true, message: "位置5名称不能为空", 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" }
        ]
      },
      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 companyPositionApi.add(self.formModel);
            } else {
              return companyPositionApi.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 companyPositionApi.create();
      } else {
        return companyPositionApi.edit(self.menuId);
      }
    })()
      .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>