<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="下单日期" prop="payTime">
        <el-date-picker
          v-model="queryParams.payTime"
          type="date"
          size="small"
          style="width: 205.4px"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="公司名" prop="companyId">
        <el-select filterable  v-model="queryParams.companyId" placeholder="请选择公司名"  clearable size="small">
          <el-option
            v-for="item in companys"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="是否共享客户" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择是否共享客户" clearable size="small">
              <el-option
                v-for="dict in yseOrNoOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>-->
      <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="queryParams.orderStatus" placeholder="订单请选择状态" clearable size="small">
              <el-option
                v-for="dict in orderStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:report:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--<el-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="日报表" name="1"></el-tab-pane>
      <el-tab-pane label="总报表" name="2"></el-tab-pane>
    </el-tabs>-->
    <el-table height="500" border v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column label="成单金额" align="center" prop="money" />
      <el-table-column label="支付时间" align="center" prop="payTime" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in orderStatusOptions"   v-if="scope.row.orderStatus==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="userName" />
       <el-table-column label="是否共享客户" align="center" prop="status" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in yseOrNoOptions"  :type="scope.row.status==1?'success':'danger'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="共享销售" align="center" prop="companyUserNames" />
      <el-table-column label="分成比例(百分比)" align="center" prop="proportion" />
      <el-table-column label="我的业绩" align="center" prop="myPerformance" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {getReport,exportReport} from "@/api/statistics/report"
import { getCompanyList } from "@/api/company/company";
export default {
  name: "report",
  data() {
    return {
      companys:[],
      activeName:"1",
      payTypeOptions:[],
      statusOptions:[],
      orderStatusOptions:[],
      yseOrNoOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      // 充值表格数据
      companyRechargeList: [],
      reportList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        money: null,
        rechargeNo: null,
        payTime: null,
        status: null,
        orderStatus:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      recharge:{
        open:false,
        title:"后台充值"
      },
      // 表单校验
      rechargeRules: {
        money: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
        imgs: [
          { required: true, message: "凭证不能为空", trigger: "change" }
        ],
      },
      // 表单参数
      rechargeForm: {
        money: 0,
      },
    };
  },
  created() {
    getCompanyList().then(response => {
      this.companys = response.data;
      if(this.companys!=null&&this.companys.length>0){
        this.companyId=this.companys[0].companyId;
        //this.getTreeselect();
      }
    });
    this.getDicts("company_pay_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("common_order_status").then((response) => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("sys_company_or").then((response) => {
      this.yseOrNoOptions = response.data;
    });
    const today = new Date();
    const y = today.getFullYear();
    const m = String(today.getMonth() + 1).padStart(2, '0');
    const d = String(today.getDate()).padStart(2, '0');
    this.queryParams.payTime = `${y}-${m}-${d}`;
    this.getList();
  },
  methods: {
    /** 查询充值列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      getReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClick(tab, event) {
      if (tab.name === "2") { // 总报表
        this.queryParams.payTime = null;
      } else if (tab.name === "1") { // 日报表
        const today = new Date();
        const y = today.getFullYear();
        const m = String(today.getMonth() + 1).padStart(2, '0');
        const d = String(today.getDate()).padStart(2, '0');
        this.queryParams.payTime = `${y}-${m}-${d}`;
      }
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeId: null,
        companyId: null,
        money: null,
        rechargeNo: null,
        createTime: null,
        payTime: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rechargeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所选日报数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
  }
};
</script>
