<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>课程主图：</span>
          <div class="flex-item">
            <div>
              <el-upload
                class="avatar-uploader"
                :action='$api.file.image.url'
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="detail.CourseImg" :src="detail.CourseImg" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程名称：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-input
                placeholder="请输入课程名称"
                v-model="detail.CourseName"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程类型：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-select v-model="detail.LocationId" placeholder="请选择">
                <el-option
                  v-for="item in location"
                  :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: 200px;">
              <el-input
                placeholder="请输入课程价格"
                v-model="detail.Price"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>案场：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-select v-model="CaseId" placeholder="请选择">
                <el-option
                  v-for="item in cases"
                  :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: 200px;">
              <el-input
                placeholder="请输入开课人数"
                v-model="detail.MaxNum"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>最小开课人数：</span>
          <div class="flex-item">
            <div style="width: 200px;">
              <el-input
                placeholder="请输入开课人数"
                v-model="detail.MinNum"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课时数：</span>
          <div class="flex-item">
            <div style="width: 200px;">
              <el-input
                placeholder="请输入课时数"
                v-model="detail.CourseNum"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程时间：</span>
          <div class="flex-item">
            <div>
              <el-date-picker
                v-model="courseDate"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-ddTHH:mm:ss"
                :picker-options="pickerOptions">
              </el-date-picker>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程计划备注：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-input
                type="textarea"
                autosize
                placeholder="请输入备注"
                v-model="detail.Remark"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程标签：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-select
              style="width:100%"
                v-model="tags"
                multiple
                filterable
                allow-create
                default-first-option
                placeholder="请选择课程标签">
                <el-option
                  v-for="item in tagList.list"
                  :key="item.TagId"
                  :label="item.TagName"
                  :value="item.TagId"
                  :value-key="item.TagId"
                  >
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>课程简介：</span>
          <div class="flex-item">
            <div style="width: 50%;">
              <el-input
                type="textarea"
                autosize
                placeholder="请输入课程简介"
                v-model="detail.CourseDesc"
                clearable
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>是否精选课程：</span>
          <div class="flex-item">
            <div>
              <el-radio v-model="detail.IsSelect" :label="1">是</el-radio>
              <el-radio v-model="detail.IsSelect" :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 { createNamespacedHelpers, mapState } from 'vuex'

const { mapState: mapCourseState, mapActions: mapCourseActions } = createNamespacedHelpers('course')
const { mapState: mapLocationState, mapActions: mapLocationActions } = createNamespacedHelpers('cms')
const { mapState: mapTagState, mapActions: mapTagActions } = createNamespacedHelpers('coursetag')

export default {
  name: '',
  data () {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
    }
  },
  computed: {
    ...mapLocationState({
      location: x => x.location
    }),
    ...mapCourseState({
      detail: x => x.courseInfo
    }),
    ...mapState({
      cases: x => x.app.cases.list,
      caseid: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId,
    }),
    ...mapTagState({
      tagList: x => x.tagList
    }),
    CaseId: {
      get () {
        return this.detail.CaseId || this.caseid
      },
      set (val) {
        this.UpdateInfo({...this.detail, CaseId: val})
      }
    },
    courseDate: {
      get () {
        return this.detail.BeginDate && this.detail.EndDate ? [this.detail.BeginDate, this.detail.EndDate] : []
      },
      set (val) {
        this.UpdateInfo({...this.detail, BeginDate: val[0], EndDate: val[1]})
      },
    },
    tags: {
      get () {
        return (this.detail.CourseTags || []).map(x => x.TagId)
      },
      set (val) {
        let v = val.map(x => {
          return {
            TagId: x,
          }
        })
        this.UpdateInfo({...this.detail, CourseTags: v})
      }
    },
  },
  components: {},
  methods: {
    ...mapCourseActions([
      'GetCourseByID',
      'AddCourse',
      'UpdateCourse',
      'UpdateInfo',
    ]),
    ...mapLocationActions([
      'updateLocationInfo',
    ]),
    ...mapTagActions([
      'GetCourseTagList',
    ]),
    handleAvatarSuccess (res, file) {
      this.UpdateInfo({...this.detail, CourseImg: res.result.url})
    },
    submit () { // 提交数据
      const { id } = this.$route.query
      if (!id || id === '') {
        this.UpdateInfo({...this.detail, CourseId: ''})
      }
      const _that = this
      this.detail.tagids = this.tags.map(x => {
        let tagfilter = (_that.tagList.list || []).filter(tag => tag.TagId === x)
        if (tagfilter.length > 0) {
          return tagfilter[0].TagName + ':' + tagfilter[0].TagId
        } else {
          return x
        }
      }).join(',')
      if ((this.detail.CourseId || '') === '') {
        this.detail.OrgId = this.orgid
        if (!this.detail.CaseId || this.detail.CaseId === '') {
          this.detail.CaseId = this.caseid
        }
        this.AddCourse(this.detail)
      } else {
        this.UpdateCourse(this.detail)
      }
      this.$message({
        type: 'success',
        message: '操作成功'
      })
      this.$router.push({ name: 'courseList' })
    },
    cancel () {
      this.$router.push({ name: 'courseList' })
    }
  },
  mounted () {
    this.updateLocationInfo()
    this.GetCourseTagList({ pagesize: 100 })
    const { id } = this.$route.query
    if (id && id !== '') {
      this.GetCourseByID({ id: id })
    } else {
      // this.SetNull()
    }
  }
}
</script>

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