/* eslint-disable no-duplicate-case */
<template>
  <div class="school-container">
    <div class="school">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="产品名称">
          <el-input v-model="formInline.productName" placeholder="产品名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('ruleForm')">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="getMemberAuthData"
        style="width: 100%"
      >
        <el-table-column
          label="用户id"
          width="280"
          prop="uid"
        />
        <el-table-column
          label="产品名称"
          width="280"
          prop="productName"
        />
        <el-table-column
          label="开始时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="结束时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="修改时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.updateTime) }}</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>
          </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="title">
            <el-input v-model="form.title" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="增加天数" :label-width="formLabelWidth" prop="addDays">
            <el-input v-model="form.addDays" autocomplete="off" clearable />
          </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>
      <!--
      <el-pagination
        v-if="getMemberOrderData.length > 0"
        background
        layout="prev, pager, next"
        :total="getMemberData.totalSize"
        @current-change="handleCurrentChange"
      />
      -->
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      formInline: {
        uid: '',
        productName: ''
      },
      page: 1,
      formLabelWidth: '120px',
      form: {
        uid: '',
        title: '',
        addDays: ''
      },
      rules: {
        title: [{ required: true, message: '请输入产品', trigger: 'blur' }],
        addDays: [{ required: true, message: '请输入增加天数', trigger: 'blur' }]
      },
      dialogFormVisible: false
    }
  },
  created() {
    this.$store.dispatch('memberList/getMemberAuthList', {
      uid: this.$route.query.uid,
      productName: ''
    })
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters([
      'getMemberAuthData'
    ])
  },
  methods: {
    newDate(date) {
      return new Date(date * 1).toLocaleString()
    },
    onSubmit(formName) {
      this.formInline.uid = this.$route.query.uid
      this.$store.dispatch('memberList/getMemberAuthList', this.formInline)
    },
    handleEdit(index, row) {
      console.log(index, row)
      this.dialogFormVisible = true
      this.form.uid = row.uid
    },
    handOk(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch('memberList/setMemberAuthList', this.form).then(() => {
            this.dialogFormVisible = false
            this.$store.dispatch('memberList/getMemberAuthList', {
              uid: this.$route.query.uid,
              productName: ''
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCurrentChange(val) {
      console.log(val)
      this.page = val
      this.$store.dispatch('schoolTimetable/getScheduleList', {
        page: this.page
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.school {
  margin-top: 20px;
}
.el-input {
  width: 300px;
}
.el-select>.el-input {
  width: 300px;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>
