<template>
  <ai-detail>
    <template slot="title">
      <ai-title :title="pageTitle" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form :model="form" label-width="120px" ref="form">
            <el-form-item label="标题" prop="title" :rules="[{required: true, message: '请输入标题', trigger: 'blur'}]">
              <el-input type="input" v-model="form.title" clearable placeholder="请输入..." maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item prop="areaId" label="发布地区" :rules="[{required: true, message: '请选择地区', trigger: 'change'}]">
              <ai-area-select clearable always-show :instance="instance" v-model="form.areaId" :disabled-level="disabledLevel"></ai-area-select>
            </el-form-item>
            <el-form-item label="正文" prop="content" :rules="[{required: true, message: '请输入内容', trigger: 'change'}]">
              <ai-editor v-model="form.content" :instance="instance"/>
            </el-form-item>
            <el-form-item label="缩略图" prop="thumbUrl">
              <ai-uploader
                :instance="instance"
                isShowTip
                v-model="form.thumbUrl"
                :limit="1"
                :cropOps="cropOps"
                is-crop>
                <template slot="tips">
                  <p>最多上传1张图片,单个文件最大10MB，支持jpg、jpeg、png格式</p>
                  <p>图片比例：1.6：1</p>
                </template>
              </ai-uploader>
            </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,
      menuName: String
    },

    data () {
      return {
        info: {},
        form: {
          title: '',
          content: '',
          areaId: '',
          createUnitName: '',
          createUserName: '',
          status: '',
          thumbUrl: []
        },
        cropOps: {
          width: "336px",
          height: "210px"
        },
        id: ''
      }
    },

    computed: {
      ...mapState(['user']),
      pageTitle: v => `${!!v.params.id ? '编辑' : '添加'}${v.menuName}`
    },

    created () {
      this.form.areaId = this.user.info.areaId
      this.disabledLevel = this.user.info.areaList.length
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

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

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appcountrysidetourism/addOrUpdate`, {
              ...this.form,
              type: 5,
              createUserName: this.user.info.name,
              thumbUrl: this.form.thumbUrl.length ? JSON.stringify([{
                url: this.form.thumbUrl[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>
