<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.content" placeholder="内容" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">新建</el-button>
    </div>

    <el-table
      :data="notifyList"
      border
      fit
      highlight-current-row
      style="width: 100%;">

      <el-table-column :label="$t('table.id')" prop="id" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="通知内容" align="center">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.content" size="small" style="width:90%"/>
            <el-button
              size="small"
              type="warning"
              @click="createData(row)"
            >
              确定
            </el-button>
          </template>
          <span v-else>{{ row.content }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="updateUser" width="80" label="编辑者" align="center"/>

      <el-table-column label="编辑时间" width="200" align="center">
        <template slot-scope="scope">
          <span >{{ scope.row.updatedAt | formatDate }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" width="190" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            @click="cancelEdit(row)"
          >
            取消
          </el-button>
          <el-button v-else type="primary" size="small" @click="row.edit=!row.edit">{{ $t('table.edit') }}</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">{{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" /> -->

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="105px" style="width: 90%; margin-left:40px;">

        <el-form-item label="通知内容" style="width:100%">
          <el-input :autosize="{ minRows: 2, maxRows: 4}" v-model="temp.content" type="textarea" placeholder="Please input"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="createData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogDeleteVisible" :data="temp" title="Delete">
      <span >
        确定要删除吗?
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="deleteData()">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getNotify, saveNotify, deleteNotify } from '@/api/notify'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import moment from 'moment'

export default {
  name: 'Notify',
  components: { Pagination },
  directives: { waves },
  filters: {
    formatDate(date, pattern = 'YYYY-MM-DD HH:mm:ss') {
      return moment(date).format(pattern)
    }
  },
  data() {
    return {
      notifyList: [],
      listQuery: {},
      temp: {},
      dialogFormVisible: false,
      dialogDeleteVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      rules: {}
    }
  },
  created() {
    this.getNotify()
  },
  methods: {
    getNotify() {
      for (const prop in this.listQuery) {
        if (this.listQuery[prop] === '') {
          delete this.listQuery[prop]
        }
      }
      getNotify(this.listQuery).then(res => {
        this.notifyList = res.data.map(item => {
          this.$set(item, 'edit', false)
          item.originalContent = item.content
          return item
        })
      })
    },

    handleFilter() {
      this.getNotify()
    },

    resetTemp() {
      this.temp = {}
    },

    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },

    createData(row) {
      this.temp = Object.assign(this.temp, row)
      this.temp.updateUser = this.$store.getters.name
      saveNotify(this.temp).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
        this.getNotify()
      })
    },

    cancelEdit(row) {
      row.content = row.originalContent
      row.edit = false
      // this.$message({
      //   message: 'The notify has been restored to the original value',
      //   type: 'warning'
      // })
    },

    handleDelete(row) {
      this.temp = row
      this.dialogDeleteVisible = true
    },

    deleteData() {
      deleteNotify({ '_id': this.temp._id }).then(() => {
        this.dialogDeleteVisible = false
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getNotify()
      })
    }
  }
}
</script>

<style scoped>
.el-form-item {
  width: 600px;
}
</style>
