<template>
  <div>
    <basic-container class="page-crad">
      <avue-crud ref="crud" :option="option" :data="dataList" :page.sync="page" :search.sync="search"
        :cell-style="cellStyle" @search-change="searchChange" @current-change="currentChange" @size-change="sizeChange"
        @refresh-change="refreshChange" @on-load="onLoad" :table-loading="loading" @saveColumn="saveColumn"
        @resetColumn="resetColumn" @search-criteria-switch="searchCriteriaSwitch">
        <template slot="menuLeft">
          <el-button type="info" size="small" @click="outExport">导出
          </el-button>
        </template>
        <template slot="businesDateSearch">
          <el-date-picker v-model="search.businesDate" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
        </template>
        <template slot="purchaseAmount" slot-scope="{ row, index }">
          <span>{{ Number(row.purchaseAmount ? row.purchaseAmount : 0).toFixed(2) }}</span>
        </template>
        <template slot="price" slot-scope="{ row, index }">
          <span>{{ Number(row.price ? row.price : 0).toFixed(2) }}</span>
        </template>
        <template slot="singleGrossProfit" slot-scope="{ row, index }">
          <span>{{ Number(row.singleGrossProfit ? row.singleGrossProfit : 0).toFixed(2) }}</span>
        </template>
        <template slot="predictOceanFreight" slot-scope="{ row, index }">
          <span>{{ Number(row.predictOceanFreight ? row.predictOceanFreight : 0).toFixed(2) }}</span>
        </template>
        <template slot="netGrossProfit" slot-scope="{ row, index }">
          <span>{{ Number(row.netGrossProfit ? row.netGrossProfit : 0).toFixed(2) }}</span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getToken } from "@/util/auth";
import { getList } from "@/api/statisticAnalysis/brandProfit"
import { gainUser } from "@/api/basicData/customerInquiry";
import { defaultDate5 } from "@/util/date";
export default {
  name: "index",
  data() {
    return {
      UConfiguration: {
        multipleChoices: false,
        multiple: false,
        disabled: false,
        searchShow: true,
        collapseTags: false,
        placeholder: '请点击右边按钮选择',
        dicData: []
      },
      form: {},
      search: {
        date:defaultDate5()
      },
      dataList: [],
      loading: false,
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      option: {},
      optionBack: {
        align: "center",
        searchShow: true,
        searchMenuPosition: "right",
        searchSpan: 8,
        searchMenuSpan: 8,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        searchIcon: true,
        menu: false,
        summaryText: "合计",
        showSummary: true,
        sumColumnList: [
          {
            name: "quantity",
            type: "sum",
            decimals:0
          },
          {
            name: "grossProfit",
            type: "sum",
            decimals:2
          },
          {
            name: "amount",
            type: "sum",
            decimals:2
          },
          {
            name: "thisUsedProfit",
            type: "sum",
            decimals:2
          },
          {
            name: "grossProfit",
            type: "sum",
            decimals:2
          },
          {
            name: "predictOceanFreight",
            type: "sum",
            decimals:2
          },
          {
            name: "netGrossProfit",
            type: "sum",
            decimals:2
          },
          {
            name: "grossProfitRate",
            type: "sum",
            decimals:2
          },
          {
            name: "purchaseAmount",
            type: "sum",
            decimals:2
          }
        ],
        column: [
          {
            label: "业务日期",
            prop: "date",
            type: "date",
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd',
            searchRange: true,
            overHidden: true,
            hide: true,
            showColumn: false,
            search: true,
          },
          {
            label: "品牌",
            prop: "brand",
            filterable: true,
            multiple: true,
            type: "select",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            dicData: [],
            dataType: "string",
            overHidden: true,
            search: true
          },
          {
            label: "销售收入",
            prop: "amount",
            overHidden: true,
          },
          {
            label: "销售成本",
            prop: "purchaseAmount",
            overHidden: true,
          },
          {
            label: "发货数量",
            prop: "quantity",
            overHidden: true,
          },
          {
            label: "返利",
            prop: "thisUsedProfit",
            overHidden: true,
          },
          {
            label: "平均单价",
            prop: "price",
            overHidden: true,
          },
          {
            label: "单条毛利",
            prop: "singleGrossProfit",
            overHidden: true,
          },
          {
            label: "订单运费",
            prop: "predictOceanFreight",
            overHidden: true,
          },
          {
            label: "毛利额（不含返利）",
            prop: "netGrossProfit",
            width:140,
            overHidden: true,
          },
          {
            label: "毛利额（含返利）",
            prop: "grossProfit",
            width:140,
            overHidden: true,
          },
          {
            label: "订单毛利率",
            prop: "grossProfitRate",
            overHidden: true,
          }
        ]
      }
    };
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(229), this.optionBack);
    this.getAllWorkDicts()
  },
  activated() {
   this.$nextTick(() => {
      this.$refs.crud.refreshTable();
    });
  },
  methods: {
    getAllWorkDicts() {
      this.option.height = window.innerHeight - 210;
      this.getWorkDicts('brand').then(res => {
        this.findObject(this.option.column, "brand").dicData =
          res.data.data;
      })
      this.$refs.crud.init();
      // gainUser().then(res => {
      //   this.findObject(this.option.column, "chargeMember").dicData =
      //     res.data.data;
      // })
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params = {}) {
      let data = this.deepClone(Object.assign({}, params, this.search));
      if (data.date) {
        data.beginTime = data.date[0]
        data.endTime = data.date[1]
        delete data.date
      }
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        data
      ).then(res => {
        this.dataList = res.data.data.records ? res.data.data.records : [];
        this.page.total = res.data.data.total;
      }).finally(() => {
        this.loading = false;
      });
    },

    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(this.getColumnName(229), this.option);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = this.optionBack;
      const inSave = await this.delColumnData(this.getColumnName(229), this.optionBack);
      this.getAllWorkDicts()
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    editOpen(row) {
    },
    outExport() {
      let params = { ...this.search }
      if (params.date && params.date.length > 0) {
        params = {
          ...params,
          beginTime: params.date[0],
          endTime: params.date[1],
        }
      } else {
        params = {
          ...params,
          beginTime: '',
          endTime: '',
        }
      }
      window.open(
        `/api/blade-purchase-sales/exportOrder/brandProfitExport?${this.website.tokenHeader
        }=${getToken()}&beginTime=${params.beginTime}&endTime=${params.endTime}&brand=${params.brand ? params.brand : ''}`
      );
    }
  }
};
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}

::v-deep .el-table__expanded-cell[class*="cell"] {
  padding: 0px;
}

.itemTable ::v-deep .el-table {
  width: 100%;
}
</style>
