<template>
  <div class="content-main">
    <el-form :inline="true" :model="searchForm" class="search-bm-boder">
      <el-row>
        <el-form-item label="交易日期" class="rangeDate">
          <el-date-picker
            size="small"
            style="width: 250px"
            v-model="searchForm.timeList"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            clearable
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <div class="search-button">
            <span class="search-line"></span>
            <el-button
              type="primary"
              @click="searchInfo"
              size="small"
              class="iconfont iconchaxun"
              >查询</el-button
            >
            <el-button
              type="primary"
              @click="chongzhi"
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row  class="exportBtnBg">
      <el-button type="primary" class="iconfont icondaochu11" size="small" @click="handleExport">导出</el-button>
    </el-row>
    <el-table :data="tableData" border stripe tooltip-effect="dark">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        prop="cjrq"
        label="交易日期"
        width="120"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="apcc"
        label="场次"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jyfxmc"
        label="交易方向"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="pzmc"
        label="品种"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="ccnx"
        label="生产年限"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="cd"
        label="产地"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="cjsl"
        label="成交数量(吨)"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="dj"
        label="底价(元/吨)"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.dj))
              ? Number(scope.row.dj).toFixed()
              : Number(scope.row.dj).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="zdcjj"
        label="最低价(元/吨)"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.zdcjj))
              ? Number(scope.row.zdcjj).toFixed()
              : Number(scope.row.zdcjj).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="zgcjj"
        label="最高价(元/吨)"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.zgcjj))
              ? Number(scope.row.zgcjj).toFixed()
              : Number(scope.row.zgcjj).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="jj"
        label="均价(元/吨)"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.jj))
              ? Number(scope.row.jj).toFixed()
              : Number(scope.row.jj).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="jye"
        label="交易额(元)"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.jye))
              ? Number(scope.row.jye).toFixed()
              : Number(scope.row.jye).toFixed(2)
          }}
        </template>
      </el-table-column>
    </el-table>
    <el-row class="pagination-wrap">
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page="pageForm.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  statisticalList,
  exportList,
} from "@/api/transaction/statisticalReport/transactionSummary/index";
import { getPzOptions } from "@/api/transaction/tradeEntrust/outInDepotEntrust/index.js";
export default {
  data() {
    return {
      pzProps: {
        label: "dictName",
        value: "dictCode",
        children: "subDataDictionarys",
        emitPath: false,
      },
      pzOptions: [],

      tableData: [],
      listLoading: true,
      searchForm: {
        timeList: [],
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 1,
      },
      buttonNameList: [],
    };
  },
  beforeCreate() {
    getPzOptions(11).then((res) => {
      this.pzOptions = res.data;
    });
  },
  created() {
    this.getCcList("start");
  },
  methods: {
    getCcList(type) {
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(this.searchForm));
        sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.searchForm = JSON.parse(sessionStorage.getItem("searchData"));
          this.pageForm = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.searchForm));
          sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
        }
      }
      let data = {};
      this.searchData = JSON.parse(sessionStorage.getItem("searchData"));
      if (this.searchData.timeList && this.searchData.timeList.length) {
        data.cjrqks = this.searchData.timeList[0].substr(0,10);
        data.cjrqjs = this.searchData.timeList[1].substr(0,10);
      } else {
        data.cjrqks = "";
        data.cjrqjs = "";
      }
      data = {
        ...data,
        ...JSON.parse(sessionStorage.getItem("pageData"))
      }
      statisticalList(data)
        .then((res) => {
          if (res.infoCode == 200) {
            this.tableData = res.data.items || [];
            this.pageForm.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    chongzhi() {
      this.searchForm = {
        timeList: [],
      };
      this.pageForm.pageNum = 1;
      this.getCcList();
    },

    //点击搜索时
    searchInfo() {
      this.pageForm.pageNum = 1;
      this.getCcList();
    },
    //导出
    handleExport() {
      let data = {
        cjrqks: "",
        cjrqjs: "",
      };
      this.searchData = JSON.parse(sessionStorage.getItem("searchData"));
      if (this.searchData.timeList && this.searchData.timeList.length) {
        data.cjrqks = this.searchData.timeList[0].substr(0,10);
        data.cjrqjs = this.searchData.timeList[1].substr(0,10);
      } else {
        data.cjrqks = "";
        data.cjrqjs = "";
      }
      exportList(data)
        .then((res) => {
          let exportName = "交易结果汇总表.docx";
          let blob = new Blob([res], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8" });
          if (!!window.ActiveXObject || "ActiveXObject" in window) {
            window.navigator.msSaveOrOpenBlob(blob, exportName);
          } else {
            const link = document.createElement("a");
            link.style.display = "none";
            link.href = URL.createObjectURL(blob);
            link.setAttribute("download", exportName);
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          }
        })
        .catch();
    },

    //分页
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.getCcList();
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val;
      this.getCcList();
    },
  },
};
</script>

<style lang="scss" scoped>
.pagination-wrap {
  text-align: right;
  margin-top: 10px;
}

/* .input-w-220 {
  width: 220px;
} */
.iconfont {
  font-size: 13px;
}

/* .iconfont {
  color: white;
} */
#out-table {
  border: 2px solid black;
}
#khxx_icon {
  padding: 0 5px !important;
  height: 26px !important;
  background: #169bd5 !important;
}
</style>
