<template>
  <div class="app-container">
    <div class="app-content">
      <div class="title">
        订单数据汇总
      </div>
        <el-form class="search-form" :inline="true">
         <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="时间:">
              <el-select v-model="value" placeholder="请选择日期">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
             <el-form-item label="公司名:" prop="companyId">
              <el-select filterable v-model="companyId" @change="companyChange" 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-col>
          <el-col :span="6">
            <el-form-item label="部门:">
              <treeselect :clearable="false"  v-model="deptId"  :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="员工:">
              <el-select filterable v-model="userIds" placeholder="请选择员工" clearable size="small">
                <el-option
                  v-for="item in users"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="订单类型:">
              <el-select v-model="orderType" placeholder="请选择订单类型" clearable size="small">
                <el-option
                  v-for="item in orderTypeOptions"
                  :key="item.dictLabel"
                  :label="item.dictLabel"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="媒体来源:">
              <el-select v-model="orderMedium" placeholder="请选择媒体来源" clearable size="small">
                <el-option
                  v-for="item in orderMediumOptions"
                  :key="item.dictLabel"
                  :label="item.dictLabel"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="筛选日期" prop="createTime">
          <el-date-picker clearable size="small" style="width: 205.4px"
            v-model="dateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="storeOrder">搜索</el-button>
        </el-form-item>
      </el-form>

      <!-- 新增的数据表格 -->
      <div class="data-box">
        <el-table :data="tableData" border stripe style="width: 100%; margin-top: 20px;" 
          :summary-method="getSummaries" show-summary>
          <el-table-column prop="province" label="省份" width="120" fixed align="center"></el-table-column>
          
          <el-table-column label="订单总数" width="120" align="center">
            <el-table-column prop="orderCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="orderAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.orderAmount ? scope.row.orderAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column label="取消单" align="center">
            <el-table-column prop="cancelCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="cancelAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.cancelAmount ? scope.row.cancelAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
            <el-table-column prop="cancelCountRatio" label="数量比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.cancelCountRatio ? scope.row.cancelCountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
            <el-table-column prop="cancelAmountRatio" label="金额比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.cancelAmountRatio ? scope.row.cancelAmountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column label="发货单" align="center">
            <el-table-column prop="shippedCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="shippedAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.shippedAmount ? scope.row.shippedAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
            <el-table-column prop="shippedCountRatio" label="数量比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.shippedCountRatio ? scope.row.shippedCountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
            <el-table-column prop="shippedAmountRatio" label="金额比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.shippedAmountRatio ? scope.row.shippedAmountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column label="在途单" align="center">
            <el-table-column prop="transitCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="transitAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.transitAmount ? scope.row.transitAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
            <el-table-column prop="transitCountRatio" label="数量比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.transitCountRatio ? scope.row.transitCountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
            <el-table-column prop="transitAmountRatio" label="金额比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.transitAmountRatio ? scope.row.transitAmountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column label="退单" align="center">
            <el-table-column prop="returnCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="returnAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.returnAmount ? scope.row.returnAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
            <el-table-column prop="returnCountRatio" label="数量比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.returnCountRatio ? scope.row.returnCountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
            <el-table-column prop="returnAmountRatio" label="金额比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.returnAmountRatio ? scope.row.returnAmountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
          </el-table-column>
          
          <el-table-column label="签收单" align="center">
            <el-table-column prop="receivedCount" label="数量" width="80" align="center"></el-table-column>
            <el-table-column prop="receivedAmount" label="金额" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.receivedAmount ? scope.row.receivedAmount.toFixed(2) : '0.00' }}
              </template>
            </el-table-column>
            <el-table-column prop="receivedCountRatio" label="数量比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.receivedCountRatio ? scope.row.receivedCountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
            <el-table-column prop="receivedAmountRatio" label="金额比%" width="100" align="center">
              <template slot-scope="scope">
                {{ scope.row.receivedAmountRatio ? scope.row.receivedAmountRatio.toFixed(2) : '0.00' }}%
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserListByDeptId} from "@/api/company/companyUser";
import { treeselect } from "@/api/company/companyDept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getCompanyList } from "@/api/company/company";
export default {
  name: 'StoreOrderData',
  components: {
    Treeselect
  },
  data() {
    return {
      companys: [],
      deptOptions: [],
      companyId: undefined,
      deptId: undefined,
      userIds: undefined,
      users: [],
      dateRange: [],
      options: [{
        value: '1',
        label: '今天'
      }, {
        value: '2',
        label: '昨天'
      }, {
        value: '3',
        label: '本周'
      }, {
        value: '4',
        label: '上周'
      }, {
        value: '5',
        label: '本月'
      }, {
        value: '6',
        label: '上月'
      }, {
        value: '7',
        label: '本季度'
      }, {
        value: '8',
        label: '上季度'
      }, {
        value: '9',
        label: '本年'
      }, {
        value: '10',
        label: '上年'
      }],
      value: '5',
      orderType: undefined,
      orderMedium: undefined,
      orderTypeOptions: [],
      orderMediumOptions: [],
      list: [],
      dates: [],
      orderCount: [],
      payMoney: [],
      tableData: [] // 新增表格数据
    }
  },
    watch: {
    // 监听deptId
    'deptId': 'currDeptChange'
    },
   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("store_order_type").then((response) => {
      this.orderTypeOptions = response.data;
    });
    // 媒体来源
    this.getDicts("crm_customer_source").then((response) => {
      this.orderMediumOptions = response.data;
    });
  },
  methods: {
    getUserListByDeptId() {
      this.userIds = undefined;
      var data = { deptId: this.deptId };
      getUserListByDeptId(data).then(response => {
        this.users = response.data;
      });
    },
    // 获取表格合计行数据
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      if (data.length === 0) return sums;
      
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          const sum = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          
          sums[index] = sum.toFixed(2);
        } else {
          sums[index] = '';
        }
      });
      
      return sums;
    },
    currDeptChange(val) {
      console.log(val)
      this.deptId = val;
      this.getUserListByDeptId();
    },
    // 模拟获取数据的方法
    storeOrder() {
      // 这里应该调用实际的API获取数据
      // 示例数据格式如下：

      console.log('模拟获取数据：', this.tableData);
      
      this.tableData = [
        {
          province: '北京市',
          orderCount: 100,
          orderAmount: 50000.00,
          cancelCount: 5,
          cancelAmount: 2500.00,
          cancelCountRatio: 5.00,
          cancelAmountRatio: 5.00,
          shippedCount: 80,
          shippedAmount: 40000.00,
          shippedCountRatio: 80.00,
          shippedAmountRatio: 80.00,
          transitCount: 10,
          transitAmount: 5000.00,
          transitCountRatio: 10.00,
          transitAmountRatio: 10.00,
          returnCount: 3,
          returnAmount: 1500.00,
          returnCountRatio: 3.00,
          returnAmountRatio: 3.00,
          receivedCount: 75,
          receivedAmount: 37500.00,
          receivedCountRatio: 75.00,
          receivedAmountRatio: 75.00
        }
      ];
      
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      var that = this;
      var param = { companyId: this.companyId }
      treeselect(param).then((response) => {
        this.deptOptions = response.data;
        console.log(this.deptOptions)
        if (response.data != null && response.data.length > 0) {
          this.deptId = response.data[0].id;
          that.storeOrder()
        }
      });
    },
    companyChange(val) {
      console.log(val);
      this.companyId = val;
      this.getTreeselect();
    },
  }
}
</script>

<style lang="scss" scoped>
.vue-treeselect {
  width: 217px;
  height: 36px;
}

.app-container {
  border: 1px solid #e6e6e6;
  padding: 12px;
  
  .app-content {
    background-color: white;
    
    .title {
      padding: 20px 30px 0px 30px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }
    
    .search-form {
      margin: 20px 30px 0px 30px;
    }
    
    .data-box {
      padding: 30px;
      background-color: rgb(255, 255, 255);
      height: 100%;
      
      .echart-box {
        margin: 0 auto;
        text-align: center;
      }
      
      .el-select {
        margin: 5px 10px;
      }
      
      .table-box {
        margin-top: 15px;
        
        .export {
          float: right;
          margin: 10px 0px;
        }
      }
      
      // 新增表格样式
      .table-container {
        margin-top: 30px;
        
        .table-title {
          font-size: 16px;
          font-weight: bold;
          color: #333;
          margin-bottom: 10px;
        }
      }
    }
  }
}

// 表格相关样式优化
::v-deep .el-table {
  font-size: 12px;
  
  th {
    background-color: #f5f7fa;
  }
  
  td, th {
    padding: 8px 0;
  }
}
</style>