<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <el-button type="primary" @click="addList">增加项列表</el-button>
      <el-table
        :data="getRowItemData"
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          width="60"
          prop="sort"
        />
        <el-table-column
          label="id"
          width="160"
          prop="id"
        />
        <el-table-column
          label="项目名称"
          width="150"
          prop="content"
        />
        <el-table-column
          label="项目icon"
          width="180"
        >
          <template slot-scope="scope">
            <img :src="scope.row.icon" style="width: 100%">
          </template>
        </el-table-column>
        <el-table-column
          label="跳转类型"
          width="80"
        >
          <template slot-scope="scope">
            <span>{{ targetType(scope.row.targetType) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="80"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.status === 'HAS_ON_STOCK' ? '上架' : '下架' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ newDate(scope.row.gmtCreated) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="修改时间"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ newDate(scope.row.gmtModified) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              @click="handleUp(scope.$index, scope.row)"
            >{{ scope.row.status === 'HAS_ON_STOCK' ? '下架' : '上架' }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="首页项列表" :visible.sync="dialogFormVisible">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item label="项目名称" :label-width="formLabelWidth" prop="content">
            <el-input v-model="form.content" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item v-if="type === 'add'" label="icon" :label-width="formLabelWidth">
            <el-input v-model="form.icon" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item v-if="type === 'add'" label="跳转类型" :label-width="formLabelWidth">
            <el-select v-model="form.targetType" placeholder="请选择">
              <el-option value="LIBRARY" label="图书馆课程" />
              <el-option value="PAY" label="知识付费" />
              <el-option value="TOPIC" label="专题" />
              <el-option value="SCHEDULE" label="课程表" />
              <el-option value="URL" label="URL" />
            </el-select>
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
            <el-input v-model="form.sort" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item v-if="type === 'add'" label="跳转地址" :label-width="formLabelWidth">
            <el-input v-model="form.targetContent" autocomplete="off" clearable />
            <el-tag type="danger">请输入Code</el-tag>
          </el-form-item>
          <el-form-item v-if="type === 'add'" label="状态" :label-width="formLabelWidth" prop="status">
            <el-select v-model="form.status" placeholder="请选择">
              <el-option value="NOT_ON_STOCK" label="下架" />
              <el-option value="HAS_ON_STOCK" label="上架" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handOk('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Dashboard',
  data() {
    return {
      form: {},
      dialogFormVisible: false,
      type: '',
      formLabelWidth: '120px',
      rules: {
        content: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '请输入icon', trigger: 'blur' }
        ],
        targetType: [{ required: true, message: '请选择模板', trigger: 'change' }],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        targetContent: [{ required: true, message: '请输入项目Code', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.$store.dispatch('dashboard/getRowItemList', {
      id: this.$route.query.id
    })
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters([
      'getRowItemData'
    ])
  },
  methods: {
    newDate(date) {
      return new Date(date).toLocaleString()
    },
    name() {
      const id = this.$route.query.id
      let name = ''
      switch (id) {
        case '0':
          name = '知识付费'
          break
        case '1':
          name = '学前'
          break
        case '2':
          name = '小学'
          break
        case '3':
          name = '中学'
          break
      }
      return name
    },
    template(code) {
      let templateCode = ''
      switch (code) {
        case 'IMAGE_TEMPATE_5':
          templateCode = '689*174图片模板'
          break
        case 'TEXT_TEMPLATE':
          templateCode = '文字模板'
          break
        case 'IMAGE_TEMPATE_1':
          templateCode = '840*300图片模板'
          break
        case 'IMAGE_TEMPATE_2':
          templateCode = '548*300图片模板'
          break
        case 'IMAGE_TEMPATE_3':
          templateCode = '404*256图片模板'
          break
        case 'IMAGE_TEMPATE_4':
          templateCode = '256*360图片模板'
          break
        case 'SCHEDULE_TEMPATE_1':
          templateCode = '1414*172课程表模板'
          break
        case 'SCHEDULE_TEMPATE_2':
          templateCode = '326*174课程表模板'
          break
        case 'SCHEDULE_TEMPATE_3':
          templateCode = '1414*172课程表模板'
          break
      }
      return templateCode
    },
    targetType(type) {
      let target = ''
      switch (type) {
        case 'LIBRARY':
          target = '图书馆课程'
          break
        case 'PAY':
          target = '知识付费课程'
          break
        case 'TOPIC':
          target = '专题'
          break
        case 'SCHEDULE':
          target = '课程表'
          break
        case 'URL':
          target = 'URL'
          break
      }
      return target
    },
    handleEdit(index, row) {
      console.log(index, row)
      this.dialogFormVisible = true
      this.type = 'set'
      this.form = {
        'id': '',
        'rowId': '',
        'content': '',
        'icon': '',
        'targetType': '',
        'targetContent': '',
        'sort': '',
        'status': ''
      }
      this.setForm(row)
    },
    addList() {
      this.dialogFormVisible = true
      this.type = 'add'
      this.form = {
        'rowId': this.$route.query.id,
        'content': '',
        'icon': '',
        'targetType': '',
        'targetContent': '',
        'sort': '',
        'status': ''
      }
    },
    handleUp(index, row) {
      console.log(index, row)
      this.setForm(row)
      this.form.status = row.status === 'NOT_ON_STOCK' ? 'HAS_ON_STOCK' : 'NOT_ON_STOCK'
      this.$store.dispatch('dashboard/setRowItemList', this.form).then(() => {
        this.$store.dispatch('dashboard/getRowItemList', {
          id: this.$route.query.id
        })
      })
    },
    handOk(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.type === 'set') {
            console.log(this.form)
            this.$store.dispatch('dashboard/setRowItemList', this.form).then(() => {
              this.dialogFormVisible = false
              this.$store.dispatch('dashboard/getRowItemList', {
                id: this.$route.query.id
              })
            })
          } else {
            this.$store.dispatch('dashboard/addRowItemList', this.form).then(() => {
              this.dialogFormVisible = false
              this.$store.dispatch('dashboard/getRowItemList', {
                id: this.$route.query.id
              })
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    setForm(data) {
      this.form = {
        'id': data.id,
        'rowId': data.rowId,
        'content': data.content,
        'icon': data.icon,
        'targetType': data.targetType,
        'targetContent': data.targetContent,
        'sort': data.sort,
        'status': data.status
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
  .el-input {
    width: 280px;
  }
  .el-select>.el-input {
    width: 300px;
  }
  .el-pagination {
    text-align: center;
    margin-top: 20px;
  }
  .el-tag.el-tag--danger {
    float: right;
  }
</style>
