<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>对应案场：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="postData.CaseId" placeholder="请选择">
                <el-option
                  v-for="item in caseList"
                  :key="item.CaseId"
                  :label="item.CaseName"
                  :value="item.CaseId">
                </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.Name"
                clearable>
              </el-input>
            </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"
              list-type="picture-card"
              :limit='limit'
              :on-success="handlePictureCardPreview"
              :file-list='imgsArr'
              :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 class="flex-h">
          <span>详细图片：</span>
          <div class="flex-item">
            <el-upload
              :action='$api.file.image.url'
              list-type="picture-card"
              :limit='limit'
              :file-list='detailimgsArr'
              :on-success="handlePictureCardPreviewDetail"
              :on-remove="handleRemoveDetail"
              :on-exceed="exceed">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisibleDetail">
              <img width="100%" :src="detailimgs" alt="">
            </el-dialog>
          </div>
        </li>
        <li class="flex-h">
          <span>专题简介：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入专题简介"
                v-model="postData.DetailContent"
                type='textarea'
                :autosize="{ minRows: 3, maxRows: 5}"
                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 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: {
        CaseId: '',
        Name: '',
        Title: '',
        DetailContent: '',
        Status: '1',
        imgs: '',
        detailimgs: '',
        courseids: ''
      },
      limit: 1,
      dialogVisible: false,
      dialogVisibleDetail: false,
      imgs: '',
      detailimgs: '',
      imgsArr: [],
      detailimgsArr: []
    }
  },
  components: {},
  computed: {
    ...mapState({
      caseList: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId
    })
  },
  methods: {
    ...mapActions(['updateSystemInfo']),
    submit () {
      this.postData.OrgId = this.OrgId
      this.postData.imgs = ''
      this.postData.detailimgs = ''
      for (let i = 0; i < this.imgsArr.length; i++) {
        this.postData.imgs += this.imgsArr[i].response.result.url + ','
      }
      this.postData.imgs = this.postData.imgs.substr(0, this.postData.imgs.length - 1)
      for (let i = 0; i < this.detailimgsArr.length; i++) {
        this.postData.detailimgs += this.detailimgsArr[i].response.result.url + ','
      }
      this.postData.detailimgs = this.postData.detailimgs.substr(0, this.postData.detailimgs.length - 1)
      this.$ajax(this.$api.cms.addCase.url, {
        method: this.$api.cms.addCase.method,
        data: this.postData
      }).then(res => {
        this.$message({
          message: '添加成功',
          type: 'success',
          duration: 1000
        })
        setTimeout(() => {
          this.$router.push({ name: 'indexCase' })
        }, 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
    },
    handlePictureCardPreviewDetail (res, file, fileList) {
      this.detailimgs = res.result.url
      this.dialogVisibleDetail = false
      this.detailimgsArr = fileList
    },
    handleRemoveDetail (file, fileList) {
      this.detailimgsArr = fileList
    },
    exceed () {
      this.$message({
        message: '超过可传的图片上限',
        type: 'info',
        duration: 1000
      })
    }
  },
  created () {
    this.updateSystemInfo().then(() => {
      this.postData.CaseId = this.defaultCaseId
    })
  }
}
</script>

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