
<template>
  <div class="root">
    <el-form :inline="true" :model="listQuery" class="form-listQuery">
      <el-form-item class="filter-item" label="商铺开关">
        <el-switch
          v-model="shopSettingValue"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="changeSetting">
        </el-switch>
         <span  style="font-size:0.7vw;color:#C0C0C0;">关闭后业主在APP端将看不到商铺相关的页面</span>
      </el-form-item>
    </el-form>
    <div class="button">
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="warning" @click="edit">修改</el-button>
      <el-button type="danger" @click="deleteShopType">删除</el-button>
      <span  style="font-size:0.7vw;color:#C0C0C0;padding-left:25%">索引顺序即显示时的先后顺序，不能相同，必须为大于0的整数，数字越小越靠前，如果有1则1号值排会在APP端第一位</span>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="shopTypeList"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column prop="id" label="编号" align="center"/>
      <el-table-column prop="typeName" label="商铺类型" align="center"/>
      <el-table-column prop="sort" label="索引" align="center"/>
      <el-table-column prop="createDate" label="发布时间" align="center"><template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template></el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page.sync="listQuery.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="listQuery.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'

export default {
  data() {
    return {
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      shopSettingValue: false,
      total: 0, // 数据总数
      shopTypeList: [],
      deleteIds: [], // 选择的id集合
      listLoading: true // 加载圈
    }
  },
  mounted() {
    // 获取数据
    this.dataQuery()
  },
  methods: {
    ...mapActions('shopType', [
      'FetchShopTypeList',
      'ChangeShopSetting'
    ]),
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listQuery.pageSize = val
      this.dataQuery()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listQuery.pageNum = val
      this.dataQuery()
    },
    padDate(value) {
      value = value < 10 ? '0' + value : value
      return value
    },
    dataQuery() { // 查询数据
      this.listLoading = true
      this.FetchShopTypeList(this.listQuery).then((resData) => {
        this.shopTypeList = resData.dataList
        this.total = resData.total
        this.shopSettingValue = resData.shopSettingValue
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
        console.log('error shopTypeList')
      })
    },
    handleSelectionChange(data) {
      // 设置为 空
      this.deleteIds = []
      for (let i = 0; i < data.length; i++) {
        this.deleteIds.push(data[i].id)
      }
    },
	  search() {
      this.listQuery.pageNum = 1
      this.dataQuery()
    },
    changeSetting() {
      this.ChangeShopSetting(this.shopSettingValue).then((res) => {
        this.search()
      }).catch(() => {
        console.log('error shopTypeList')
      })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = 10
      this.listQuery.ownerName =''
      this.listQuery.announcementNumber=''
      this.listQuery.announcementTitle=''
      this.dataQuery()
    },
    // 添加公告
    add() {
      this.$router.push({ name: 'shopSetting-add' })
    },
    edit(){// 编辑公告
      const ids = this.deleteIds
      if (ids.length > 1) {
        this.$message.error('只能选择一行数据进行修改!')
        return
      }
      if (ids < 1) {
        this.$message.error('请选择一行数据进行修改!')
        return
      }
       let ide= this.deleteIds[0]
       this.listQuery.id = ide
       this.$router.push({ name: 'shopSetting-edit', query: { id: ide }})
    },
    deleteShopType(){
      const ids = this.deleteIds
      this.listLoading = true
      this.$store.dispatch('shopType/DeleteShopType', ids).then((res) => {
        this.listLoading = false
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.dataQuery()
      }).catch(() => {
        this.listLoading = false
        console.log('error DeleteShopType')
      })
    },
    clickTitle(id) {
      this.$router.push({ name: 'contentParticulars-details', query: { id: id }})
    },
    formatDate(val) {
      if (val === null) {
        return ''
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = value.getMonth() + 1
      var day = value.getDate()
      var hour = value.getHours()
      var minutes = value.getMinutes()
      var seconds = value.getSeconds()
      // return year + '-' + month + '-' + day
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
    }
  }
}
</script>

<style scoped>
.root{
  display: flex;
  flex-flow: column;
}
.form-listQuery{
  margin-top: 20px;
  margin-left: 30px;
}
.operation{
  display: flex;
  justify-content: space-between;
  margin-left: 20px;
  margin-right: 20px;
}
.block{
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.button {
  margin-left: 20px;
}
</style>

