<template>
  <ai-detail>
    <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 label="标题" style="width: 100%;" prop="title" :rules="[{required: true, message: '请输入标题', trigger: 'blur'}]">
              <el-input size="small" v-model="form.title" clearable placeholder="请输入..." :maxlength="50" :show-word-limit="true"></el-input>
            </el-form-item>
            <el-form-item prop="areaId" style="width: 100%;" label="发布地区" :rules="[{required: true, message: '请选择地区', trigger: 'change'}]">
              <ai-area-select clearable @fullname="v => form.areaName = v" always-show :instance="instance" v-model="form.areaId" :disabled-level="disabledLevel"></ai-area-select>
            </el-form-item>
            <el-form-item prop="contentType" label="文章类型" :rules="[{required: true, message: '请选择文章类型', trigger: 'change'}]">
              <el-select style="width: 100%;" v-model="form.contentType" size="small" placeholder="请选择文章类型">
                <el-option
                  v-for="item in contentTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="categoryId" label="分类">
              <el-select style="width: 100%;" v-model="form.categoryId" size="small" placeholder="请选择分类">
                <el-option
                  v-for="item in cateList"
                  :key="item.id"
                  :label="item.categoryName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.contentType === '0'" label="正文" prop="content" style="width: 100%;" :rules="[{required: true, message: '请输入内容', trigger: 'change'}]">
              <ai-editor v-model="form.content" :instance="instance"/>
            </el-form-item>
            <el-form-item v-if="form.contentType === '0'" label="图片" prop="files" style="width: 100%;">
              <ai-uploader
                :instance="instance"
                v-model="form.files"
                fileType="img"
                acceptType=".jpg,.png,.jpeg"
                :limit="9">
                <template slot="tips">
                  <p>最多上传9张图片,单个文件最大10MB，支持jpg、jpeg、png格式</p>
                </template>
              </ai-uploader>
            </el-form-item>
            <el-form-item v-if="form.contentType === '1'" label="视频" prop="files" style="width: 100%;" :rules="[{required: true, message: '请上传视频', trigger: 'change'}]">
              <ai-uploader
                :instance="instance"
                v-model="form.files"
                acceptType=".mp4"
                fileType="video"
                :limit="1">
              </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
    },

    data () {
      return {
        info: {},
        form: {
          title: '',
          content: '',
          areaId: '',
          files: [],
          categoryId: '',
          contentType: '0',
          areaName: '',
          thumbUrl: []
        },
        cropOps: {
          width: "336px",
          height: "210px"
        },
        id: '',
        contentTypeList: [
          {
            name: '文章',
            value: '0'
          },
          {
            name: '视频',
            value: '1'
          }
        ],
        cateList: []
      }
    },

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

    created () {
      this.getCateList()
      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/appcontentinfo/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      },

      getCateList () {
        this.instance.post(`/app/appcontentmodulecategory/list`, null, {
          params: {
            ...this.cateSearch,
            moduleId: this.$route.query.moduleId,
            size: 100
          }
        }).then(res => {
          if (res.code == 0) {
            this.cateList = res.data.records
          }
        })
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appcontentinfo/addOrUpdate`, {
              ...this.form,
              moduleId: this.$route.query.moduleId,
              createUserName: this.user.info.name,
              createUserId: this.user.info.id,
              categoryName: this.cateList.length ? this.cateList.filter(v => v.id === this.form.categoryId)[0].categoryName : ''
            }).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>
