<template>
  <ai-detail class="content-add">
    <template slot="title">
      <ai-title :title="params.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 prop="title" style="width: 100%;" label="话题名称" :rules="[{required: true, message: '请输入话题名称', trigger: 'change'}]">
              <el-input size="small" placeholder="请输入话题名称" v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item prop="title" style="width: 100%;" label="排序" :rules="[{required: true, message: '请输入排序', trigger: 'change'}]">
              <el-input-number v-model="form.showIndex" :min="1" :max="100" size="small"></el-input-number>
            </el-form-item>
            <el-form-item label="话题描述" prop="description" style="width: 100%;" :rules="[{required: true, message: '请输入发布内容', trigger: 'change'}]">
              <el-input type="textarea" placeholder="请输入内容" v-model="form.description" rows="8"></el-input>
            </el-form-item>
            <el-form-item label="话题图标" prop="files" style="width: 100%;" :rules="[{required: true, message: '请上传话题图标', trigger: 'change'}]">
              <ai-uploader
                :instance="instance"
                v-model="form.files"
                isShowTip
                :limit="1">
              </ai-uploader>
            </el-form-item>
            <el-form-item prop="title" style="width: 100%;" label="是否启用" :rules="[{required: true}]">
              <el-radio-group v-model="form.status">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },
    data () {
      return {
        form: {
          title: '',
          showIndex: '',
          description: '',
          files: [],
          picUrl: '',
          status: '0',
        },
      }
    },

    computed: {
      ...mapState(['user'])
    },

    created () {
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

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

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appneighborhoodassistancetheme/addOrUpdate`, {
              ...this.form,
              picUrl: this.form.files[0].url
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              }
            })
          }
        })
      },

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

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