<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 400px;max-width:400px;">
      <q-card-section>
        <div class="text-h6">{{optionTitle}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="row wrap">
          <q-input filled stack-label maxlength="30" :dense="false" v-model="objOption.EnterpriseName" ref="EnterpriseName"
            class="col-12 q-pb-lg" label="企业名称" :rules="[val => !!val || '请填写企业名称']" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveEnterprise" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    SetEnterprise,
    GetEnterprise,
  } from '../../api/users/user'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        saveLoading: false,
        objOption: {
          Id:0,
          EnterpriseName:""
        },
        optionTitle: "",
      }
    },
    mounted() {
      this.initObj();
    },
    methods: {
      //初始化表单
      initObj() {
        // if (this.saveObj && this.saveObj.Id > 0) {
        //   this.objOption.Id = this.saveObj.Id;
        //   this.objOption.EnterpriseName = this.saveObj.EnterpriseName;
        //   this.objOption.No = this.saveObj.No;
        //   this.optionTitle = "修改企业信息"
        // } else {
        //   this.optionTitle = "新增企业信息"
        //   this.objOption.Id = 0;
        //   this.objOption.StageName = '';
        //   this.objOption.No = '';
        // }
        if (this.saveObj && this.saveObj.Id > 0) {
          GetEnterprise({
            Id: this.saveObj.Id
          }).then(res => {
            this.objOption.Id = res.Data.Id;
            this.objOption.EnterpriseName = res.Data.EnterpriseName;
          })
          this.optionTitle = "修改企业信息"
        } else {
          this.optionTitle = "新增企业信息"
          this.objOption.Id = 0;
          this.objOption.EnterpriseName = '';
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
      },
      //保存菜单
      saveEnterprise() {
        this.$refs.EnterpriseName.validate();
        if (!this.$refs.EnterpriseName.hasError) {
          this.saveLoading = true
          SetEnterprise(this.objOption).then(res => {
            this.saveLoading = false;
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '数据保存成功！',
                position: 'top'
              })
              this.$emit("success")
              this.closeSaveForm()
            }
          })
        }
      }
    }
  }

</script>
