<template>
  <div class="sales-page">
    <Card>
      <div class="search-con">
        <Input placeholder="请输入促销活动编号" clearable class="search-input" v-model="searchContent.promotionId" style="width:200px;"/>
        <Input placeholder="请输入促销活动名称" clearable class="search-input" v-model="searchContent.name" style="width:200px;"/>
        <Select class="search-input"  v-model="searchContent.status" style="width:200px;"
          @on-change="handleChange">
          <Option value>全部</Option>
          <Option v-for="(item, index) in salesStatusData"
            :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>
      </div>
      <tables
        ref="tables"
        editable
        pagable
        v-model="tableData"
        :columns="columns"
        :currentPage="searchContent.pageNum"
        :pageSize="searchContent.pageSize"
        :totalSize="totalSize"
        :loading="loading"
        @on-change="handleCurrentChange"
      />
    </Card>
  </div>
</template>
<script>
import Tables from '_c/tables'
import { SalesRulesObj, PromotionStatusObject, PromotionStatus } from '@/enums'
import { formatTime } from '@/libs/tools'
export default {
  inject: ['reload'],
  components: {
    Tables
  },
  data () {
    return {
      searchContent: {
        name: '',
        page: 1,
        status: '',
        pageSize: 10
      },
      salesStatusData: [
        {
          name: '已取消',
          index: 0
        },
        {
          name: '进行中',
          index: 1
        },
        {
          name: '未开始',
          index: 2
        },

        {
          name: '已结束',
          index: 3
        }

      ],
      tableData: [],
      columns: [
        { title: '编号', key: 'promotionId', align: 'left', minWidth: 120 },
        { title: '名称', key: 'name', align: 'center', width: 120 },
        { title: '类型',
          key: 'type',
          align: 'center',
          width: 80,
          render: (h, params) => {
            let type = params.row.type
            return h('div', SalesRulesObj[type])
          }
        },
        { title: '使用条件',
          key: 'remark',
          align: 'center',
          minWidth: 120,
          render: (h, params) => {
            let x
            switch (params.row.type) {
              case 4:
                x = h('span', `直降 ${params.row.promotionRule.promotionVal} 元`)
                break
              case 2:
                x = h('span', `满 ${params.row.promotionRule.conditionVal}元减${params.row.promotionRule.promotionVal} 元`)
                break
              case 3:
                x = h('span', `每满 ${params.row.promotionRule.conditionVal}元减${params.row.promotionRule.promotionVal} 元`)
                break
              case 5:
                x = h('span', `${params.row.promotionRule.promotionVal * 10} 折`)
                break
            }
            return x
          }
        },
        { title: '开始时间',
          align: 'center',
          minWidth: 110,
          render: (h, params) => {
            return h('span', formatTime(params.row.startTime, '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: '结束时间',
          align: 'center',
          minWidth: 110,
          render: (h, params) => {
            return h('span', formatTime(params.row.endTime, 'yyyy-MM-dd'))
          },
          renderHeader: (h, params) => {
            return h('span',
              [
                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: {
                      marginLeft: '2px',
                      transform: 'translateY(-1px)'
                    }
                  })])
                ]
              ])
          }
        },
        { title: '状态',
          key: 'status',
          align: 'center',
          width: 120,
          render: (h, params) => {
            return h('Tag', {
              props: {
                color: PromotionStatusObject[params.row.status].color
              }
            },
            PromotionStatusObject[params.row.status].label
            )
          }
        },
        { title: '操作',
          key: 'index',
          align: 'center',
          // fixed: 'right',
          minWidth: 230,
          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 == PromotionStatus.NOT_START ? 'inline-block' : 'none'
                }
              },
              '编 辑'
            )
            let delBtn = h(
              'Button',
              {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: this.handleDel(params.row)
                },
                style: {
                  marginRight: '5px',
                  display: params.row.status == PromotionStatus.NOT_START ? 'inline-block' : 'none'
                }
              },
              '取 消'
            )
            return h('div', [detailBtn, !this.platform && editBtn, !this.platform && delBtn])
          } }
      ],
      loading: false,
      totalSize: 0
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.api({
        apiName: 'getPromotionList',
        postdata: this.searchContent,
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data.records
          this.totalSize = res.data.total
        }
      })
    },
    handleChange () {

    },
    handleSearch () {
      this.searchContent.pageNum = 1
      this.requestData()
    },
    handleAdd () {
      this.$router.push({ name: 'add_promotion' })
    },
    handleCurrentChange () {

    },
    handleRefresh () {
      this.reload()
    },
    // 详情
    handleDetail (row) {
      return () => {
        let query = { promotionId: row.promotionId }
        this.$router.push({ name: 'promotion_detail', query })
      }
    },
    // 编辑
    handleUpdate (row) {
      return () => {
        let query = { promotionId: row.promotionId }
        this.$router.push({ name: 'update_promotion', query })
      }
    },
    // 取消活动
    handleDel (row) {
      return () => {
        this.$Modal.confirm({
          title: '取消',
          content: `您确认要取消名称为${row.name} 的活动？`,
          onOk: () => {
            this.api({
              apiName: 'deletePromotion',
              postdata: [row.promotionId],
              success: () => {
                this.requestData()
                this.$Notice.success({ title: `操作成功！` })
              }
            })
          }
        })
      }
    }
  },
  created () {
    this.requestData()
  }
}
</script>
<style scoped lang="less">
.sales-page .search-con {
  display: flex;
  padding: 10px 0;
  .search-input {
    width: 300px;
    margin-right: 10px;
  }
}
</style>
