<template>
  <div class="edit-course-wrapper">
    <el-form
      ref="addcourse"
      :model="formInfo"
      label-width="120px"
      :rules="rules"
    >
      <el-form-item label="课程名称" prop="title">
        <el-input v-model="formInfo.title" />
      </el-form-item>
      <el-form-item label="课程类型" prop="type">
        <treeselect v-model="formInfo.type" :options="typeOptions" style="width: 120px;margin-left: 20px;" />
      </el-form-item>
      <el-form-item label="封面图片">
        <div class="tool-import-cover-wrapper">
          <div class="import-cover-wrapper">
            <label style="width: 100%; height: 100%;">
              导入图片
              <input ref="importfile" type="file" hidden @change="importFile">
            </label>
          </div>
          <div class="progress-wrapper">
            <el-progress :percentage="progressPercent" />
          </div>
        </div>
        <img v-if="fileUrl" style="width: 400px;" :src="fileUrl" alt="没有导入图片">
        <img v-if="formInfo.cover" :src="formInfo.cover" alt="没有导入图片">
      </el-form-item>
      <el-form-item label="课程简介" prop="introduce">
        <el-input
          v-model="formInfo.introduce"
          type="textarea"
          autosize
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div class="submit-course-wrapper">
      <el-button size="medium">提交</el-button>
    </div>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      formInfo: {
        title: '',
        type: '1',
        cover: '',
        introduce: ''
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }
        ]
      },
      progressPercent: 0,
      typeOptions: [
        {
          id: 1,
          label: 'a',
          children: [
            {
              id: 12,
              label: 'aa'
            },
            {
              id: 13,
              label: 'ab'
            }
          ]
        }, {
          id: 2,
          label: 'b',
          children: []
        },
        {
          id: 3,
          label: 'c',
          children: []
        }
      ],
      fileUrl: ''
    }
  },
  mounted() {
    // alert(this.$route.params.name)
  },
  methods: {
    importFile() {
      if (this.$refs.importfile.files.length === 0) {
        this.fileUrl = ''
      } else {
        this.fileUrl = URL.createObjectURL(this.$refs.importfile.files[0])
      }
    }
  }
}
</script>

<style lang="scss">
  .edit-course-wrapper {
    padding-top: 30px;
    width: 800px;
    .submit-course-wrapper {
      display: flex;
      justify-content: flex-end;
    }
  }
  .tool-import-cover-wrapper {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    width: 600px;
    .import-cover-wrapper {
      width: 200px;
      height: 100px;
      background: #A1A1A1;
      color: white;
      font-size: 18px;
      text-align: center;
      line-height: 100px;
    }
    .progress-wrapper {
      flex-grow: 1;
    }
  }
</style>
