<template>
  <div class="channel-trans-list">
    <el-form ref="searchFrom" :model="searchFrom" label-width="100px" inline>
      <el-form-item label="进量日期">
        <el-date-picker
          v-model="searchFrom.payTime"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="{shortcuts:[today,yesterday,last7Day,last30Day]}"
          :default-time="['00:00:00','23:59:59']"
          @change="getChannelTransList"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="渠道code">
        <el-input v-model="searchFrom.invite_type" style="width: 110px"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
          <el-select v-model="searchFrom.goods_id" filterable placeholder="请选择" style="width: 150px" @change="getChannelTransList" clearable>
            <el-option
              v-for="(data,index) in goodList"
              :key="index"
              :label="data | filterGoods"
              :value="data.id">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item>
        <div class="flexRow">
          <el-button type="primary" plain @click="seachData">搜索</el-button>
        </div>
      </el-form-item>
    </el-form>
    <el-table
      border
      :span-method="objectSpanMethod"
      :data="list"
      :style="{width: width+'px'}"
      @sort-change="sortMethod"
      fixed
    >
    <el-table-column prop="is_other" label="渠道来源">
      <template slot-scope="scope">
        {{scope.row.is_other==0?"内部":"外部"}}
      </template>
    </el-table-column>
    <el-table-column prop="invite_type_name" label="渠道类别"></el-table-column>
      <el-table-column prop="invite_type" label="渠道类型"></el-table-column>
      <el-table-column prop="sum_total_num" label="订单数" sortable='custom'></el-table-column>
      <el-table-column prop="sum_class_num" label="到班数" sortable='custom'></el-table-column>
      <el-table-column prop="class_num_except_total_num" label="到班率" >
        <template slot-scope="scope">
          {{scope.row.class_num_except_total_num}}%
        </template>
      </el-table-column>
      <el-table-column prop="sum_friend_num" label="好友数" sortable='custom'></el-table-column>
      <el-table-column prop="friend_num_except_total_num" label="好友率" >
        <template slot-scope="scope">
          {{scope.row.friend_num_except_total_num}}%
        </template>
      </el-table-column>
      <el-table-column prop="sum_come_num" label="到课数" sortable='custom'></el-table-column>
      <el-table-column prop="come_num_except_total_num" label="到课率" >
        <template slot-scope="scope">
          {{scope.row.come_num_except_total_num}}%
        </template>
      </el-table-column>
      <el-table-column prop="sum_buy_num" label="转化人数" sortable='custom'></el-table-column>
      <el-table-column prop="sum_buy_num_except_total_num" label="转化率" >
        <template slot-scope="scope">
          {{scope.row.sum_buy_num_except_total_num}}%
        </template>
      </el-table-column>
      <el-table-column prop="sum_one_buy_num" label="一年课购买人数" sortable='custom'></el-table-column>
      <el-table-column prop="sum_two_buy_num" label="两年课购买人数" sortable='custom'></el-table-column>
      <el-table-column prop="sum_one_buy_money" label="一年课转化金额" sortable='custom'></el-table-column>
      <el-table-column prop="sum_two_buy_money" label="两年课转化金额" sortable='custom'></el-table-column>
    </el-table>
    <page
      :nowPage="nowPage"
      :total="total"
      :limit="limit"
      @pageChange="onPageChange"
      @sizeChange="onSizeChange"
    />
  </div>
</template>
<script>
import {
    getChannelConversionListApi,
  getGoodsListApi
} from "../../service/api";
import page from "../framework/page";
import { GOODSTYPE, CLASSSOURCE } from "../../util/wordbook";
export default {
  name: "channelConversionList",
  data() {
    return {
      width: 0,
      nowPage: 1,
      total: 0,
      limit: 20,
      list: [],
      goods_id: null,
      teacherList: [],
      goodsList: [],
      periodList: [],
      total: 0,
      today: {
        text: "今天",
        onClick: () => {
          this.searchFrom.payTime = [
            this.formatTime(new Date()) + " 00:00:00",
            this.formatTime(new Date()) + " 23:59:59"
          ];
        }
      },
      yesterday: {
        text: "昨天",
        onClick: () => {
          let preDate = this.formatTime(
            new Date(new Date().getTime() - 24 * 60 * 60 * 1000)
          );
          this.searchFrom.payTime = [
            preDate + " 00:00:00",
            preDate + " 23:59:59"
          ];
        }
      },
      last30Day: {
        text: "过去30天",
        onClick: () => {
          let preDate = this.formatTime(
            new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000)
          );
          this.searchFrom.payTime = [
            preDate + " 00:00:00",
            this.formatTime(new Date()) + " 23:59:59"
          ];
        }
      },
      last7Day: {
        text: "过去7天",
        onClick: () => {
          let preDate = this.formatTime(
            new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)
          );
          this.searchFrom.payTime = [
            preDate + " 00:00:00",
            this.formatTime(new Date()) + " 23:59:59"
          ];
        }
      },
      propertyList: [],
      spanArr: [],
      contentSpanArr: [],
      searchFrom: {
        payTime: [],
        start_at: "",
        end_at: "",
        invite_type: "",
        invite_name: "",
        watch_num: "",
        periods_id: "",
        squad: "",
        teacher_id: "",
        periods_title: ""
      },
      propertyList: [],
      watchList: [
        { id: 0, title: 5 },
        { id: 1, title: 6 },
        { id: 2, title: 10 },
        { id: 3, title: 20 }
      ],
      goodList:[]
    };
  },
  filters: {
      filterGoods(val){
        // console.log(val,229)
        return '['+val.id+']['  + GOODSTYPE[val.goods_type] + ']' + '【' +val.current_price / 100 + '元】' + val.name
      },
    },
  components: { page },
  methods: {
    sortMethod(data){
      this.searchFrom.sort_key = data.prop
      if(data.order=='ascending'){
        this.searchFrom.sort_value = 'asc'
      }else{
        this.searchFrom.sort_value = 'dasc'
      }
      this.getChannelTransList()
    },
    getGoodsOption(){
        let json = {
          page: 1,
          limit: 100,
          goods_type:'1,2'
        };
        getGoodsListApi(json).then(res=>{
          this.goodList = res.list;
          console.log(this.goodList,176)
        })
    },
    seachData() {
      this.total = 0;
      this.nowPage = 1;
      this.getChannelTransList();
    },
    onPeriodChange(value) {
      let str = "";
      if (value.length > 1) {
        str = value.join(",");
      } else {
        str = value[0];
      }
      this.periods_id = str;
      console.log(str);
    },

    objectSpanMethod(data) {
      // if (this.propertyList.indexOf(data.column.property) > -1) {
      //   if (
      //     data.rowIndex === 0 ||
      //     data.row.cur_date !== this.list[data.rowIndex - 1].cur_date
      //   ) {
      //     let rowspan = 1;
      //     for (let i = data.rowIndex + 1; i < this.list.length; i++) {
      //       if (data.row.cur_date === this.list[i].cur_date) {
      //         rowspan++;
      //       } else {
      //         break;
      //       }
      //     }
      //     return {
      //       rowspan: rowspan,
      //       colspan: 1
      //     };
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0
      //     };
      //   }
      // } else {
      //   return {
      //     rowspan: 1,
      //     colspan: 1
      //   };
      // }
      // if (data.columnIndex === 0) {
      //   //用于设置要合并的列
      //   const _row = this.spanArr[data.rowIndex];
      //   const _col = _row > 0 ? 1 : 0;
      //   return {
      //     rowspan: _row, //合并的行数
      //     colspan: _col //合并的列数
      //   };
      // } else {
      //   return false;
      // }
    },
    handleItemChange(val) {
      getPeriodsApi({ goods_id: val[0], limit: 100 }).then(res => {
        res.list.forEach(i => {
          i.name = i.title;
        });
        this.goodsList.find(i => {
          return i.id === val[0];
        }).children = res.list;
      });
    },
    changePeriods(data) {
      if (data.length > 1) {
        this.goods_id = data[0];
        let nowGoods = this.goodsList.find(i => {
          return i.id === data[0];
        });
        this.periods = nowGoods.children.find(i => {
          return i.id === data[1];
        });
        this.searchFrom.periods_id = this.periods.id;
        this.getChannelTransList();
      }
    },
    changeTeacher(value) {
      this.searchFrom.teacher_id = value;
      this.getChannelTransList();
    },
    selectChange(value) {
      this.searchFrom.watch_num = this.watchList[value].title;
      this.getChannelTransList();
    },
    formatTime(date) {
      let year = date.getFullYear();
      let Month = date.getMonth() + 1;
      if (Month < 10) {
        Month = `0${Month}`;
      }
      let Day = date.getDate();
      if (Day < 10) Day = `0${Day}`;
      return `${year}-${Month}-${Day}`;
    },
    onPageChange(val) {
      this.nowPage = val;
      this.getChannelTransList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getChannelTransList();
    },
    getPeriodsOtherList() {
      let json = { max_watch_num: 20 };
      getPeriodsOtherListApi(json).then(res => {
        this.periodList = res.list;
      });
    },
    getChannelTransList() {
      let json = {
        limit: this.limit,
        page: this.nowPage
      };
      // 搜索筛选
      if (this.searchFrom.invite_type) {
        json.invite_type = this.searchFrom.invite_type;
      }
      if (this.searchFrom.goods_id) {
        json.goods_id = this.searchFrom.goods_id;
      }
      if(this.searchFrom.sort_value){
        json.sort_value = this.searchFrom.sort_value;
        json.sort_key = this.searchFrom.sort_key;
      }
      if (this.searchFrom.payTime&&this.searchFrom.payTime.length > 0) {
        if (
          this.searchFrom.payTime[0] &&
          this.searchFrom.payTime[0].length > 0
        ) {
          this.searchFrom.start_at = this.searchFrom.payTime[0];
          json.start_at = this.searchFrom.start_at;
        }
        if (
          this.searchFrom.payTime[1] &&
          this.searchFrom.payTime[1].length > 0
        ) {
          this.searchFrom.end_at = this.searchFrom.payTime[1];
          json.end_at = this.searchFrom.end_at;
        }
      }
        getChannelConversionListApi(json).then(res => {
        if (res) {
          if (res.list && res.list.length > 0) {
            this.total = res.total;
            this.list = res.list.map((item, index) => {
              item.index = index;
              if (index == 0) {
                this.spanArr.push(1);
                this.pos = 0;
              } else {
                if (item.cur_date == res.list[index - 1].cur_date) {
                  this.spanArr[this.pos] += 1;
                  this.spanArr.push(0);
                } else {
                  this.spanArr.push(1);
                  this.pos = index;
                }
              }

              let name =
                item.invite_name && item.invite_name.length > 0
                  ? `(${item.invite_name})`
                  : "";
              let goods_id =
                item.goods_id && item.goods_id > 0
                  ? `【${item.goods_id}】`
                  : "";
              let watch_num =
                item.watch_num && item.watch_num > 0
                  ? `【${item.watch_num}个课时】`
                  : "";
              item.invite_type = `${item.invite_type}${name}`;
              item.goods_name = `${goods_id}${item.goods_name}${watch_num}`;
              return item;
            });
            for (let key in this.list[0]) {
              this.propertyList.push(key);
            }
            this.width = document.documentElement.clientWidth - 200;
          }
          if (res.list && res.list.length === 0) {
            this.list = [];
          }
        } else {
          this.list = [];
        }
      });
    }
  },
  mounted() {
    this.getChannelTransList();
    this.getGoodsOption()
    // this.getGoodsList();
  }
};
</script>
<style lang="less" scope>
.channel-trans-list {
  padding: 20px 0;
}
.el-table {
  .cell {
    text-align: center !important;
  }
}
</style>