<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="modalTitle"
    :modal-append-to-body="false"
    style="text-align:left;"
    @close="closeDialog"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'150px'">
        <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="insuranceDescription">
          <el-input type="textarea" :rows="10" v-model="formModel.insuranceDescription" placeholder="请输入投保声明" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="雇主责任险条款" prop="clause">
          <el-input type="textarea" :rows="10" v-model="formModel.clause" 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 insuranceDefinitionApi from "@/api/base/insuranceDefinition";

export default {
  props: ["businessKey", "modalTitle"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        name: [{ required: true, message: "险种不能为空", trigger: "blur" }],
        insuranceDescription: [{ required: true, message: "投保声明不能为空", trigger: "blur" }],
        clause: [{ required: true, message: "雇主责任险条款不能为空", trigger: "blur" }]
      },
      showDialog: true,
      loading: false,
      submitting: false,
      queryResult:[]
    };
  },
  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 insuranceDefinitionApi.add(self.formModel);
            } else {
              return insuranceDefinitionApi.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;
    self.loading = true;
    (function() {
      if (self.businessKey.length == 0) {
        return insuranceDefinitionApi.create();
      } else {
        return insuranceDefinitionApi.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>