<template>
  <div class="content-main">
    <el-form
      :inline="true"
      :model="searchForm"
      class="search-bm-boder"
      label-width="75px"
    >
      <el-row>
        <el-form-item label="交易类型:">
          <el-select
            v-model="searchForm.jyxz"
            style="width: 170px"
            clearable
            placeholder="请选择交易类型"
          >
            <el-option
              v-for="(item, index) in ['定向', '竞价']"
              :key="index"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易性质:">
          <el-select
            v-model="searchForm.jylx"
            style="width: 170px"
            clearable
            placeholder="请选择交易性质"
          >
            <el-option
              v-for="(item, index) in ['采购', '销售']"
              :key="item"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易级别:">
          <el-select
            v-model="searchForm.lsxzbm"
            style="width: 170px"
            clearable
            placeholder="请选择交易级别"
          >
            <el-option
              v-for="(item, index) in ['区级', '市级']"
              :key="item"
              :label="item"
              :value="index + 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div class="search-button">
            <span class="search-line"></span>
            <div
              class="search-select"
              @click="searchShowMoreBool = !searchShowMoreBool"
            >
              更多条件
              <i class="el-icon-arrow-up" v-show="searchShowMoreBool"></i>
              <i class="el-icon-arrow-down" v-show="!searchShowMoreBool"></i>
            </div>
            <el-button
              type="primary"
              @click="doSearch"
              size="small"
              class="iconfont iconchaxun"
              >查询</el-button
            >
            <el-button
              type="primary"
              @click="reSearch"
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-row>
      <el-row v-show="searchShowMoreBool">
        <el-form-item label="品种:">
          <el-select
            v-model="searchForm.cd"
            style="width: 170px"
            clearable
            placeholder="请选择品种"
          >
            <el-option
              v-for="(item, index) in ['进口', '国产']"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易品种:">
          <el-select
            v-model="searchForm.lspzbm"
            style="width: 170px"
            clearable
            placeholder="请选择交易品种"
          >
            <el-option
              v-for="item in variety"
              :key="item.dictCode"
              :label="item.dictName"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易日期:">
          <el-date-picker
            v-model="searchForm.cjrq"
            style="width: 250px"
            clearable
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            range-separator="至"
            value-format="yyyy-MM-dd"
            type="daterange"
          ></el-date-picker>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row class="addBtnBg">
      <el-button
        type="primary"
        class="iconfont icondaochu11"
        size="small"
        @click="handleDaochu"
      >
        导出</el-button
      >
    </el-row>
    <el-row>
      <el-table
        :data="tableData"
        stripe
        v-loading="listLoading"
        border
        fit
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="序号"
          width="70"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjrq"
          label="交易日期"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lsxzbm"
          label="交易级别"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["市级", "区级"][scope.row.lsxzbm - 2] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="sscc"
          label="场次"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jyxz"
          label="交易类型"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["定向", "竞价"][scope.row.jyxz] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="jylx"
          label="交易性质"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["采购", "销售"][scope.row.jylx] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="cd"
          label="品种"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lspzbm"
          label="交易品种"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ varietyTypes[scope.row.lspzbm] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="trsl"
          label="投入量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="qpjg"
          label="交易底价"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjsl"
          label="成交数量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pjj"
          label="平均价"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjje"
          label="成交额"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="sxf"
          label="手续费"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lysl"
          label="履约数量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lyl"
          label="履约率"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ scope.row.lyl }}%</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="qysl"
          label="参加交易企业数"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="wbqy"
          label="外部企业数"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <el-table
        :data="tableDataDaochu"
        stripe
        border
        fit
        id="outTable"
        v-show="false"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="序号"
          width="70"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjrq"
          label="交易日期"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lsxzbm"
          label="交易级别"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["区级", "市级"][scope.row.lsxzbm - 1] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="sscc"
          label="场次"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jyxz"
          label="交易类型"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["定向", "竞价"][scope.row.jyxz] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="jylx"
          label="交易性质"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ ["采购", "销售"][scope.row.jylx] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="cd"
          label="品种"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lspzbm"
          label="交易品种"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ varietyTypes[scope.row.lspzbm] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="trsl"
          label="投入量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="qpjg"
          label="交易底价"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjsl"
          label="成交数量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pjj"
          label="平均价"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="cjje"
          label="成交额"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="sxf"
          label="手续费"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lysl"
          label="履约数量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="lyl"
          label="履约率"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>{{ scope.row.lyl }}%</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="qysl"
          label="参加交易企业数"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="wbqy"
          label="外部企业数"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <el-row style="padding: 8px"
        >投入量总和：吨；成交数量总和：吨；总成交额：元；总手续费：元；</el-row
      >
    </el-row>
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageForm.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  list,
  selectTurnoverDataImport,
} from "@/api/transaction/tradePlan/tradeInfoSummary.js";
import { varietys } from "@/api/dict/index.js";
import { dateTimeFormat, getParameter } from "@/utils/index";
import FileSaver from "file-saver";
import XLSX from "xlsx";
const status = {
  // 91000008:'已驳回',
  // 91000048:'已发布',
  // 91000049: '待安排',
  // 91000001: '待提交',
  91000004: "已提交",
};
export default {
  data() {
    return {
      searchShowMoreBool: false,
      variety: [], // 品种列表,
      varietyTypes: {},
      tradeTypes: [
        {
          label: "采购",
          value: "0",
        },
        {
          label: "销售",
          value: "1",
        },
      ],
      types: Object.keys(status).map((item) => ({
        code: item,
        name: status[item],
      })),
      listLoading: false,
      searchForm: {
        page: 1,
        // zts: Object.keys(status),
        operaterType: 3,
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      tableDataDaochu: [],
    };
  },
  created() {
    this.getTypes();
    this.getList("start");
  },
  methods: {
    handleDaochu() {
      let vb = XLSX.utils.table_to_book(document.getElementById("outTable"));
      let vbout = XLSX.write(vb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          new Blob([vbout], { type: "application/octet-stream" }),
          `交易信息汇总.xlsx`
        );
      } catch (e) {
        if (typeof console !== "undefined") {
          console.log(e, vbout);
        }
      }
      return vbout;
    },
    handlePublish(row) {
      this.$router.push({
        path: "publish",
        query: {
          state: 0,
          wtbm: row.wtbm,
        },
      });
    },
    handlePublished(row) {
      this.$router.push({
        path: "publish",
        query: {
          state: 1,
          wtbm: row.wtbm,
        },
      });
    },
    async handleDel(row) {
      await this.$confirm("此操作删除该委托, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      var res = await del(row.wtbm);
      if (res && res.infoCode === 200) {
        this.$message.success(res.msg);
        this.getList();
        return;
      }
      this.$message.error(res.msg);
    },
    handleUpdate(row) {
      this.$router.push({
        path: "plan",
        query: {
          state: 1,
          wtbm: row.wtbm,
          jylx: row.jylx,
        },
      });
    },
    handlePlan(row) {
      this.$router.push({
        path: "plan",
        query: {
          state: 0,
          wtbm: row.wtbm,
          jylx: row.jylx,
        },
      });
    },
    handleDetail(row) {
      this.$router.push({
        path: "detail",
        query: {
          state: 2,
          wtbm: row.wtbm,
        },
      });
    },
    handlePlanDetail(row) {
      this.$router.push({
        path: "planDetail",
        query: {
          state: 2,
          wtbm: row.wtbm,
          jylx: row.jylx,
        },
      });
    },
    handleAdd() {
      this.$router.push({
        path: "add",
        query: {
          state: 0,
        },
      });
    },
    handleConfirm(row) {
      this.$router.push({
        path: "confirm",
        query: {
          state: 3,
          wtbm: row.wtbm,
        },
      });
    },
    getTypes() {
      varietys(11).then((res) => {
        this.variety = res.data;
        this.variety.forEach((d) => {
          this.varietyTypes[d.dictCode] = d.dictName;
        });
      });
    },
    reSearch() {
      this.searchForm = {
        page: 1,
        operaterType: 3,
      };
      this.doSearch();
    },
    doSearch() {
      this.pageForm.pageNum = 1;
      var tbrq = this.searchForm.tbrq;
      this.getList();
    },
    async getList(type) {
      this.listLoading = true;
      let { pageNum, pageSize } = this.pageForm;
      for (var key in this.searchForm) {
        if (this.searchForm[key] === "") this.searchForm[key] = null;
        if (typeof this.searchForm[key] === "string")
          this.searchForm[key] = this.searchForm[key].trim();
      }

      let cjrq = this.searchForm.cjrq;
      let data = {
        cjrqStart: cjrq && cjrq.length ? cjrq[0] : null,
        cjrqEnd: cjrq && cjrq.length ? cjrq[1] : null,
        ...this.searchForm,
        pageNum,
        pageSize,
      };
      delete data.cjrq;
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(data));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          let searchData = JSON.parse(sessionStorage.getItem("searchData"));
          this.searchForm = getParameter(searchData, [
            "page",
            "lsxzbm",
            "jylx",
            "jyxz",
            "cd",
            "lspzbm",
          ]);
          this.searchForm.cjrq =
            searchData.cjrqStart && searchData.cjrqEnd
              ? [searchData.cjrqStart, searchData.cjrqEnd]
              : "";
          this.pageForm = getParameter(searchData, ["pageNum", "pageSize"]);
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(data));
        }
      }
      let res = await list(JSON.parse(sessionStorage.getItem("searchData")));
      this.listLoading = false;
      if (res && res.infoCode === 200) {
        this.pageForm.total = res.data.total;
        this.tableData = res.data.items;
      } else {
        this.pageForm.total = 0;
        this.tableData = [];
      }
      let resDaochu = await selectTurnoverDataImport(
        JSON.parse(sessionStorage.getItem("searchData"))
      );
      // this.listLoading = false;
      if (resDaochu && resDaochu.infoCode === 200) {
        this.tableDataDaochu = resDaochu.data;
      } else {
        this.tableDataDaochu = [];
      }
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.iconfont {
  font-size: 13px;
}
</style>
