<template>
  <div style="padding: 0 20px">
    <div ref="lxHeader">
      <lx-header icon="md-apps" text="活动列表" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
        <slot>
          <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;">
            <Button icon="ios-add" type="primary" style="margin-right: 10px;" @click="$refs['addActivity'].isShow = true,$refs['addActivity'].type = 'add'">添加</Button>

          </div>
        </slot>
      </lx-header>
    </div>

    <xy-table
      :list="list"
      :total="total"
      @pageSizeChange="e => select.pageSize = e"
      @pageIndexChange="pageChange"
      :table-item="table"
      @delete="deleteActivity"
      @editor="editorActivity"></xy-table>

    <addActivity ref="addActivity" @refresh="getActivities"></addActivity>
    <showActiveOrder :id="activeId" :name="activeName" :is-show.sync="isShowOrder"></showActiveOrder>
  </div>
</template>

<script>
import {index,destroy} from "@/api/activity"

import addActivity from '@/views/activity/component/addActivity'
import showActiveOrder from '@/views/activity/component/showActiveOrder'
import { Message } from 'element-ui'
export default {
  components:{
    addActivity,
    showActiveOrder
  },
  data() {
    return {
      select:{
        pageSize:10,
        pageIndex:1
      },

      total:0,
      list:[],
      isShowOrder:false,
      activeId:null,
      activeName:"",
      table:[
        {
          label:"活动名称",
          width: 220,
          prop:'active_name',
          align:'left',
          fixed:'left',
        },
        {
          label:"活动发布方",
          width: 140,
          prop:'shop.name'
        },
        {
          label:"主办方",
          width: 140,
          prop:'sponsor'
        },
        {
          label:"主办方logo",
          width: 140,
          customFn:(row)=>{
            if(row.sponsor_logo){
              return (<el-image style={{'height':'90px'}} src={row.sponsor_logo}></el-image>)
            }
          }
        },
        {
          label:"主办方宣传语",
          width: 200,
          prop:'sponsor_advertise',
          align:'left'
        },
        {
          label:"活动海报",
          width: 240,
          align:'left',
          customFn:(row)=>{
            if(row.active_playbill){
              return (<el-image style={{'height':'110px'}} src={row.active_playbill}></el-image>)
            }
          }
        },
        {
          label:"活动开始日期",
          width: 240,
          customFn:(row)=>{
            return (<div>{row.start_date} 至 {row.end_date}</div>)
          }
        },
        {
          label:"开始时间",
          width: 240,
          customFn:(row)=>{
            return (<div>{row.start_time} 至 {row.end_time}</div>)
          }
        },
        {
          label:"活动地址",
          minWidth: 260,
          align:'left',
          prop:'address'
        },
        // {
        //   label:"金额",
        //   width: 150,
        //   prop:'money',
        //   align:"right"
        // },
        {
          label:"开放名额",
          width: 140,
          prop:'member_sum',
          formatter:(cell,data,value)=>{
            return value ?? 0
          }
        },
        {
          label:"初始活动名额",
          width: 140,
          prop:'start_member_sum',
          formatter:(cell,data,value)=>{
            return value ?? 0
          }
        },
        {
          label:"剩余名额",
          width: 140,
          customFn:(row)=>{
            return (<div>{row?.member_sum - row?.start_member_sum - row?.total}</div>)
          }
        },
        {
          label:"报名次数",
          width: 140,
          prop:'total',
          customFn: (cell) => {
            return ( < div style = {
                {
                  cursor: 'pointer',
                  color: "blue"
                }
              }
              on = {
                {
                  ['click']: (e) => {
                    if(cell.total<1){
                      return
                    }
                    this.activeId = cell.id;
                    this.activeName = cell.active_name
                    this.isShowOrder = true;
                  }
                }
              }

              >
              {
                cell.total?cell.total:0
              } </div> )
            }


          // formatter:(cell,data,value)=>{
          //   return value ?? 0
          // }
        },
        {
          label:"状态",
          prop:'type_name'
        }
      ]
    }
  },
  methods: {
    async getActivities(){
      const res = await index({
        rows:this.select.pageSize,
        page:this.select.pageIndex
      })
      this.list = res.data
      this.total = res.total
    },

    pageChange(e){
      this.select.pageIndex = e
    },

    deleteActivity(row){
      destroy({id:row.id}).then(res => {
        Message({
          type:'success',
          message:'删除活动成功'
        })
        this.getActivities()
      })
    },
    editorActivity(row){
      this.$refs['addActivity'].id = row.id
      this.$refs['addActivity'].type = 'editor'
      this.$refs['addActivity'].isShow = true
    }
  },
  mounted() {
    this.getActivities()
  }

}
</script>

<style scoped lang="scss">
</style>
