<template>
  <div id="weixin">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="crumbs">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 微信宣传转发统计 </el-breadcrumb-item>
          </el-breadcrumb>
        </el-col>
        <el-col :span="24" class="container down">
          <el-col :span="24" class="downTop">
            <el-col :span="4">
              <el-select v-model="searchForm.xsid" clearable placeholder="请选择销售员">
                <el-option v-for="item in xsList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
              </el-select>
            </el-col>
            <el-col :span="6">
              <el-date-picker
                v-model="searchForm.date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable
              >
              </el-date-picker>
            </el-col>
            <el-col :span="14">
              <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </el-col>
          </el-col>
          <el-col :span="24" class="downList">
            <el-col :span="24" class="downListOne"> 微信转发总{{ wxtotal }}次 </el-col>
            <el-col :span="24" class="downListTwo">
              <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search">
                <template #custom="{item, row}">
                  <template v-if="item.prop == 'num'">
                    <span>{{ getkhTotal(row) }}</span>
                  </template>
                </template>
              </data-table>
            </el-col>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash';
import dataTable from '@/components/frame/filter-page-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: xs } = createNamespacedHelpers('xs');
const { mapActions: zf } = createNamespacedHelpers('zf');
export default {
  metaInfo: { title: '微信宣传转发统计' },
  name: 'weixin',
  props: {},
  components: {
    dataTable, //列表组件
  },
  data: function() {
    return {
      // 查询
      searchForm: {},
      xsList: [],
      // 客户列表
      opera: [],
      fields: [
        { label: '销售员姓名', prop: 'xsname' },
        { label: '微信转发数', prop: 'times' },
      ],
      list: [],
      total: 0,
      // 微信转发总数
      wxtotal: 0,
    };
  },
  created() {
    this.searchXs();
  },
  methods: {
    ...xs({ xsQuery: 'query' }),
    ...zf({ zfQuery: 'query' }),
    async searchXs() {
      let res = await this.xsQuery({ ssid: this.user.tableid });
      if (this.$checkRes(res)) {
        let data = res.data;
        data.push({ name: '全部' });
        this.$set(this, `xsList`, data);
      }
    },
    async search({ skip = 0, limit = 10, ...info } = {}) {
      info = {
        xsid: this.searchForm.xsid,
        start: _.get(this.searchForm, 'date[0]'),
        end: _.get(this.searchForm, 'date[1]'),
      };
      let res = await this.zfQuery({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        let wxtotal = res.data.reduce((p, n) => p + (n['times'] * 1 || 0), 0);
        if (wxtotal) this.$set(this, `wxtotal`, wxtotal);
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

<style lang="less" scoped>
.down {
  .downList {
    .downListOne {
      padding: 15px 0;
    }
  }
}
</style>
