<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'" :visible.sync="dialog" width="450px" :close-on-click-modal="false"
               top="50px">
      <el-form ref="form" :model="form" label-width="100px" class="bind-phone">
        <el-form-item label="项目名称：">
          <el-input v-model="form.project_name" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="LOGO：">
          <upload class="marginT-20" type="qrcode" v-model="form.project_logo"></upload>
        </el-form-item>
        <el-form-item label="项目描述：">
          <el-input v-model="form.project_desc" placeholder="请输入项目描述"></el-input>
        </el-form-item>
        <el-form-item label="项目域名：">
          <el-input v-model="form.project_host" placeholder="请输入项目域名"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="danger" @click="handleSubmit" :disabled="booLock">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import upload from '@/components/upload'

export default {
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  components: {
    upload,
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        project_name: '',
        project_logo: '',
        project_desc: '',
        project_host: '',
      },
      booLock: false
    }
  },
  methods: {
    async handleSubmit () {
      let url = this.exData.id ? '/project/edit' : '/project/add'
      this.booLock = true
      const data = await this.$fetch('/api/auth' + url, { ...this.form })
      this.booLock = false
      if (data.code === 200) {
        this.$message.success('提交成功')
        this.$emit('success')
        this.$store.dispatch('common/setProjectArr')
        this.dialog = false
      }
    }
  },
  mounted () {
    if (this.exData.id) {
      this.form.id = this.exData.id
      for (const key in this.form) {
        this.form[key] = this.exData[key]
      }
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
