<template>
  <ContainerQuery>
    <div slot="left">
      <el-button size="small" type="primary" @click="getAllData(1)">
        导出
      </el-button>
    </div>
    <div slot="more">
      <el-form inline size="small" style="margin-bottom: 0">
        <el-form-item>
          <el-input
            v-model="search_form.goodsName"
            placeholder="商品名称"
            @keyup.enter.native="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="pageChange(1)"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="search_form.outStatus"
            style="width: 150px"
            placeholder="入库状态"
            @change="pageChange(1)"
          >
            <el-option label="已入库" :value="5"></el-option>
            <el-option label="未入库" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="search_form.time"
            type="daterange"
            range-separator="-"
            start-placeholder="汇总开始日期"
            end-placeholder="汇总结束日期"
            value-format="timestamp"
            @change="timeChange"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <SelectCustomer
            v-model="customer_name"
            @clear="customerClear"
            @change="customerSel"
          />
        </el-form-item>
        <el-form-item>
          <GoodsCategory
            v-model="category"
            size="small"
            :check-strictly="true"
            width="150"
            placeholder="商品分类"
            @change="cateChange"
          />
        </el-form-item>
        <el-form-item>
          <SelectShop
            v-model="search_form.shopId"
            :is-default="true"
            :clearable="false"
            @change="selShop"
            @default="shopDefault"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="onReturn" @change="RemoveChange">
            去除退货
          </el-checkbox>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" show-summary :summary-method="getSummaries">
      <el-table-column
        v-if="form_name === '商品'"
        prop="categoryName"
        label="商品类别"
        min-width="100"
      ></el-table-column>
      <el-table-column
        v-if="form_name === '客户'"
        prop="customerName"
        :label="form_name"
        min-width="120"
      ></el-table-column>
      <el-table-column
        v-if="form_name === '销售员'"
        prop="salesManName"
        :label="form_name"
        min-width="120"
      ></el-table-column>
      <el-table-column prop="goodsCode" label="商品" min-width="160">
        <template slot-scope="scope">
          <div class="click-div" @click="detailPage(scope.row)">
            <p>{{ scope.row.goodsName }}</p>
            <p>{{ scope.row.goodsCode }}</p>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-if="specificationFlag"
        prop="unitName"
        label="规格"
        min-width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.unitName }};{{ scope.row.skuName }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="shopFlag"
        prop="shopName"
        label="商铺"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="customerName"
        label="客户"
        min-width="120"
      ></el-table-column>
      <el-table-column
        v-if="quantityFlag"
        prop="num"
        label="数量"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formatNub(scope.row.num) }}
          <div v-if="scope.row.isEq === 5">
            其他单位:{{ $_common.formatNub(scope.row.otherNum) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-if="marketFlag"
        prop="totalMoney"
        label="销售收入"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.totalMoney) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="costFlag"
        prop="costPrice"
        label="成本"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.costPrice) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="totalCostFlag"
        prop="totalCost"
        label="总成本"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.totalCost) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="grossMarginFlag"
        prop="profit"
        label="毛利"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.profit) }}
        </template>
      </el-table-column>
      <el-table-column prop="outStatus" label="入库状态" min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.outStatus === 5" class="success-status">
            已入库
          </span>
          <span v-if="scope.row.outStatus === 4" class="warning-status">
            未入库
          </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="totalGrossProfitFlag"
        prop="totalProfit"
        label="总毛利"
        min-width="100"
      >
        <template slot="header">
          <span class="operation">总毛利</span>
          <el-popover popper-class="custom-table-checkbox" trigger="click">
            <el-checkbox-group v-model="checkList">
              <el-checkbox
                v-for="(item, index) in columns"
                :key="index"
                :label="item.label"
                @change="change"
              ></el-checkbox>
            </el-checkbox-group>
            <el-button
              slot="reference"
              icon="el-icon-setting"
              type="text"
            ></el-button>
          </el-popover>
        </template>
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.totalProfit) }}
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>

<script>
  import GoodsCategory from "@/component/common/GoodsCategory.vue";
  import FooterPage from "@/component/common/FooterPage";
  import SelectCustomer from "@/component/common/SelectCustomer.vue";
  import SelectShop from "@/component/goods/SelectShop.vue";
  import { getAllOrderData, exportGetAllOrderData } from "@/api/Order";

  export default {
    name: "GoodsForm",
    components: {
      GoodsCategory,
      FooterPage,
      SelectShop,
      SelectCustomer,
    },
    data() {
      return {
        show_shop: false,
        category: [],
        page: 1,
        pageSize: 10,
        total: 0,
        tableData: [],
        onReturn: true,
        customer_name: "",
        search_form: {
          outStatus: "",
          goodsName: "",
          categoryId: "",
          customerId: "",
          shopId: "",
          start: "",
          end: "",
          onReturn: 1,
          time: [],
        },
        type: 1,
        form_name: "",
        checkList: [
          "规格",
          "商铺",
          "数量",
          "销售收入",
          "成本",
          "总成本",
          "毛利",
          "总毛利",
        ],
        columns: [
          {
            label: "规格",
          },
          {
            label: "商铺",
          },
          {
            label: "数量",
          },
          {
            label: "销售收入",
          },
          {
            label: "成本",
          },
          {
            label: "总成本",
          },
          {
            label: "毛利",
          },
          {
            label: "总毛利",
          },
        ],
        specificationFlag: true,
        shopFlag: true,
        quantityFlag: true,
        marketFlag: true,
        costFlag: true,
        totalCostFlag: true,
        grossMarginFlag: true,
        totalGrossProfitFlag: true,
      };
    },
    created() {
      let start = new Date(this.$_common.showMonthFirstDay());
      let end = new Date();
      this.search_form.end = parseInt(end.getTime() / 1000);
      this.search_form.start = parseInt(start.getTime() / 1000);
      this.search_form.time = [start, end];
      switch (this.$route.name) {
        case "GoodsForm":
          this.form_name = "商品";
          this.type = 1;
          break;
        case "CustomerForm":
          this.form_name = "客户";
          this.type = 2;
          break;
        case "StaffForm":
          this.form_name = "销售员";
          this.type = 3;
          break;
      }
      // this.getAllData();
    },
    methods: {
      shopDefault(val) {
        this.search_form.shopId = val;
        this.pageChange(1);
      },
      //  选择商铺
      selShop(val, row) {
        this.pageChange(1);
      },
      // 选择客户
      customerSel(val, list) {
        this.search_form.customerId = list[0].id;
        this.pageChange(1);
      },
      customerClear() {
        this.search_form.customerId = "";
        this.customer_name = "";
        this.pageChange(1);
      },
      // 分类
      cateChange(val) {
        if (val && val.length) {
          this.category = val;
          this.search_form.categoryId = val[val.length - 1];
        } else {
          this.search_form.categoryId = "";
          this.category = "";
        }
        this.pageChange(1);
      },
      //  时间搜索
      timeChange(val) {
        this.search_form.start = val[0] / 1000;
        this.search_form.end = val[1] / 1000 + 86399;
        this.pageChange(1);
      },
      //  去除采购 RemoveChange
      RemoveChange(val) {
        this.search_form.onReturn = val ? 1 : 0;
        this.pageChange(1);
      },
      // 获取列表
      async getAllData(isexport) {
        const params = {
          outStatus: this.search_form.outStatus,
          goodsName: this.search_form.goodsName,
          categoryId: this.search_form.categoryId,
          customerId: this.search_form.customerId,
          shopId: this.search_form.shopId,
          start: this.search_form.start,
          end: this.search_form.end,
          type: this.type,
          onReturn: this.search_form.onReturn,
          page: this.page,
          pageSize: this.pageSize,
        };
        if (isexport === 1) {
          params.export = 1;
          const { data } = await exportGetAllOrderData(params);
        } else {
          const { data, pageTotal } = await getAllOrderData(params);
          this.tableData = data;
          this.total = pageTotal;
        }
      },
      // 合计
      getSummaries(param) {
        return this.$_common.getSummaries(param, [
          "单价",
          "销售收入",
          "成本",
          "总成本",
          "毛利",
          "总毛利",
        ]);
      },
      // 切页
      pageChange(val) {
        this.page = val;
        this.getAllData();
      },
      // 每页数据大小改变
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      detailPage(row) {
        const params = {
          goodsName: row.goodsName,
          goodsId: row.goodsId,
          shopId: row.shopId,
          start: this.search_form.start,
          end: this.search_form.end,
          customerId: row.customerId,
        };
        sessionStorage.setItem("profitSearch", JSON.stringify(params));
        setTimeout(() => {
          this.$router.push("/order/SaleTotalForm/SaleSummary");
        }, 50);
      },
      change() {
        this.specificationFlag = this.checkList.some((item) => item === "规格");
        this.shopFlag = this.checkList.some((item) => item === "商铺");
        this.quantityFlag = this.checkList.some((item) => item === "数量");
        this.marketFlag = this.checkList.some((item) => item === "销售收入");
        this.costFlag = this.checkList.some((item) => item === "成本");
        this.totalCostFlag = this.checkList.some((item) => item === "总成本");
        this.grossMarginFlag = this.checkList.some((item) => item === "毛利");
        this.totalGrossProfitFlag = this.checkList.some(
          (item) => item === "总毛利"
        );
      },
    },
  };
</script>

<style scoped lang="scss">
  .look-text {
    cursor: pointer;
    color: #409eff;
  }
  .custom-table-checkbox {
    .el-checkbox {
      display: block !important;
      margin: 0 0 $base-padding/4 0;
    }
  }
</style>
