<style scoped lang="scss">
::v-deep a {
  color: #409eff;
  display: block;
}
</style>
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >

      <el-form-item label="选择公司" prop="companyId" v-if="this.$store.getters.roles[0] == 'admin'">
        <el-select
          filterable
          v-model="companyId"
          placeholder="选择公司"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in companyList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="发货日期">
        <el-date-picker
          v-model="uploadDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="downLoadLoading"
        >导出
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="销售" align="center" prop="collectSampleName">
        <template slot-scope="scope">
          <a @click="toIndexDetail(scope.row)">
            {{ scope.row.userName }}({{ scope.row.userId }})
          </a>
        </template>
      </el-table-column>
      <el-table-column label="商品数" align="center" prop="itemCount">
        <template slot-scope="scope">
          {{ scope.row.itemCount }}
        </template>
      </el-table-column>

      <el-table-column label="订单数" align="center" prop="orderNum">
      </el-table-column>

      <el-table-column label="收入" align="center" prop="totalFee">
        <template slot-scope="scope">
          {{ scope.row.totalFee }}
        </template>
      </el-table-column>

      <el-table-column label="成本" align="center" prop="costPrice">
        <template slot-scope="scope">
          {{ scope.row.costPrice }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {getCompanyList, getSaleReport} from "@/api/industry/item";
import {downFile} from "@/api/bytedance/bytedance";

export default {
  name: "SaleReport",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      companyList: [],
      companyId: null,
      submitUserId: null,
      promotersCountLoading: false,
      promotersCount: null,
      editCountVisible: false,
      downLoadLoading: false,
      dialogTableVisible: false,
      logPage: {
        pageNum: 1,
        pageSize: 10,
      },
      logData: [],
      logTotal: 0,
      logLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分配销售
      openAllocation: false,
      // 选择的销售id
      saleId: undefined,
      // 获取用户id
      userId: undefined,
      roleId: undefined,
      // 销售列表
      saleList: [],
      // 分配销售确定loading
      confirmLoadingSale: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      uploadDate: [new Date(), new Date()],
      bindDate: [],
      //查询参数所用下拉数据
      queryParamsList: {
        personList: [],
      },
    };
  },
  created() {
    this.userId = this.$store.getters.userId;
    this.roleId = this.$store.getters.roles[0];
    let date = new Date();
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? "0" + MM : MM;
    let dd = date.getDate();
    dd = dd < 10 ? "0" + dd : dd;
    var firstDay = new Date(y, MM - 1, 1); // 月份需要减1，因为getMonth()返回的是0-11
    var startDateDay = ('0' + firstDay.getDate()).slice(-2); // 使用slice来确保总是两位数
    this.uploadDate = [`${y}-${MM}-${startDateDay}`, `${y}-${MM}-${dd}`];
    this.bindDate = [];
    this.getCompanyList();
  },
  mounted() {
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {

    /** 导出按钮操作 */
    handleExport() {
      this.downLoadLoading = true;
      this.queryParams.userId = this.userId;
      downFile(
        "/kwaixiaodian/report/exportSaleReport",
        this.addDateRange(
          {
            ...this.queryParams

          },
          this.uploadDate,
          "startDate",
          "endDate"
        )
      ).then((res) => {
        let blob = new Blob([res], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        });
        let downloadElement = document.createElement("a");
        let href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = `销售管理.xlsx`; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
        this.downLoadLoading = false;
      }).catch();
    },

    toIndexDetail(item) {
      this.$router.replace({
        path: "/industry/saleDetail",
        query: {
          id: item.userId,
          startDate: this.uploadDate[0],
          endDate: this.uploadDate[1],
        },
      });
    },

    getCompanyList() {
      getCompanyList().then((res) => {
        this.companyList = res.data.map((item) => {
          return {
            label: item.companyName,
            value: item.companyId,
          };
        });
      });
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId = this.userId
      this.queryParams.companyId = this.companyId
      getSaleReport(
        this.addDateRange(
          this.queryParams,
          this.uploadDate,
          "startDate",
          "endDate"
        )
      ).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.confirmLoading = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.dateFile = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];

      let date = new Date();
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let dd = date.getDate();
      dd = dd < 10 ? "0" + dd : dd;
      var firstDay = new Date(y, MM - 1, 1); // 月份需要减1，因为getMonth()返回的是0-11
      var startDateDay = ('0' + firstDay.getDate()).slice(-2); // 使用slice来确保总是两位数
      this.uploadDate = [`${y}-${MM}-${startDateDay}`, `${y}-${MM}-${dd}`];
      this.bindDate = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.companyId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
  },
};
</script>
