<template>
  <ai-detail isHasSidebar class="AppMarketingActivity-detail">
    <template slot="title">
      <ai-title title="活动详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <AiSidebar :tabTitle="tabList" v-model="currIndex"></AiSidebar>
      <div v-show="currIndex === 0">
        <ai-card title="基本信息">
          <template #content>
            <ai-wrapper
              label-width="180px">
              <ai-info-item label="活动标题" isLine :value="info.title"></ai-info-item>
              <ai-info-item label="开始时间" :value="info.beginTime"></ai-info-item>
              <ai-info-item label="结束时间" :value="info.endTime"></ai-info-item>
              <ai-info-item label="第一轮抽奖限制日活天数" :value="info.round1ActiveLimit"></ai-info-item>
              <ai-info-item label="第二轮抽奖限制日活天数" :value="info.round2ActiveLimit"></ai-info-item>
              <ai-info-item label="第三轮抽奖限制日活天数" :value="info.round3ActiveLimit"></ai-info-item>
              <ai-info-item label="第四轮抽奖限制日活天数" :value="info.round4ActiveLimit"></ai-info-item>
              <ai-info-item label="第五轮抽奖开始时间" :value="info.beginTime5"></ai-info-item>
              <ai-info-item label="第五轮抽奖限制日活天数" :value="info.round5ActiveLimit"></ai-info-item>
              <ai-info-item label="微信插件二维码">
                <ai-uploader
                  :instance="instance"
                  disabled
                  :value="[{
                    url: info.wxQrcode
                  }]"
                  :limit="1">
                </ai-uploader>
              </ai-info-item>
            </ai-wrapper>
          </template>
        </ai-card>
        <ai-card title="奖品">
          <template #content>
            <ai-wrapper
              class="prize-item"
              v-for="(item, key) in info.prizes"
              :key="key"
              label-width="180px">
              <ai-info-item label="奖品名称" isLine :value="item.name"></ai-info-item>
              <ai-info-item label="中奖规则" :value="item.rule"></ai-info-item>
              <ai-info-item label="奖品总数" :value="item.total"></ai-info-item>
              <ai-info-item label="奖品排序" :value="item.showIndex"></ai-info-item>
              <ai-info-item label="开放轮次" :value="item.belongRound"></ai-info-item>
              <ai-info-item label="是否默认" :value="item.defaultPrize === '0' ? '否' : '是'"></ai-info-item>
              <ai-info-item label="奖品图片" isLine>
                <ai-uploader
                  :instance="instance"
                  disabled
                  :value="[{
                    url: item.picture
                  }]"
                  :limit="1">
                </ai-uploader>
              </ai-info-item>
            </ai-wrapper>
          </template>
        </ai-card>
      </div>
      <div v-show="currIndex === 1">
        <ai-card title="报名记录">
          <template #content>
            <ai-search-bar class="search-bar">
              <template #left>
                <ai-select
                  v-model="search.activeStatus"
                  clearable
                  placeholder="请选择活跃情况"
                  :selectList="statusList"
                  @change="search.current = 1, getList()">
                </ai-select>
                <ai-download
                  :instance="instance"
                  url="/app/appmarketingactivityinfo/exportSignUpList"
                  :params="{
                    ...search,
                    activityId: params.id
                  }"
                  fileName="报名记录"
                  :disabled="!tableData.length">
                </ai-download>
              </template>
              <template #right>
                <el-input
                  v-model="search.name"
                  class="search-input"
                  size="small"
                  v-throttle="() => {search.current = 1, getList()}"
                  placeholder="请输入姓名"
                  clearable
                  @clear="search.current = 1, getList()"
                  suffix-icon="iconfont iconSearch">
                </el-input>
              </template>
            </ai-search-bar>
            <ai-table
              :tableData="tableData"
              :col-configs="colConfigs"
              :total="total"
              style="margin-top: 6px;"
              :current.sync="search.current"
              :size.sync="search.size"
              @getList="getList">
              <el-table-column slot="options" width="110px" fixed="right" label="操作" align="center">
                <template slot-scope="{ row }">
                  <div class="table-options">
                    <el-button type="text" @click="changePhone(row.id, row.phone)">更换手机号</el-button>
                  </div>
                </template>
              </el-table-column>
            </ai-table>
          </template>
        </ai-card>
      </div>
      <div v-show="currIndex === 2">
        <ai-card title="抽奖记录">
          <template #content>
            <ai-search-bar class="search-bar">
              <template #left>
                <ai-select
                  v-model="prizeSearch.prizeId"
                  clearable
                  placeholder="请选择奖品"
                  :selectList="prizes"
                  @change="prizeSearch.current = 1, getPrizeList()">
                </ai-select>
                <ai-download
                  :instance="instance"
                  url="/app/appmarketingactivityinfo/exportTaffleList"
                  :params="{
                    ...prizeSearch,
                    activityId: params.id
                  }"
                  fileName="抽奖记录"
                  :disabled="!tableData.length">
                </ai-download>
              </template>
              <template #right>
                <el-input
                  v-model="prizeSearch.name"
                  class="search-input"
                  size="small"
                  v-throttle="() => {prizeSearch.current = 1, getPrizeList()}"
                  placeholder="请输入奖品名称"
                  clearable
                  @clear="prizeSearch.current = 1, getPrizeList()"
                  suffix-icon="iconfont iconSearch">
                </el-input>
              </template>
            </ai-search-bar>
            <ai-table
              :tableData="prizeList"
              :col-configs="prizeColConfigs"
              :total="prizeSearch.total"
              style="margin-top: 6px;"
              :current.sync="prizeSearch.current"
              :size.sync="prizeSearch.size"
              @getList="getPrizeList">
            </ai-table>
          </template>
        </ai-card>
      </div>
      <ai-dialog
        :visible.sync="isShowPhone"
        width="790px"
        @onConfirm="onConfirm"
        title="更换手机号">
        <el-form class="ai-form" :model="form" label-width="180px!important" ref="form">
          <el-form-item prop="phone" style="width: 100%;" label="标题" :rules="[{required: true, message: '请输入手机号', trigger: 'change'}]">
            <el-input size="small" placeholder="请输入手机号" v-model="form.phone"></el-input>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        currIndex: 0,
        form: {
          phone: ''
        },
        id: '',
        tabList: ['基本信息', '报名记录', '抽奖记录'],
        colConfigs: [
          { prop: 'name',  label: '姓名', align: 'left', width: '200px' },
          { prop: 'phone', label: '手机号', align: 'center' },
          { prop: 'departmentName', label: '所属部门', align: 'center' },
          { prop: 'activeCount', label: '活跃天数', align: 'center' },
          { prop: 'createTime', label: '报名时间', align: 'center' }
        ],
        prizeColConfigs: [
          { prop: 'name',  label: '姓名', align: 'left', width: '200px' },
          { prop: 'phone', label: '手机号', align: 'center' },
          { prop: 'prizeName', label: '奖品名称', align: 'center' },
          { prop: 'createTime', label: '抽奖时间', align: 'center' }
        ],
        prizeList: [],
        prizeSearch: {
          size: 10,
          current: 1,
          name: '',
          prizeId: ''
        },
        search: {
          size: 10,
          name: '',
          activeStatus: '',
          current: 1
        },
        prizes: [],
        tableData: [],
        total: 0,
        info: {},
        isShowPhone: false,
        statusList: [
          {
            dictName: '无活跃',
            dictValue: '0'
          },
          {
            dictName: '有活跃',
            dictValue: '1'
          }
        ]
      }
    },

    created () {
      this.getInfo()
      this.getList()
      this.getPrizeList()
    },

    methods: {
      getList() {
        this.instance.post(`/app/appmarketingactivityinfo/signUpList?activityId=${this.params.id}`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      changePhone (id, phone) {
        this.id = id
        this.form.phone = phone

        this.isShowPhone = true
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appmarketingactivityinfo/updateBindPhone`, null, {
              params: {
                ...this.form,
                signupId: this.id
              }
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('更换成功')
                this.isShowPhone = false
                this.getList()
              }
            })
          }
        })
      },

      getPrizeList() {
        this.instance.post(`/app/appmarketingactivityinfo/raffleList?activityId=${this.params.id}`, null, {
          params: {
            ...this.prizeSearch
          }
        }).then(res => {
          if (res.code == 0) {
            this.prizeList = res.data.records
            this.prizeSearch.total = res.data.total
          }
        })
      },

      getInfo () {
        this.instance.post(`/app/appmarketingactivityinfo/queryDetailById?id=${this.params.id}`).then(res => {
          if (res.code == 0) {
            if (res.data) {
              this.info = res.data
              this.prizes = res.data.prizes.map(v => {
                return {
                  dictValue: v.id,
                  dictName: v.name
                }
              })
            }
          }
        })
      },

      cancel () {
        this.$emit('change', {
          type: 'List',
          isRefresh: true
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .AppMarketingActivity-detail {
    .prize-item {
      padding: 20px 0 0;
      border-bottom: 1px solid #eee;

      &:first-child {
        padding-top: 0;
      }
    }
  }
</style>
