<template>
  <div class="pkg-show">
    <el-form inline>

      <el-form-item label="日期">
        <el-date-picker
          v-model="condition.dataDate"
          type="daterange"
          @change="fillDate"
          unlink-panels
          range-separator="-"
          start-placeholder="起始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="应用选择">
        <el-select
          v-model="condition.pkg"
          placeholder="应用选择"
          @change="_fetchAdvCfgList"
          filterable
          clearable
        >
          <el-option
            v-for="item in pkgList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="剪辑" v-if="ifEditorAdmin">
        <el-select
          v-model="condition.editor"
          placeholder="选择剪辑"
          @change="_fetchAdvCfgList"
          filterable
          clearable
        >
          <el-option
            v-for="item in editorList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="_fetchAdvCfgList()">查询</el-button>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column align="center" label="日期" prop="datadate" width="100px"/>
      <el-table-column label="上新素材量" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.upload_videos? scope.row.upload_videos: 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="在投素材量" prop="cost_videos" />
      <el-table-column label="点击率" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.ctr > 0? (scope.row.ctr * 100).toFixed(2) + '%': 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转化率" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.cvr > 0? (scope.row.cvr * 100).toFixed(2) + '%': 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="消耗" prop="total_cost" />
      <el-table-column label="单素材平均消耗" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.cost_per_video > 0? (scope.row.cost_per_video).toFixed(2): 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="提成" prop="tc" />
      <el-table-column v-if="ifEditorAdmin" align="center" label="ROI" prop="roi">
        <template slot-scope="scope">
          <span>{{ scope.row.roi > 0? (scope.row.roi).toFixed(2): 0 }}</span>
        </template>

      </el-table-column>

    </el-table>

    <!-- 页数跳转 -->
    <!--<div class="changePage" style="padding-bottom: 10px">
      <font style="font-size: 12px; color: gray">选择页数:</font>
      <el-input-number
        size="mini"
        :min="1"
        @change="_fetchAdvCfgList"
        v-model="pageInfo.page"
      ></el-input-number>
      <font style="font-size: 12px; color: gray">每页条数:</font>
      <el-select
        style="width: 66px"
        v-model="pageInfo.size"
        size="mini"
        @change="_fetchAdvCfgList"
      >
        <el-option value="10" />
        <el-option value="20" />
        <el-option value="50" />
        <el-option value="100" />
      </el-select>
    </div>-->
  </div>
</template>

<script>
import { getBusinIncome, getSelectApps } from "@/api/cloud";
import { fetchCommonTable } from "@/api/common";
import { backhaul } from '@/api/report';
import moment  from 'moment'
export default {
  data() {
    return {
      ifEditorAdmin: false,
      editorList: [],
      condition: {
        dataDate: [],
        startDate: '',
        endDate: '',
        pkg: '',
        editor: '',
      },
      form: {
        pkg: '',
        ader_id: '',
        plan_id: '',
        userId: '',
        callAction: 0
      },
      pkgList: [],
      tableData: [],
      pageInfo: {
        page: 1,
        size: 10
      },
      order: 'show_pv DESC',
      callActionList: [
        {
          label: '未上报',
          value: 0
        },
        {
          label: '已上报',
          value: 1
        }
      ]

    }
  },
  created() {
    // this.condition.uid = window.localStorage.getItem('uid')
    this.condition.dataDate[0] = moment().subtract(7, "days").format("YYYY-MM-DD");
    this.condition.dataDate[1] = moment().format("YYYY-MM-DD");

    if (window.localStorage.getItem('roles').indexOf('editor_manager') > 0) {
      this.ifEditorAdmin = true
      this._fetchEditors()
    }

    this.fillDate()
    this._fetchAppList();
    this._fetchAdvCfgList();

  },
  methods: {
    fillDate(){
      this.condition.startDate = this.condition.dataDate[0]
      this.condition.endDate = this.condition.dataDate[1]
    },
    _fetchEditors() {

      fetchCommonTable({sqlId: 1386}).then(resp => {
        this.editorList = resp.result.data
      })


    },

    // 网络请求 获取app列表
    _fetchAppList() {
      const params = {
        platformId: 5,
        menuCode: "game.Overview,android",
      };
      getSelectApps(params).then((response) => {
        this.pkgList = response.result.data;
        if (this.ifEditorAdmin) {
          this.pkgList.splice(0, 0, {label: '全部', value: ''})
        }
        /*if (response.result.data[0].value == "") {
          response.result.data.splice(0, 1);
        }*/

      });
    },

    // 网络请求 获取表格数据
    _fetchAdvCfgList() {
      const params = {
        sqlId: 1383,
        ...this.condition,
      };
      delete params.dataDate
      fetchCommonTable(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    // 虚拟回传
    virtualBackhaul(row) {
      let params = {
        id: row.id,
        action: row.actionCode
      };
      backhaul(params).then((res)=>{
        console.log(res)
        let msg = res.result.data.msg;
        this.$message.success(msg);
        this._fetchAdvCfgList();
      })
    },
    toFixed(row) {
      var newData = row.arpu.toFixed(2);
      return newData;
    },
    // 控制视频数排序
    renderHeader(h) {
        return(
            <div style="display:flex;justify-content:left;align-items:center;">
             <span>视频数</span>
              <div style="display:flex;justify-content:center;align-items:center;flex-wrap:wrap;">
                <i class="el-icon-caret-top" style="color:#ccc; padding:0;width:100%"  onClick={()=>this.handleup()}></i><br/>
                <i class="el-icon-caret-bottom" style="color:#ccc; padding:0;width:100%;margin-top:-10px" onClick={()=>this.handledown()}></i>
              </div>
            </div>
        )
    },
　　handleup() {
      // this.tableData = [];
      this.order = "show_pv ASC";
      this.pageInfo.page = 1;
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "show_pv ASC" ,
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    handledown() {
      // this.tableData = [];
      this.pageInfo.page = 1;
      this.order = "show_pv DESC";
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "show_pv DESC",
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    // 控制ecpm排序
    renderecpm(h) {
        return(
            <div style="display:flex;justify-content:left;align-items:center">
              <span>预估ecpm</span>
              <div style="display:flex;justify-content:center;align-items:center;flex-wrap:wrap;">
                <i class="el-icon-caret-top" style="color:#ccc; padding:0;width:100%"  onClick={()=>this.handleecpmUp()}></i><br/>
                <i class="el-icon-caret-bottom" style="color:#ccc; padding:0;width:100%;margin-top:-10px" onClick={()=>this.handleecpmDown()}></i>
              </div>
            </div>
        )
    },
　　handleecpmUp() {
      // this.tableData = [];
      this.order = "ecpm ASC";
      this.pageInfo.page = 1;
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "ecpm ASC" ,
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    handleecpmDown() {
      // this.tableData = [];
      this.order = "ecpm DESC";
      this.pageInfo.page = 1;
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "ecpm DESC",
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    // 控制arpu排序
    renderarpu(h) {
        return(
            <div style="display:flex;justify-content:left;align-items:center;">
             <span>arpu</span>
              <div style="display:flex;justify-content:center;align-items:center;flex-wrap:wrap;">
                <i class="el-icon-caret-top" style="color:#ccc; padding:0;width:100%"  onClick={()=>this.handlearpuUp()}></i><br/>
                <i class="el-icon-caret-bottom" style="color:#ccc; padding:0;width:100%;margin-top:-10px" onClick={()=>this.handlearpuDown()}></i>
              </div>
            </div>
        )
    },
　　handlearpuUp() {
      // this.tableData = [];
      this.order = "arpu ASC";
      this.pageInfo.page = 1;
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "arpu ASC" ,
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
    handlearpuDown() {
      // this.tableData = [];
      this.order = "arpu DESC";
      this.pageInfo.page = 1;
      let params = {
        sqlId: 1348,
        pkg: this.form.pkg,
        aderId: this.form.ader_id,
        planId: this.form.plan_id,
        OrderBy: "arpu DESC",
        ...this.pageInfo
      };
      getBusinIncome(params).then((res)=>{
        this.tableData = res.result.data;
      })
    },
  }
}
</script>

<style scoped>
  .pkg-show {
    padding: 25px;
  }
  .changePage {
    margin-top: 20px;
  }
</style>
