<template>
  <div class="content-main">
    <el-form :inline="true" :model="searchForm" class="search-bm-boder">
      <el-row>
        <el-form-item label="场次:">
          <el-input
            v-model="searchForm.cc"
            clearable
            style="width: 170px"
            placeholder="请输入场次"
          ></el-input>
        </el-form-item>
        <el-form-item label="委托单位:">
          <el-input
            v-model="searchForm.wtdw"
            clearable
            style="width: 200px"
            placeholder="请输入委托单位"
          ></el-input>
        </el-form-item>
        <el-form-item label="品种:">
          <el-cascader
            style="width: 170px"
            :options="pzOptions"
            :props="pzProps"
            :show-all-levels="false"
            v-model="searchForm.pzbm"
            placeholder="请选择"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="粮食性质:">
          <el-select
            v-model="searchForm.lsxzbm"
            style="width: 170px"
            clearable
            placeholder="请选择粮食性质"
          >
            <el-option
              v-for="item in xzOptions"
              :key="item.dictCode"
              :label="item.dictName"
              :value="item.dictCode"
            ></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="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-row v-show="searchShowMoreBool">
        <el-form-item label="会员名称:">
          <el-input
            v-model="searchForm.hymc"
            clearable
            style="width: 200px"
            placeholder="请输入会员名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="交易日期:" class="rangeDate">
          <el-date-picker
            style="width: 250px"
            v-model="searchForm.timeList"
            type="daterange"
            value-format="yyyy-MM-dd"
            :default-time="['00:00:00', '23:59:59']"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="成交情况:">
          <el-select
            clearable
            v-model="searchForm.cjqk"
            style="width: 150px"
            placeholder="请选择成交情况"
          >
            <el-option
              v-for="(item, index) in statusList"
              :key="index"
              :label="item.dictName"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row  class="exportBtnBg">
      <el-button type="primary" class="iconfont icondaochu11" size="small" @click="exportInfo">导出</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="cc"
        label="场次"
        width="100"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="ccbh"
        label="场次编号"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="wtdw"
        label="委托单位"
        align="center"
        width="200"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="pzmc"
        label="品种"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="lsxzmc"
        label="粮食性质"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="hymc"
        label="会员名称"
        align="center"
        width="190"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jyqbzj"
        label="交易前保证金"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            /^[0-9]+$/.test(Number(scope.row.jyqbzj))
              ? Number(scope.row.jyqbzj).toFixed()
              : Number(scope.row.jyqbzj).toFixed(2)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="jyrq"
        label="交易日期"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="hth"
        label="合同号"
        width="180"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="cjqk"
        label="成交情况"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>

      <!-- <el-table-column align="center" label="操作" width="100px">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="light"
            content="查看"
            placement="top"
          >
            <el-button
              type="primary"
              size="small"
              @click="showInfo(scope.row)"
              class="operate_show_btn iconfont iconchakan2"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="导出"
            placement="top"
          >
            <el-button
              type="primary"
              size="small"
              @click="exportInfo(scope.row)"
              class="operate_show_btn iconfont icondaochu11"
            ></el-button>
          </el-tooltip>
        </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,
  exportDeal,
  lsxz,
} from "@/api/transaction/registrationStatus/index";
import { getPzOptions } from "@/api/transaction/tradeEntrust/outInDepotEntrust/index.js";
// 引入导出Excel表格依赖
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
  data() {
    return {
      pzProps: {
        label: "dictName",
        value: "dictCode",
        children: "subDataDictionarys",
        emitPath: false,
      },
      pzOptions: [],
      xzOptions: [
        { dictName: "市储", dictCode: "1" },
        { dictName: "区储", dictCode: "2" },
        { dictName: "市自", dictCode: "3" },
      ], // 粮食性质列表

      searchShowMoreBool: false,
      statusList: [
        {
          dictCode: "1",
          dictName: "已成交",
        },
        {
          dictCode: "0",
          dictName: "未成交",
        },
      ], //引用json状态
      jyfsList: [
        {
          dictCode: "0",
          dictName: "竞价",
        },
        {
          dictCode: "1",
          dictName: "挂牌",
        },
        {
          dictCode: "2",
          dictName: "购销双向",
        },
      ],
      tableData: [],
      listLoading: true,
      searchForm: {
        cc: "",
        wtdw: "",
        pzbm: "",
        lsxzbm: "",
        hymc: "",
        timeList: [],
        cjqk: "",
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 1,
      },
      buttonNameList: [],
    };
  },
  beforeCreate() {
    getPzOptions(11).then((res) => {
      this.pzOptions = res.data;
    });
    // lsxz().then((res) => {
    //   if (res.infoCode === 200) {
    //     this.xzOptions = res.data;
    //   }
    // });
  },
  created() {
    // this.getresourceCode();
    this.getCcList("start");
  },
  methods: {
    /* getresourceCode() {
      let codeData = {
        resourceCode: sessionStorage.getItem("resourceCode"),
        userId: sessionStorage.getItem("userCode"),
      };
      authorityControl(codeData).then((res) => {
        this.buttonNameList = [];
        res.data.forEach((item) => {
          this.buttonNameList.push(item.resourceName);
        });
        this.getCcList("start");
      });
    }, */
    getCcList(type) {
      // pageType 1 场次  2：底价
      // 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();
      }
      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 = {
        ...JSON.parse(sessionStorage.getItem("searchData")),
        // lsxzbm: "2",
      };
      if (data.timeList && data.timeList.length) {
        data.jyrqStart = data.timeList[0];
        data.jyrqEnd = data.timeList[1];
      } else {
        data.jyrqStart = null;
        data.jyrqEnd = null;
      }
      console.log(data);
      statisticalList(JSON.parse(sessionStorage.getItem("pageData")), 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 = {
        cc: "",
        wtdw: "",
        pzbm: "",
        lsxzbm: "",
        hymc: "",
        timeList: [],
        cjqk: "",
      };
      this.pageForm.pageNum = 1;
      this.getCcList();
    },

    //点击搜索时
    searchInfo() {
      this.pageForm.pageNum = 1;
      this.getCcList();
    },
    //点击查看
    showInfo(row) {
      this.$router.push({
        // path: "/transaction/registrationStatus/details",
        name: "registrationStatusDetails",
        query: {
          apcc: row.apcc,
          data: JSON.stringify(row),
        },
      });
    },
    // showInfo(row, type) {
    //   console.log(type, "type");
    //   this.$router.push({
    //     path: "/transaction/fieldPlan/detail",
    //     query: {
    //       ccbm: row.ccbm,
    //       data: row,
    //       type: "show",
    //     },
    //   });
    // },
    //导出
    exportInfo() {
      let data = {
        ...this.searchForm
      };
      if (data.timeList && data.timeList.length) {
        data.jyrqStart = data.timeList[0];
        data.jyrqEnd = data.timeList[1];
      } else {
        data.jyrqStart = null;
        data.jyrqEnd = null;
      }
      exportDeal(data)
        .then((res) => {
          let exportName = "报名情况统计表.xlsx";
          let blob = new Blob([res], { type: "application/vnd.ms-excel;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();
    },
    //定义导出Excel表格事件
    exportExcel() {
      /* 从表生成工作簿对象 */
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      /* 获取二进制字符串作为输出 */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          //Blob 对象表示一个不可变、原始数据的类文件对象。
          //Blob 表示的不一定是JavaScript原生格式的数据。
          //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
          //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
          new Blob([wbout], { type: "application/octet-stream" }),
          //设置导出文件名称
          "sheetjs.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
  },
};
</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>
