<template>
  <div class="app-container goods-add">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-divider content-position="left">基础信息</el-divider>
        <el-form ref="addItem"  :model="addData" label-width="120px">
          <el-row :gutter="10">
            <el-col  :xs="{span: 22, offset: 0}" :sm=" {span: 18, offset: 0}" :lg="{span: 10, offset: 0}">
              <el-form-item label="活动名称:" prop="title">
                <el-input v-model="addData.title" placeholder="输入活动名称" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="活动封面:" >
                <el-image
                  style="width: 110px;"
                  :src="addData && addData.picUrl"
                  :preview-src-list="[addData && addData.picUrl]"
                />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="活动日期:" prop="dateValue">
                <el-date-picker value-format="timestamp" v-model="addData.dateValue" type="datetimerange" readonly
                                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
              </el-form-item>
            </el-col>
            <el-col :xs="{span: 22, offset: 0}" :sm=" {span: 18, offset: 0}" :lg="{span: 10, offset: 0}">
              <el-form-item label="初始用户数:">
                <el-input v-model="addData.fakeNum" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="是否可无条件参与:" label-width="140px">
                <div v-if="addData.codeType == 1">是</div>
                <div v-else>否</div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">奖品设置</el-divider>
          <el-row :gutter="10">
            <el-col :span="22">

              <el-form-item>
                <!-- 奖级列表 -->
                <div class="prize" v-for="(item, index) in awardsList" :key="index">
                  <div class="prize-top">
                    <div>奖级：{{ item.name }}</div>
                    <div>名额：{{item.quantity}}</div>
                    <div>内部名额：{{item.insideNum}}</div>
                  </div>
                  <div class="prize-table">
                    <el-table :data="item.prizeList" class="el-table">
                      <el-table-column label="奖品图片" align="center">
                        <template slot-scope="scope">
                          <el-image
                            style="width: 70px; height: 70px"
                            :src="scope.row.picUrl"
                            :preview-src-list="[scope.row.picUrl]"
                          >
                          </el-image>
                        </template>
                      </el-table-column>
                      <el-table-column label="奖品名称" prop="title" align="center" >
                        <template slot-scope="scope">
                          <div v-if="scope.row.prizeType == 'coin'">盲豆 x{{scope.row.value}}</div>
                          <div v-else>{{scope.row.title}}</div>
                        </template>
                      </el-table-column>
                      <el-table-column label="奖品类型" align="center">
                        <template slot-scope="scope">
                          <div v-if="scope.row.prizeType == 'goods'">商品</div>
                          <div v-if="scope.row.prizeType == 'coupon'">券</div>
                          <div v-if="scope.row.prizeType == 'coupon_pkg'">券包</div>
                          <div v-if="scope.row.prizeType == 'coin'">盲豆</div>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">活动详情</el-divider>
          <el-row>
            <el-col :span="22">
              <el-form-item label="" prop="description">
                <div v-html="addData.description"></div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <el-row>
          <el-col :span="24" style="text-align: center">
            <el-button type="info" @click="$router.replace('/marketing/marketing')">取消</el-button>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { publicFileGetUrl } from "@/api/common";
import {getMarketingDetail, addMarketing} from '@/api/business/marketing'
import Upload from '@/components/DragImageUpload'
import WangEditor from '@/components/WangEditor'

export default {
  name: "add",
  components: {
    Upload,
    WangEditor,
  },
  data() {
    return {
      id: this.$route.query.id,
      addData: {
        picUrl: '',//活动主图
        startTime: '',//开始时间
        endTime: '',//结束时间
        fakeNum: 0,//初始用户数
        description: '',//活动详情
        awardsList: [],//奖级列表
        codeType: '',
      },
      labelRadio: 1,//单选框

      // 奖级列表
      awardsList: [],
      dateValue: [],//开始与结束日期
    }
  },
  computed: {
  },
  created() {
    if (this.id) {
      getMarketingDetail(this.id).then(res => {
        const { startTime, endTime, description, fakeNum,picUrl, codeType, realNum, title, awardsList, id } = res.data
        this.addData = { title, dateValue:[startTime,endTime], picUrl: publicFileGetUrl + picUrl, fakeNum, codeType, realNum, description, awardsList, id }
        if(awardsList){
          awardsList.forEach((item) => {
            item.prizeList.forEach((ele) => {
              (ele.picUrl = publicFileGetUrl + ele.picUrl.split(',')[0]),
                (ele.prizeType = JSON.parse(ele.prizeType).value);
            });
          });
          this.awardsList = awardsList
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.goods-add {
  .tip {
    font-size: 12px;
    color: #999;
  }

  .el-divider {
    .is-left {
      color: #409EFF;
      font-size: 20px;
      font-weight: bolder;
      left: 10px;
    }
  }
}
.prize {
  width: 100%;
  margin-bottom: 50px;
  background: #f9f9f9;
  border: 1px solid #bbbbbb;
  font-size: 14px;
  &-top {
    padding: 10px 20px;
    margin-bottom: 10px;
    display: flex;
    align-content: center;
    justify-content: space-around;
    border-bottom: 1px solid #bbbbbb;
    div {
      line-height: 36px;
    }
  }
  &-btn {
    border-top: 1px solid #bbbbbb;
    padding: 10px;
  }
}
</style>
