<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input placeholder="科普文章"
                  v-model="dataForm.title"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addContent(0)">添加文章</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%;">
      <el-table-column type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column>
       <el-table-column prop="id"
                       header-align="center"
                       align="center"
                       label="编号"
                       width="200">
      </el-table-column>
      <el-table-column prop="name"
                       header-align="center"
                       align="center"
                       width="300"
                       label="名称">
      </el-table-column>
      <el-table-column prop="type"
                       header-align="center"
                       align="center"
                       label="分类">
        <template slot-scope="scope">
          <span>{{ checkType(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="date"
                       header-align="center"
                       align="center"
                       label="时间"
                       width="180">
      </el-table-column>
      <el-table-column fixed="right"
                       header-align="center"
                       align="center"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="text"
                     size="small"
                     @click="addContent(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle"
                   @current-change="currentChangeHandle"
                   :current-page="pageIndex"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="pageSize"
                   :total="totalPage"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <add-or-update v-if="addContentF"
                   ref="addOrUpdate"
                   @refreshDataList="getDataList"></add-or-update>
    <detail v-if="addContentD" ref="detail"></detail>
  </div>
</template>

<script>
import AddOrUpdate from './patternsen-add-or-update.vue'
import Detail from './patternsen-detail.vue'
export default {
  data () {
    return {
      dataForm: {
        name: ''
      },
      dataWashingForm: {
        washingDate: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addContentF: false,
      addContentD: false,
      addContentW: false
    }
  },
  components: {
    AddOrUpdate,
    Detail
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/api/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.dataForm.title
        })
      }).then(({ data }) => {
        if (data && data.successful) {
          var content = data.resultValue
          this.dataList = content.content
          this.totalPage = content.totalElements
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.$router.push({ path: '/textbook/update/' + id })
    },
    updateWashing (row) {
      this.addContentW = true
      this.dataWashingForm = JSON.parse(JSON.stringify(row))
      console.log(row)
    },
    detail (code) {
      this.addContentD = true
      console.log(code)
      this.$nextTick(() => {
        this.$refs.detail.init(code)
      })
    },
    // 删除
    deleteHandle (id) {
      let articleIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${articleIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/pattern/del'),
          method: 'post',
          data: this.$http.adornData({
            'id': id
          })
        }).then(({ data }) => {
          if (data && data.successful) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    },
    checkType (type) {
      if (type === 1) {
        return '可回收垃圾'
      } else if (type === 2) {
        return '厨余垃圾'
      } else if (type === 3) {
        return '有害垃圾'
      } else {
        return '其他'
      }
    },
    // 新增 / 修改
    addContent (id) {
      this.$router.push({ path: '/patternsen/update/' + id })
    }
  }
}
</script>

<style scoped>
</style>
