<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>名称：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入名称"
                v-model="postData.LocationName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>标题：<em>*</em></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 style="width:50%">
              <el-input
                placeholder="请输入资讯分类"
                v-model="postData.Consult"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>图片：<em>*</em></span>
          <div class="flex-item">
            <el-upload
              action='string'
              :http-request="toolClass.upload"
              :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, mapActions } from 'vuex'
export default {
  name: '',
  data () {
    return {
      postData: {
        LocationName: '', // 名称
        Title: '', // 标题
        Consult: '', // 资讯分类
        number: '', // 序号
        OrgId: '',
        CaseId: '',
        TypeImg: ''
      },
      dialogVisible: false,
      imgs: '',
      imgsArr: [],
      limit: 1
    }
  },
  components: {},
  created () {
    this.updateSystemInfo().then(() => {
      this.postData.CaseId = this.defaultCaseId
    })
    this.getDetail()
  },
  computed: {
    ...mapState({
      OrgId: x => x.app.user.OrgId,
      defaultCaseId: x => x.app.cases.default
    })
  },
  methods: {
    ...mapActions(['updateSystemInfo']),
    submit () {
      if (this.postData.LocationName === '') {
        this.$message({
          type: 'error',
          message: '名称不能为空'
        })
        return false
      }
      if (this.postData.Title === '') {
        this.$message({
          type: 'error',
          message: '标题不能为空'
        })
        return false
      }
      if (this.postData.Consult === '') {
        this.$message({
          type: 'error',
          message: '资讯分类不能为空'
        })
        return false
      }
      if (!this.imgsArr.length) {
        this.$message({
          type: 'error',
          message: '图片不能为空'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      this.postData.TypeImg = ''
      for (let i = 0; i < this.imgsArr.length; i++) {
        this.postData.TypeImg += this.imgsArr[i].response.result.url + ','
      }
      if (this.postData.TypeImg) {
        this.postData.TypeImg = this.postData.TypeImg.substr(0, this.postData.TypeImg.length - 1)
      }
      this.$ajax(this.$api.cms.editLocation.url, {
        method: this.$api.cms.editLocation.method,
        urlData: {
          id: this.$route.query.id
        },
        data: this.postData
      }).then(res => {
        this.$message({
          type: 'success',
          message: '编辑成功'
        })
        this.$router.push({ name: 'fiveA' })
      }).catch(msg => {

      })
    },
    cancel () {
      this.$router.go(-1)
    },
    getDetail () {
      this.$ajax(this.$api.cms.locationDetail.url, {
        method: this.$api.cms.locationDetail.method,
        urlData: {
          id: this.$route.query.id
        }
      }).then(res => {
        let imgsArr = []
        let item = {
          url: res.TypeImg,
          response: {
            result: {
              url: res.TypeImg
            }
          }
        }
        imgsArr.push(item)
        this.imgsArr = imgsArr
        this.postData = res
      }).catch(msg => {

      })
    },
    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
      })
    }
  }
}
</script>

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