<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>跳转类型：</span>
          <div class="flex-item">
            <div style="width:50%" class="radio">
                <el-radio v-model="postData.ForwardType" label='1' >链接</el-radio>
                <el-radio v-model="postData.ForwardType" label='0' >课程</el-radio>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>链接：</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">
          <span>是否发布：</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>图片位置：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="postData.LocationIds" multiple 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>图片：</span>
          <div class="flex-item">
            <el-upload
              :action="$api.file.image.url"
              :limit='limit'
              list-type="picture-card"
              :file-list='imgsArr'
              :on-success="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-exceed="exceed">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="imgs" alt="">
            </el-dialog>
          </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 } from 'vuex'
const { mapActions: mapCmsActions } = createNamespacedHelpers('cms')
export default {
  name: '',
  data () {
    return {
      postData: {
        ForwardType: '1',
        ForwardUrl: '',
        ForwardResourceId: '',
        Status: '1',
        LocationIds: [],
        ImageUrl: '',
        Title: '',
        OrgId: '',
        CaseId: ''
      },
      dialogVisible: false,
      imgs: '',
      imgsArr: [],
      limit: 1
    }
  },
  components: {},
  created () {
    this.updateLocationInfo().then(() => {
      this.getDetail()
    })
  },
  computed: {
    ...mapState({
      positionList: x => x.cms.location
    })
  },
  methods: {
    ...mapCmsActions(['updateLocationInfo']),
    submit () {
      if (this.postData.ForwardType === '1') {
        if (this.postData.ForwardUrl === '') {
          this.$message({
            message: '链接地址不能为空',
            type: 'error',
          })
          return false
        } else {
          var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/  // eslint-disable-line
          if (!reg.test(this.postData.ForwardUrl)) {
            this.$message({
              message: '链接地址格式不正确',
              type: 'error',
            })
            return false
          }
        }
      }
      this.postData.ForwardType === '1' ? this.postData.ForwardType = 'url' : this.postData.ForwardType = 'course'
      this.postData.locationids = this.postData.LocationIds.join(',')
      this.postData.ImageUrl = ''
      for (let i = 0; i < this.imgsArr.length; i++) {
        this.postData.ImageUrl += this.imgsArr[i].response.result.url + ','
      }
      if (this.postData.ImageUrl) {
        this.postData.ImageUrl = this.postData.ImageUrl.substr(0, this.postData.ImageUrl.length - 1)
      }
      this.$ajax(this.$api.cms.editNews.url, {
        method: this.$api.cms.editNews.method,
        urlData: {
          id: this.$route.query.id
        },
        data: this.postData
      }).then(res => {
        this.$message({
          message: '编辑成功',
          type: 'success',
          duration: 1000
        })
        setTimeout(() => {
          this.$router.push({ name: 'newsManager' })
        }, 1000)
      }).catch(msg => {

      })
    },
    cancel () {
      this.$router.go(-1)
    },
    handlePictureCardPreview (res, file, fileList) {
      this.imgs = res.result.url
      this.dialogVisible = false
      this.imgsArr = fileList
    },
    handleRemove (file, fileList) {
      this.imgsArr = fileList
    },
    exceed () {
      this.$message({
        message: '超过可传的图片上限',
        type: 'info',
        duration: 1000
      })
    },
    getDetail () {
      this.$ajax(this.$api.cms.newsDetail.url, {
        method: this.$api.cms.newsDetail.method,
        urlData: {
          id: this.$route.query.id
        }
      }).then(res => {
        res.ForwardType === 'url' ? res.ForwardType = '1' : res.ForwardType = '0'
        res.Status += ''
        res.LocationIds = res.NewsLocations.reduce((previousValue, currentValue, index, array) => {
          console.log(previousValue)
          previousValue.push(currentValue.LocationId)
          return previousValue
        }, [])
        console.log(res.LocationIds)
        let imgsArr = []
        let item = {
          url: res.ImageUrl,
          response: {
            result: {
              url: res.ImageUrl
            }
          }
        }
        imgsArr.push(item)
        this.imgsArr = imgsArr
        this.postData = res
      }).catch(msg => {

      })
    }
  }
}
</script>

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