<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>跳转类型：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%" class="radio">
                <el-radio v-model="postData.ForwardType" label='url' >链接</el-radio>
                <el-radio v-model="postData.ForwardType" label='course' >课程</el-radio>
            </div>
          </div>
        </li>
        <li class="flex-h" v-if="postData.ForwardType==='url'">
          <span>链接：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入链接"
                v-model="postData.ForwardUrl"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h" v-else>
          <span>课程：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="postData.ForwardResourceId" placeholder="请选择课程">
                <el-option
                  v-for="item in courses.list"
                  :key="item.CourseId"
                  :label="item.CourseName"
                  :value="item.CourseId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>是否发布：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%" class="radio">
              <el-radio v-model="postData.Status" label='1' >是</el-radio>
              <el-radio v-model="postData.Status" label='0' >否</el-radio>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>图片位置：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="postData.LocationId" placeholder="请选择">
                <el-option
                  v-for="item in positionList"
                  :key="item.LocationId"
                  :label="item.LocationName"
                  :value="item.LocationId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>标题：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入标题"
                v-model="postData.Title"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>图片：<em>*</em></span>
          <div class="flex-item">
            <div class="red-hint">建议上传650*300尺寸的图片</div>
            <el-upload
              class="avatar-uploader"
              action='string'
              :before-upload="toolClass.beforeUpload"
              :http-request="toolClass.upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img v-if="postData.ImageUrl" :src="postData.ImageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers, mapActions } from 'vuex'
const { mapActions: mapCmsActions } = createNamespacedHelpers('cms')
const { mapState: mapCourseState, mapActions: mapCourseActions } = createNamespacedHelpers('course')

export default {
  name: '',
  data () {
    return {
      postData: {
        ForwardType: '1',
        ForwardUrl: '',
        ForwardResourceId: '',
        Status: '1',
        LocationId: '',
        ImageUrl: '',
        Title: '',
        OrgId: '',
        CaseId: '',
      },
      dialogVisible: false,
      imgs: '',
      imgsArr: [],
      limit: 1
    }
  },
  components: {},
  created () {
    this.picSize({ w: 650, h: 300 })
    this.GetCourseList({page: 1, pagesize: 100, callback: this.getDetail})
    this.updateLocationAllInfo()
  },
  computed: {
    ...mapState({
      positionList: x => x.cms.locationAll,
      OrgId: x => x.app.user.OrgId,
      caseList: x => x.app.cases.list
    }),
    ...mapCourseState({
      courses: x => x.courseList,
    })
  },
  methods: {
    ...mapCourseActions([
      'GetCourseList',
    ]),
    ...mapCmsActions(['updateLocationAllInfo']),
    ...mapActions(['picSize']),
    submit () {
      if (this.postData.ForwardType === 'url') {
        this.postData.ForwardResourceId = ''
        if (this.postData.ForwardUrl === '') {
          this.$message({
            message: '链接地址不能为空',
            type: 'error',
          })
          return false
        } else {
          var strRegex = /^(ftp|https?)\:\/\/([\w\_\-]+)\.([\w\-]+[\.]?)*[\w]+\.[a-zA-Z]{2,10}(.*)/  // eslint-disable-line
          if (this.postData.ForwardUrl.match(strRegex) === null) {
            this.$message({
              message: '链接地址格式不正确',
              type: 'error',
            })
            return false
          }
        }
      } else {
        this.postData.ForwardUrl = ''
        if (this.postData.ForwardResourceId === '') {
          this.$message({
            message: '课程不能为空',
            type: 'error',
          })
          return false
        }
      }
      if (this.postData.LocationId === '') {
        this.$message({
          message: '图片位置不能为空',
          type: 'error',
        })
        return false
      }
      if (this.postData.ImageUrl === '') {
        this.$message({
          message: '图片不能为空',
          type: 'error',
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      this.postData.CaseId = this.CaseId
      this.$ajax(this.$api.cms.editImage.url, {
        method: this.$api.cms.editImage.method,
        data: this.postData,
        urlData: {
          id: this.$route.query.id
        }
      }).then(res => {
        this.$message({
          message: '编辑成功',
          type: 'success',
          duration: 1000
        })
        setTimeout(() => {
          this.$router.push({ name: 'bannerManager' })
        }, 1000)
      }).catch(msg => {

      })
    },
    cancel () {
      this.$router.go(-1)
    },
    getDetail () {
      this.$ajax(this.$api.cms.imageDetail.url, {
        method: this.$api.cms.imageDetail.method,
        urlData: {
          id: this.$route.query.id
        }
      }).then(res => {
        res.Status = res.Status + ''
        this.postData = res
      }).catch(msg => {

      })
    },
    handleAvatarSuccess (res, file) {
      this.postData.ImageUrl = res.result.url
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
