<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-form :rules="rules" ref="curriculumddForm" :model="addCurriculum" label-position="left" label-width="120px"
              style='margin-left:20px'>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
              
              <div class="block" style='margin-left:44px'>
               第 <el-input-number :controls="false" style="width: 15%;" class="filter-item" v-model="addCurriculum.stage" >
                </el-input-number> 期
              </div>
           
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('college.c_name')" prop="c_name">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="addCurriculum.c_name" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.c_type')"  prop="c_type">
              <div class="block">
                <el-select style="width: 100%;" class="filter-item" v-model="addCurriculum.c_type" @change="contentChange()">
                  <el-option v-for="item in contentOption"  :key="item.wct_code" :label="item.wct_name" :value="item.wct_code" >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="7" :xl="7">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('college.c_step')" prop="c_step" >
                <el-select style="width: 100%;" class="filter-item" v-model="addCurriculum.c_step">
                  <el-option v-for="item in wcTypeOption" :key="item.value" :label="item.label" :value="item.value" >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
        </el-col>
      </el-row>
      
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('college.city')" prop="city">
              <div class="block">
                <el-select style="width: 100%;" class="filter-item" v-model="addCurriculum.city" >
                   <el-option v-for="item in branchesOptions" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                   </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('college.credit')" prop="credit">
              <div class="block">
                <el-input-number style="width: 100%;" controls-position="right" class="filter-item" v-model="addCurriculum.credit">
                </el-input-number>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="7" :xl="7">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.c_desc')"  prop="c_desc">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="addCurriculum.c_desc">
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        
      </el-row>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('college.m_name')" prop="m_name">
              <div class="block">
                 <el-input style="width: 100%;" controls-position="right" class="filter-item" v-model="addCurriculum.m_name" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.m_address')"  prop="m_address">
              <div class="block">
                 <el-input style="width: 100%;" controls-position="right" class="filter-item" v-model="addCurriculum.m_address" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="12" :lg="7" :xl="7">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('college.latitude')" prop="latitude" >
                <el-input style="width: 100%;" controls-position="right" class="filter-item" v-model="addCurriculum.latitude" >
                </el-input>
              </el-form-item>
            </div>
        </el-col>
      </el-row>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="12" :lg="8" :xl="8">
          <div class="grid-content bg-purple">
            <el-form-item  :label="$t('college.longitude')" prop="longitude">
              <div class="block">
                <el-input style="width: 100%;" controls-position="right" class="filter-item" v-model="addCurriculum.longitude" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.img_list_href')"  prop="img_href">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="addCurriculum.img_href" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
        <el-col :xs="23" :sm="23" :md="11" :lg="7" :xl="7">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.head_img_href')"  prop="head_img_href">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="addCurriculum.head_img_href" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="24" >
        <el-col :xs="23" :sm="23" :md="11" :lg="8" :xl="8">
          <div class="grid-content bg-purple" >
            <el-form-item  :label="$t('college.detail_img_href')"  prop="detail_img_href">
              <div class="block">
                <el-input style="width: 100%;" class="filter-item" v-model="addCurriculum.detail_img_href" >
                </el-input>
              </div>
            </el-form-item>
          </div>
        </el-col>
      </el-row>

      </el-form>
    </div>
    <div slot="footer" class="dialog-footer" align="center" style="width: 90%">
        <el-button type="primary" @click="saveDianping()" :loading ="buttonloading">{{$t('table.save')}}</el-button>
        <el-button @click="resetForm()">{{$t('navbar.reset')}}</el-button>
      </div>
  </div>
</template>
<script>
  import waves from '@/directive/waves' // 水波纹指令
  import { parseTime } from '@/utils'
  import { getWXContentTypes } from '@/api/wxmini'
  import { getBranches } from '@/api/article'
  import { addCollegeCurriculumExperience } from '@/api/college'
  // 富文本
  import Tinymce from '@/components/Tinymce'
  import Upload from '@/components/Upload/singleImage3'
  import MDinput from '@/components/MDinput'
  import Multiselect from 'vue-multiselect'// 使用的一个多选框组件，element-ui的select不能满足所有需求
  import Sticky from '@/components/Sticky' // 粘性header组件
  export default {
    // name: 'curriculumcoreadd', // 不支持keep-alive
    components: { Tinymce, MDinput, Upload, Multiselect, Sticky },
    directives: {
      waves
    },
    data() {
      return {
        tableKey: 0,
        buttonloading: false,
        contentOption: [],
        wcTypeOption: [],
        branchesOptions: [],
        view: {
          name: undefined,
          path: undefined,
          title: undefined
        },
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        addCurriculum: {
          c_name: null,
          c_type: null,
          c_step: null,
          c_desc: null,
          detail_img_href: null,
          head_img_href: null,
          city: null,
          m_name: null,
          m_address: null,
          latitude: null,
          longitude: null,
          credit: null,
          img_href: null,
          stage: null
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          c_name: [{ required: true, message: '请录入课程名称', trigger: 'blur' }],
          c_type: [{ required: true, message: '请选择课程类型', trigger: 'blur' }],
          c_step: [{ required: true, message: '请选择课程展示时间', trigger: 'blur' }],
          city: [{ required: true, message: '请选择门店', trigger: 'blur' }],
          credit: [{ required: true, message: '请录入学分', trigger: 'blur' }],
          c_desc: [{ required: true, message: '请录入课程描述', trigger: 'blur' }],
          m_name: [{ required: true, message: '请录入门店名称', trigger: 'blur' }],
          m_address: [{ required: true, message: '请录入门店地址', trigger: 'blur' }],
          latitude: [{ required: true, message: '请录入经度', trigger: 'blur' }],
          longitude: [{ required: true, message: '请录入纬度', trigger: 'blur' }],
          detail_img_href: [{ required: true, message: '请录入内容', trigger: 'blur' }],
          img_href: [{ required: true, message: '请录入图片地址', trigger: 'blur' }],
          head_img_href: [{ required: true, message: '请录入图片地址', trigger: 'blur' }]
        },
        downloadLoading: false
      }
    },
    created() {
      getWXContentTypes().then(response => {
        this.contentOption = response.data
      })
      getBranches().then(response5 => {
        this.branchesOptions = response5.data
      })
    },
    destroyed() {
      this.view.name = 'curriculumexperienceadd'
      this.view.path = '/college/curriculumexperienceadd'
      this.view.title = 'curriculumexperienceadd'
      this.$store.dispatch('delVisitedViews', this.view)
    },
    methods: {
      resetForm(formName) {
        this.addCurriculum.c_name = null
        this.addCurriculum.c_type = null
        this.addCurriculum.c_step = null
        this.addCurriculum.city = null
        this.addCurriculum.credit = 0
        this.addCurriculum.c_desc = null
        this.addCurriculum.m_name = null
        this.addCurriculum.m_address = null
        this.addCurriculum.latitude = null
        this.addCurriculum.longitude = null
        this.addCurriculum.img_href = null
        this.addCurriculum.detail_img_href = null
        this.addCurriculum.head_img_href = null
        this.wcTypeOption = []
      },
      saveDianping() {
        this.$refs['curriculumddForm'].validate((valid) => {
          if (valid) {
            this.buttonloading = true
            this.addCurriculum.stage = '第' + this.addCurriculum.stage + '期'
            this.addCurriculum.create_user = this.$store.state.user.introduction
            addCollegeCurriculumExperience(this.addCurriculum).then(response => {
              if (response.data === 1) {
                this.$notify({
                  title: '成功',
                  message: '保存成功!',
                  type: 'success',
                  duration: 2000
                })
              } else if (response.data === 0) {
                this.$notify({
                  title: '修改失败',
                  message: '课程已存在！',
                  type: 'warning',
                  duration: 2000
                })
              } else {
                this.$notify({
                  title: '失败',
                  message: '请联系管理员！',
                  type: 'error',
                  duration: 2000
                })
              }
              this.buttonloading = false
              this.resetForm()
            })
          }
        })
      },
      contentChange() {
        this.wcTypeOption = []
        this.addCurriculum.c_step = null
        for (let i = 0; i < this.contentOption.length; i++) {
          if (this.contentOption[i].wct_code === this.addCurriculum.c_type) {
            this.wcTypeOption = this.range(1, this.contentOption[i].wct_step, 1)
            break
          }
        }
      },
      range(start, end, step) {
        var arr = []
        for (let i = start; i <= end; i++) {
          var tempOption = {
            label: null,
            value: null
          }
          if (i % step === 0) {
            tempOption.value = i
            tempOption.label = i
            arr.push(tempOption)
          }
        }
        return arr
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      }
    }
  }
</script>
<style>
.el-form--label-left .el-form-item__label{
  text-align:right
}
.el-rate{
  line-height: 0;
  margin-top:8px;
}
.el-checkbox.el-checkbox {
    margin-left: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";
  .title-prompt{
    position: absolute;
    right: 0px;
    font-size: 12px;
    top:10px;
    color:#ff4949;
  }
  .createPost-container {
    position: relative;
    .createPost-main-container {
      padding: 40px 45px 20px 50px;
      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;
        .postInfo-container-item {
          float: left;
        }
      }
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
            text-align: right;
            margin-right: 10px;
            .editor-upload-btn {
                display: inline-block;
            }
        }
      }
    }
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
  }
</style>