<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <!--用户数据-->
        <el-radio-group v-model="day" size="medium" @change="radioChange">
          <el-radio-button :label="0" :value="0">当日</el-radio-button>
          <el-radio-button :label="1" :value="1">本周</el-radio-button>
          <el-radio-button :label="2" :value="2">本月</el-radio-button>
          <el-radio-button :label="3" :value="3">自定义时间</el-radio-button>
        </el-radio-group>
        <el-date-picker
          style="margin-left: 10px"
          v-if="day == 3"
          @change="pickerChange"
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-col>
      <el-col :span="24" style="margin-top: 30px">
        <div class="header" style="width: 100%" v-if="!hdUserId">
          <el-row class="row" :gutter="12">
            <el-col :span="8">
              <el-card shadow="always">
                <div class="item">
                  <div style="padding: 0px 10px 10px 10px">
                    <span>所有商品总入库金额(￥)</span>
                  </div>
                  <div class="count">
                    {{ statistics ? statistics.totalPrice : 0 }}
                  </div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card shadow="always">
                <div class="item">
                  <div style="padding: 0px 10px 10px 10px">
                    <span>所有商品总出库金额(￥)</span>
                  </div>
                  <div class="count">
                    {{ statistics ? statistics.outBoundNumTotalPrice : 0 }}
                  </div>
                </div>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card shadow="always">
                <div class="item">
                  <div style="padding: 0px 10px 10px 10px">
                    <span>所有商品总出库利润(￥)</span>
                  </div>
                  <div class="count">
                    {{ statistics ? statistics.outBoundProfit : 0 }}
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <el-col :span="24" :xs="24" style="margin-top: 30px">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
        >
          <el-form-item prop="key">
            <el-input
              v-model="queryParams.key"
              style="width: 200px"
              placeholder="商品ID/商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item prop="categoryId">
            <el-cascader
              v-model="queryParams.categoryId"
              ref="formCascader"
              placeholder="请选择分类"
              :props="categoryprops"
              @change="cascaderChange"
            />
          </el-form-item>

          <el-form-item label="商品状态" prop="spuStatus">
            <el-select
              v-model="queryParams.spuStatus"
              placeholder="请选择商品状态"
              style="width: 240px"
            >
              <el-option label="全部" value=""> </el-option>
              <el-option label="售罄" value="2"> </el-option>
              <el-option label="上架" value="0"> </el-option>
              <el-option label="下架" value="1"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getList"
              >搜索</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-button icon="el-icon-refresh" size="mini" @click="handleExport"
            >批量导出</el-button
          >
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
      </el-col>
      <el-table
        v-loading="loading"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="商品ID" align="center" prop="spuId" />
        <el-table-column label="商品名称" align="center" prop="spuName" />
        <el-table-column label="商品分类" align="center" prop="categoryName" />
        <el-table-column label="商品状态" align="storeGoods">
          <template slot-scope="scope">
            {{
              scope.row.spuStatus == 0
                ? "上架"
                : scope.row.spuStatus == 1
                ? "下架"
                : scope.row.spuStatus == 2
                ? "售罄"
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column label="当前库存数量" align="center" prop="canOutboundNum" />
        <el-table-column
          label="当前库存总金额(￥)"
          align="center"
          prop="canOutboundTotalPrice"
          width="150"
        />
        <el-table-column label="入库总数量" align="center" prop="totalStoreNum" />
        <el-table-column label="入库总金额(￥)" align="center" prop="totalStorePrice" />
        <el-table-column
          label="出库总数量"
          align="center"
          prop="totalOutboundNum"
          width="200"
        />
        <el-table-column
          label="出库总金额(￥)"
          align="center"
          prop="totalOutboundPrice"
        />
        <el-table-column
          label="已出库利润(￥)"
          align="center"
          prop="outBoundProfit"
          width="200"
        />

        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDetail(scope.row)"
              v-hasPermi="['storage:detail']"
              >流水详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
  </div>
</template>

<script>
import {
  outboundStoreSerialqueryPage,
  storeManageRemove,
  statisticsPrice,
} from "@/api/manage/storage.js";
import { getClassificationListPage } from "@/api/common/index";
export default {
  data() {
    return {
      day: 0,
      showSearch: true,
      queryParams: {
        key: "",
        pageNo: 1,
        pageSize: 10,
        storeTime: "",
        createTime: "",
      },
      dataList: [],
      loading: false,
      statistics: {},
      dateRange: [],
      categoryprops: {
        checkStrictly: true,
        lazy: true,
        lazyLoad: this.categoryLazyLoad,
      },
      total: 0,
      ids: [],
    };
  },
  mounted() {
    this.getList();
    this.getstatisticsPrice();
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.spuId);
      console.log(this.ids);
      
    },
    handleExport() {
      this.queryParams.spuIds = this.ids.toString();
      this.download(
        "system/outboundStoreSerial/export",
        {
          ...this.queryParams,
        },
        `出入库流水表_${new Date().getTime()}.xlsx`
      );
    },
    pickerChange(val) {
      this.getstatisticsPrice(val);
    },
    radioChange() {
      if (this.day != 3) {
        this.getstatisticsPrice();
      }
    },
    getstatisticsPrice(val) {
      let startTime;
      let endTime;
      let dayType;
      if (this.day != 3) {
        dayType = this.day;
      } else {
        dayType = "";
      }
      if (val) {
        startTime = val[0];
        endTime = val[1];
      }

      statisticsPrice({ dayType: dayType, startTime: startTime, endTime: endTime }).then(
        (res) => {
          if (res.code == 200) {
            this.statistics = res.data;
          }
        }
      );
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    getList() {
      this.loading = true;
      outboundStoreSerialqueryPage(this.queryParams).then((res) => {
        this.dialogVisible = true;
        this.dataList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleRemove(record) {
      this.$confirm("是否确认删除该记录,删除后无法恢复？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          storeManageRemove(record.id).then((res) => {
            if (res.code == 200) {
              this.$message.success("删除成功！");
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    handleDetail(record) {
      this.$router.push({ path: "/storage/running/detail", query: record });
    },

    categoryLazyLoad(node, resolve) {
      let that = this;
      let level = node.level;
      console.log(node);
      if (!node.data) {
        getClassificationListPage({ parentId: 0 }).then((res) => {
          //接口
          const nodes = Array.from(res.data).map((item, index) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 2,
          }));
          console.log("111", node);
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else if (level == 1) {
        getClassificationListPage({ parentId: node.data.value }).then((res) => {
          const nodes = Array.from(res.data).map((item) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 2,
            // level: 2,
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else if (level == 2) {
        getClassificationListPage({ parentId: node.data.value }).then((res) => {
          const nodes = Array.from(res.data).map((item) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 1,
            level: 1,
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else {
        resolve({});
      }
    },
    cascaderChange(val) {
      if (val.length == 1) {
        this.queryParams.categoryId = val[0];
      } else if (val.length == 2) {
        this.queryParams.categoryId = val[1];
      } else if (val.length == 3) {
        this.queryParams.categoryId = val[2];
      }
    },
  },
};
</script>

<style scoped lang="scss">
.header {
  margin-bottom: 30px;
  .item {
    height: 100px;
  }
  .row {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    .count {
      padding: 10px;
      margin-top: 10px;
      font-size: 20px;
      font-weight: 500;
      text-align: center;
      color: #164bd3e6;
    }
    .zz {
      font-size: 14px;
      color: red;
      padding: 0px 10px;
    }
  }
}
</style>
