<template>
  <Container>
    <div slot="left" class="box-search-form">
      <el-form inline size="small">
        <el-form-item label="客户:">
          <el-select
            v-model="profitSearch.customerId"
            placeholder="请选择客户"
            @change="customerIdChange"
          >
            <el-option
              v-for="item in customer_options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品:">
          {{ goodsName }}
        </el-form-item>
        <el-form-item label="时间:">
          {{ $_common.formatDate(profitSearch.start, "yyyy-MM-dd") }}-{{
            $_common.formatDate(profitSearch.end, "yyyy-MM-dd")
          }}
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getOrderProfit(1)">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="num-ul">
      <p>
        数量：
        <span class="num-span">{{ totalNum }}</span>
      </p>
      <p>
        销售收入：
        <span class="num-span">
          {{ $_common.formattedNumber(totalSaleMoney) }}
        </span>
      </p>
      <p>
        销售成本：
        <span class="num-span">{{ $_common.formattedNumber(totalCost) }}</span>
      </p>
      <p>
        销售毛利：
        <span class="num-span">
          {{ $_common.formattedNumber(totalProfit) }}
        </span>
      </p>
      <p>
        销售毛利率：
        <span class="num-span">
          {{
            totalSaleMoney > 0
              ? $_common.formatNub(
                  $NP.divide(totalProfit, totalSaleMoney) * 100,
                  4
                )
              : 0
          }}%
        </span>
      </p>
    </div>
    <!-- 表格 -->
    <el-table :data="summary_data">
      <el-table-column
        prop="materielName"
        label="单据类型"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          {{
            scope.row.orderType === 1
              ? "销售订单"
              : scope.row.orderType === 23
              ? "销售单"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="单据日期" min-width="160" align="center">
        <template slot-scope="scope">
          {{ $_common.formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="customerName"
        label="客户名"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyNum"
        label="数量"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formatNub(scope.row.buyNum) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        label="销售价"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="outCostPrice"
        label="成本价"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.outCostPrice) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="totalMoney"
        label="销售收入"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.totalMoney) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="saleCostAmount"
        label="销售成本"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.saleCostAmount) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="saleProfitAmount"
        label="销售毛利"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.saleProfitAmount) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="saleProfitRate"
        label="销售毛利率"
        min-width="120"
        align="center"
      >
        <template slot-scope="scope">{{ scope.row.saleProfitRate }}%</template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </Container>
</template>

<script>
  import { getOrderProfit, exprotsgetOrderProfit } from "@/api/Order";

  export default {
    name: "SaleSummary",
    components: {
      // searchTime
    },
    data() {
      return {
        summary_data: [],
        pageSize: 10,
        page: 1,
        total: 1,
        warehouse_list: [],
        warehouseId: "",
        endTime: "",
        startTime: "",
        shopId: "",
        time_val: [],
        customer_options: [],
        goodsName: "",
        profitSearch: {
          shopId: "", // 必填
          goodsId: "",
          start: "",
          end: "",
          customerId: "", // 选填
        },
      };
    },
    computed: {
      totalNum() {
        let num = 0;
        this.summary_data.forEach((item) => {
          num = this.$NP.plus(num, item.buyNum);
        });
        return num;
      },
      totalSaleMoney() {
        let num = 0;
        this.summary_data.forEach((item) => {
          num = this.$NP.plus(num, item.totalMoney);
        });
        return num;
      },
      totalCost() {
        let num = 0;
        this.summary_data.forEach((item) => {
          num = this.$NP.plus(num, item.saleCostAmount);
        });
        return num;
      },
      totalProfit() {
        let num = 0;
        this.summary_data.forEach((item) => {
          num = this.$NP.plus(num, item.saleProfitAmount);
        });
        return num;
      },
    },
    async created() {
      let profitSearch = sessionStorage.getItem("profitSearch");
      if (profitSearch) {
        profitSearch = JSON.parse(profitSearch);
        this.goodsName = profitSearch.goodsName;
        this.profitSearch = {
          shopId: profitSearch.shopId, // 必填
          goodsId: profitSearch.goodsId,
          start: profitSearch.start,
          end: profitSearch.end,
          customerId: profitSearch.customerId, // 选填
        };
      }
      await this.getOrderProfit();
    },
    async activated() {
      if (this.$_isInit()) return;
      let profitSearch = sessionStorage.getItem("profitSearch");
      if (profitSearch) {
        profitSearch = JSON.parse(profitSearch);
        this.goodsName = profitSearch.goodsName;
        this.profitSearch = {
          shopId: profitSearch.shopId, // 必填
          goodsId: profitSearch.goodsId,
          start: profitSearch.start,
          end: profitSearch.end,
          customerId: profitSearch.customerId, // 选填
        };
      }
      await this.getOrderProfit();
    },
    methods: {
      customerIdChange() {
        this.pageChange(1);
      },
      async getOrderProfit(exports) {
        let params = {
          ...this.profitSearch,
          page: this.page,
          pageSize: this.pageSize,
        };
        if (exports) {
          params.export = 1;
          const target = await exprotsgetOrderProfit({ ...params });
        } else {
          const { data, pageTotal } = await getOrderProfit({
            ...params,
          });
          this.summary_data = data.data;
          this.total = pageTotal;
          this.customer_options = data.customerData;
        }
      },
      //  选择时间
      searchTimeChange(time) {
        // 时间组件导出数据
        this.startTime = time.startTime;
        this.endTime = time.endTime;
        this.pageChange(1);
      },
      pageChange(page) {
        this.page = page;
        this.getOrderProfit();
      },
      sizeChange(size) {
        this.pageSize = size;
        this.pageChange(1);
      },
    },
  };
</script>

<style scoped lang="scss">
  .num-span {
    font-weight: 600;
    color: $base-color-red;
    font-size: 14px;
  }
  .num-ul {
    padding: 10px;
    p {
      font-size: 14px;
      color: #666;
      display: inline-block;
      padding-right: 10px;
    }
  }
</style>
