<template>
  <div class="notice-page">
    <Card>
      <div class="search-con">
        <Input placeholder="请输入活动编号" class="search-input" v-model="searchContent.returnId" style="width: 200px"/>
        <Input placeholder="请输入活动名称" class="search-input" v-model="searchContent.name" style="width: 200px"/>
        <Select class="search-input" placeholder="请选择活动状态"  v-model="searchContent.status" style="width: 200px">
          <Option value="">全部</Option>
          <Option v-for="(item, index) in integralStatusData"
            :value="item.index"
            :key="'series-option-' + index"
          >
            {{item.name}}
          </Option>
        </Select>
        <ButtonGroup class="search-btngroup">
          <Button @click="handleSearch" type="primary">查找</Button>
          <Button type="primary" @click="handleRefresh">
            <Icon type="md-refresh" />
          </Button>
        </ButtonGroup>
        <Button type="info" style="margin-left: 20px" @click="handleAdd">+新增</Button>
        <Button type="error" style="margin-left: 20px" @click="handleBatch">批量删除</Button>
      </div>
      <tables
        ref="tables"
        editable
        pagable
        v-model="tableData"
        :columns="columns"
        :currentPage="searchContent.pageNum"
        :pageSize="searchContent.pageSize"
        :totalSize="totalSize"
        :loading="loading"
        @on-change="handleCurrentChange"
        @on-selection-change="changeSelect"
      />
    </Card>
  </div>
</template>
<script>
import Tables from '_c/tables'
import { formatTime } from '@/libs/tools'
import { IntegralStatus, IntegralStatusObj } from '@/enums'
export default {
  inject: ['reload'],
  components: {
    Tables
  },
  data () {
    return {
      searchContent: {
        returnId: '',
        name: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      integralStatusData: [
        // {
        //   name: '删除',
        //   index: 0
        // },
        {
          name: '未开始',
          index: 1
        },
        {
          name: '可用',
          index: 2
        },
        {
          name: '不可用',
          index: 3
        },
        {
          name: '已过期',
          index: 4
        }

      ],
      tableData: [],
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        { title: '编号', key: 'returnId', align: 'center', minWidth: 150 },
        { title: '名称', key: 'name', align: 'center', width: 120 },
        { title: '返现数量', key: 'num', align: 'center', width: 100 },
        { title: '返现条件',
          key: 'index',
          align: 'center',
          minWidth: 150,
          render: (h, params) => {
            let x
            switch (params.row.returnCondition) {
              case 1:
                x = h('span', `每 ${params.row.conditionVal}个商品返现`)
                break
              case 2:
                x = h('span', `满 ${params.row.conditionVal}元返现`)
                break
            }
            return x
          }
        },
        { title: '开始时间',
          align: 'center',
          width: 120,
          render: (h, params) => {
            return h('span', formatTime(params.row.effectiveStartTime, 'yyyy-MM-dd'))
          },
          renderHeader: (h, params) => {
            return h('div',
              [
                h('span', {
                  style: {
                    marginLeft: '8px'
                  }
                }, '开始时间'),
                [
                  h('Tooltip', {
                    props: {
                      trigger: 'hover',
                      content: `活动从当天的00:00:00开始`,
                      wordWrap: true,
                      width: 200,
                      transfer: true
                    }
                  }, [ h('Icon', {
                    props: {
                      type: 'ios-help-circle-outline',
                      size: 14
                    },
                    style: {
                      marginRight: '2px',
                      transform: 'translateY(-1px)'
                    }
                  })])
                ]
              ])
          }
        },
        { title: '结束时间',
          // key: 'endTime',
          align: 'center',
          width: 120,
          render: (h, params) => {
            return h('span', formatTime(params.row.effectiveEndTime, 'yyyy-MM-dd'))
          },
          renderHeader: (h, params) => {
            return h('div',
              [
                h('span', {
                  style: {
                    marginLeft: '8px'
                  }
                }, '结束时间'),
                [
                  h('Tooltip', {
                    props: {
                      trigger: 'hover',
                      content: `活动到当天的23:59:59结束`,
                      wordWrap: true,
                      width: 200,
                      transfer: true
                    }
                  }, [ h('Icon', {
                    props: {
                      type: 'ios-help-circle-outline',
                      size: 14
                    },
                    style: {
                      marginRight: '2px',
                      transform: 'translateY(-1px)'
                    }
                  })
                  ])
                ]
              ])
          }
        },
        { title: '状态',
          key: 'status',
          align: 'center',
          width: 100,
          render: (h, params) => {
            return h('Tag', {
              props: {
                color: IntegralStatusObj[params.row.status].color
              }
            },
            IntegralStatusObj[params.row.status].label
            )
          } },
        { title: '操作',
          key: 'index',
          align: 'center',
          minWidth: 150,
          render: (h, params) => {
            let detailBtn = h(
              'Button',
              {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: this.handleDetail(params.row)
                }
              },
              '详 情'
            )
            let editBtn = h(
              'Button',
              {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: this.handleUpdate(params.row)
                },
                style: {
                  marginRight: '5px',
                  display: params.row.status == IntegralStatus.NOT_START || params.row.status == IntegralStatus.ABLE ? 'inline-block' : 'none'
                }
              },
              '编 辑'
            )
            let delBtn = h(
              'Button',
              {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: this.handleDelete(params.row)
                },
                style: {
                  marginRight: '5px',
                  display: params.row.status == IntegralStatus.DISABLE || params.row.status == IntegralStatus.EXPIRED ? 'inline-block' : 'none'
                }
              },
              '删 除'
            )
            let offShelfBtn = h(
              'Button',
              {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: this.handleOffShelf(params.row)
                },
                style: {
                  marginRight: '5px',
                  display: params.row.status == IntegralStatus.ABLE ? 'inline-block' : 'none'
                }
              },
              '下 架'
            )
            return h('div', [detailBtn, editBtn, delBtn, offShelfBtn])
          }
        }
      ],
      loading: false,
      totalSize: 0,
      selectCount: 0
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.api({
        apiName: 'integralReturnPage',
        postdata: this.searchContent,
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data.records
          this.totalSize = res.data.total
        }
      })
    },
    handleRefresh () {
      this.reload()
    },
    handleSearch () {
      this.searchContent.pageNum = 1
      this.requestData()
    },
    handleCurrentChange (pageNum) {
      this.searchContent.pageNum = pageNum
      this.requestData()
    },
    handleAdd () {
      this.$router.push({ name: 'add_integral' })
    },
    handleUpdate (row) {
      return () => {
        this.$router.push({ name: 'update_integral', query: { returnId: row.returnId } })
      }
    },
    handleDetail (row) {
      return () => {
        this.$router.push({ name: 'integral_detail', query: { returnId: row.returnId } })
      }
    },
    handleDelete (row) {
      return () => {
        this.$Modal.confirm({
          title: '删除',
          content: `您确认要删除名称为${row.name} 的积分？`,
          onOk: () => {
            this.api({
              apiName: 'deleteIntegralReturn',
              postdata: row.returnId,
              success: () => {
                this.requestData()
                this.$Notice.success({ title: `操作成功！` })
              }
            })
          }
        })
      }
    },
    handleOffShelf (row) {
      return () => {
        this.$Modal.confirm({
          title: '下架',
          content: `您确认要下架名称为${row.name} 的积分？`,
          onOk: () => {
            this.api({
              apiName: 'offShelfIntegralReturn',
              postdata: row.returnId,
              success: () => {
                this.requestData()
                this.$Notice.success({ title: `操作成功！` })
              }
            })
          }
        })
      }
    },
    changeSelect (e) {
      let obj = {}
      this.selectList = e.map(item => {
        obj = {
          returnId: item.returnId,
          status: item.status
        }
        return obj
      })
      this.selectCount = e.length
    },
    handleBatch () {
      if (this.selectCount <= 0) {
        this.$Message.warning('您还未选择要删除的数据')
        return false
      }
      let returnIds = []
      let status = this.selectList.some(item => {
        return item.status == 2
      })
      if (status == true) {
        this.$Message.warning('您选中的积分返现有进行中')
        return false
      } else {
        this.selectList.forEach(item => {
          returnIds.push(item.returnId)
        })
      }
      console.log(returnIds)
      this.$Modal.confirm({
        title: '删除',
        content: `您确认要删除所选的 ${this.selectCount}条数据？`,
        onOk: () => {
          this.api({
            apiName: 'deleteBatchIntegralReturn',
            postdata: returnIds,
            success: () => {
              this.requestData()
              this.$Notice.success({ title: `操作成功！` })
            }
          })
        }
      })
    }
  },
  created () {
    this.requestData()
  }
}
</script>
<style scoped lang="less">
.notice-page .search-con {
  display: flex;
  padding: 10px 0;
  .search-input {
    width: 250px;
    margin-right: 10px;
  }
}
</style>
