<template>
  <ai-detail class="content-add">
    <template slot="title">
      <ai-title :title="$route.query.id ? '编辑' : '添加'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form class="ai-form" :model="form" label-width="120px" ref="form">
            <el-form-item label="姓名" style="width: 100%;" prop="name" :rules="[{required: true, message: '请输入姓名', trigger: 'blur'}]">
              <el-input size="small" v-model="form.name" placeholder="请输入..." :maxlength="20"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" style="width: 100%;" prop="phone" :rules="[{required: true, message: '请输入联系电话', trigger: 'blur'}]">
              <el-input size="small" v-model="form.phone" placeholder="请输入..." :maxlength="11"></el-input>
            </el-form-item>
            <el-form-item label="网格名称" style="width: 100%;" prop="girdName" :rules="[{required: true, message: '请输入网格名称', trigger: 'blur'}]">
              <el-input size="small" v-model="form.girdName" placeholder="请输入..." :maxlength="50"></el-input>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" :loading="isLoading" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'ApplyAdd',

    props: {
      instance: Function,
      dict: Object,
    },

    data () {
      return {
        form: {
          name: '',
          phone: '',
          gridName: ''
        },
        isLoading: false,
        id: ''
      }
    },

    created () {
      if ( this.$route.query.id) {
        this.id = this.$route.query.id
        this.getInfo(this.$route.query.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appcontentinfo/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      },

      onChange () {
        this.form.files = []
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.instance.post(`/app/appgirdmemberapply/addOrUpdate`, {
              ...this.form,
              id: this.$route.query.id || ''
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              } else {
                this.isLoading = false
              }
            }).catch(() => {
              this.isLoading = false
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'list',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
