<template>
  <div>
    <el-form :model="form" :inline="true" class="demo-form-inline"><br>
      <el-form-item label="拼团名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入要搜索的拼团名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择搜索的活动状态" clearable>
          <el-option value="0" label="热销中"></el-option>
          <el-option value="1" label="已结束"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
      </el-form-item>
      <br>
      <el-button type="primary" @click="addTeam">点击开团</el-button>
    </el-form>

    <el-table :data="pageList.list">
      <el-table-column label="拼团id" align="center" prop="id" />
      <el-table-column label="拼团名称" align="center" prop="name" />
      <el-table-column label="商品活动图" align="center" prop="productImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productImage" :width="100" :height="100"/>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="productPrice" />
      <el-table-column label="拼团价格" align="center" prop="teamPrice" />
      <el-table-column label="拼团人数" align="center" prop="attendNumber" />
      <el-table-column label="开团人数" align="center" prop="openTeamNumber" />
      <el-table-column label="参团人数" align="center" prop="addTeamNumber" />
      <el-table-column label="团购库存" align="center" prop="teamStock" />
      <el-table-column label="剩余库存" align="center" prop="remainStock" />
      <el-table-column label="活动结束时间" align="center" prop="endTime" />
      <el-table-column label="活动状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.pageNum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="form.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageList.total">
    </el-pagination>


    <!--添加弹出框  -->
    <el-dialog title="开团" :visible.sync="dialogFormVisible">
      <!-- 步骤条 -->
      <el-steps :active="form" finish-status="success">
        <el-step title="步骤 1"></el-step>
        <el-step title="步骤 2"></el-step>
        <el-step title="步骤 3"></el-step>
      </el-steps>

      <div v-if="active == 0">
        <el-form :model="addForm">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model="addForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品" :label-width="formLabelWidth">
            <el-button type="primary" @click="addProduct">选择商品</el-button>
          </el-form-item>
          <el-form-item label="活动简介" :label-width="formLabelWidth">
            <el-input v-model="addForm.introduction" autocomplete="off">活动介绍</el-input>
          </el-form-item>
          <el-form-item label="图片" prop="image" :label-width="formLabelWidth">
            <image-upload v-model="addForm.image" :limit="1" :is-show-tip="false"/>
          </el-form-item>
          <el-form-item label="商品单位" :label-width="formLabelWidth">
            <el-input v-model="addForm.unit" autocomplete="off">品牌单位</el-input>
          </el-form-item>
          <el-form-item label="结束时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="addForm.endTime"
              type="datetime"
              placeholder="选择日期时间"
              format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>

      <div v-if="active == 1">
        <el-form :model="addForm">
          <el-form-item label="活动排序" :label-width="formLabelWidth">
            <el-input v-model="addForm.sort" autocomplete="off">活动排序</el-input>
          </el-form-item>
          <el-form-item label="活动详细" :label-width="formLabelWidth">
            <el-input v-model="addForm.content" autocomplete="off">活动详细</el-input>
          </el-form-item>
          <el-form-item label="活动状态" :label-width="formLabelWidth">
            <template>
              <el-radio v-model="addForm.status" label="1">开启</el-radio>
              <el-radio v-model="addForm.status" label="2">关闭</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="最大团购量" :label-width="formLabelWidth">
            <el-input v-model="addForm.maxBuy" autocomplete="off">最大团购量</el-input>
          </el-form-item>
          <el-form-item label="单次团购量" :label-width="formLabelWidth">
            <el-input v-model="addForm.oneBuy" autocomplete="off">单次团购量</el-input>
          </el-form-item>
          <el-form-item label="补充人数" :label-width="formLabelWidth">
            <el-input v-model="addForm.virtualNumber" autocomplete="off">补充人数</el-input>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="active ==2">
        <el-form :model="addForm">
          <el-table
            :data="skuData"
            style="width: 100%"  @selection-change="handSku">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="180" ></el-table-column>
            <el-table-column prop="sku" label="日期" width="180" ></el-table-column>
            <el-table-column prop="stock" label="姓名" width="180"></el-table-column>
            <el-table-column prop="price" label="地址" width="180" ></el-table-column>
          </el-table>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button style="margin-top: 12px;" v-if="active != 0" @click="nesx">上一步</el-button>
        <el-button style="margin-top: 12px;" v-if="active != 2" @click="next">下一步</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSku">确 定</el-button>
      </div>

    </el-dialog>
    <el-dialog title="选择商品" :visible.sync="dialogTableVisible">
      <el-table ref="multipleTable" :data="productList" tooltip-effect="dark" style="width: 120%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="商品id" width="120">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="120"/>
        <el-table-column prop="type" label="商品类型" width="120"></el-table-column>
        <el-table-column label="商品图片" width="120">
          <template slot-scope="scope">
            <image-preview :src="scope.row.image" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="商品描述" width="120">
          <template slot-scope="scope">
            <span style="margin-left: 10px" v-html="scope.row.introduction"></span>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="selectOver">选择完成</el-button>
    </el-dialog>
  </div>
</template>

<script>
import {addTeamProduct, listBuy} from "@/api/actives";
import {cached, getList, productList} from '@/api/product/info';
import {listSku} from "@/api/product/sku";
export default {
  data() {
    return {
      active: 0,
      addForm: {
      },
      pageList: {
        list: [],
        total: 0,
      },
      productList: [],
      form: {
        pageNum: 1,
        pageSize: 2
      },
      skuData:[],
      dialogTableVisible: false,
      formLabelWidth: '160px',
      multipleSelection: [],
      multipleSelectionSku:[],
      dialogFormVisible: false,
    }
  },
  // 表单校验
  created() {
    this.getList()
  },
  methods: {
    nesx() {
      this.active--
    },
    next() {
      this.active++
    },
    addTeam() {
      this.dialogFormVisible = true
      this.getProductData()
    },
    //添加拼团信息规格
    addSku(){
       for (var i=0;i<this.multipleSelectionSku.length;i++){
         this.multipleSelectionSku[i].teamPrice=this.multipleSelectionSku[i].price
         this.multipleSelectionSku[i].teamStock=this.multipleSelectionSku[i].stock
         this.multipleSelectionSku[i].productSku=this.multipleSelectionSku[i].sku
       }
       //获取商品的ID
     this.addForm.productId= this.multipleSelectionSku[0].id
     this.addForm.projectSkuInfoList=this.multipleSelectionSku
     addTeamProduct(this.addForm).then(res=>{
     this.$message.success("添加成功！！！")
       getList()
      })
    },
    /**添加**/
    addProduct() {
      this.dialogTableVisible = true
      this.getProductList()
    },
    /**分页数据**/
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.form.pageSize = val

    },
    getProductData() {
      getList().then(res => {
        this.productList = res.data
      })
    },
    selectOver() {
      this.dialogTableVisible = false
      let id = this.multipleSelection[0].id
      cached(id).then(res => {
        this.addForm = res.data
      })
      listSku(id).then(res => {
        console.log(res.data)
        this.skuData = res.data
      })
    },
    /**分页数据**/
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.form.pageNum = val
    },
    /** 查询商品拼团信息列表 */
    getList() {
      listBuy(this.form).then(response => {
        this.pageList.list = response.data.rows;
        this.pageList.total = response.data.total;
        console.log(this.pageList)
      });
    },
    /**商品sku**/
    handSku(val) {
      this.multipleSelectionSku = val;
    },
    /**搜索**/
    handleQuery(){
      this.getList()
    },
    /**商品列表**/
    getProductList(){
      productList().then(res => {
      this.productList = res.data
      })
    },
    /**选择的商品**/
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>
<style scoped>
</style>
