<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.apcc"
            clearable
            style="width: 170px"
            placeholder="请输入场次"
          ></el-input>
        </el-form-item>
        <el-form-item label="交易性质:">
          <el-select
            clearable
            v-model="searchForm.jylx"
            style="width: 170px"
            placeholder="请选择交易性质"
          >
            <el-option
              v-for="item in statusList"
              :key="item.dictCode"
              :label="item.dictName"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <label slot="label">品种:</label>
          <el-select
            v-model="searchForm.pzbm"
            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>
          <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-row> -->
    </el-form>

    <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="apcc"
        label="场次"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jylxmc"
        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="sxflx"
        label="手续费类型"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jysl"
        label="数量"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="sxfl"
        label="手续费"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column align="center" label="操作" width="180px">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="light"
            content="查看"
            placement="top"
          >
            <el-button
              type="primary"
              size="small"
              @click="showInfo(scope.row, 'detail')"
              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="showInfo(scope.row, 'daochu')"
              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="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import { varietys } from "@/api/transaction/trading/transactionSignupFood/index";
import { selectTurnoverSxfPageList } from "@/api/transaction/trading/serviceChargeStatistics/index";
import { authorityControl } from "@/api/menuList";
// 引入导出Excel表格依赖
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
  data() {
    return {
      variety: [],
      excalFalse: false,
      tableMrtabl: [],
      searchShowMoreBool: false,
      statusList: [
        { dictCode: null, dictName: "不限" },
        {
          dictCode: "0",
          dictName: "采购",
        },
        {
          dictCode: "1",
          dictName: "销售",
        },
      ], //引用json状态
      ztOptions: [],
      tableData: [],
      listLoading: true,
      showType: false,
      searchForm: {
        apcc: "",
        cczt: "",
        jylx: null,
        pzbm: null,
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 1,
      },
      buttonNameList: [],
    };
  },
  created() {
    // this.selectList()
    this.getresourceCode();
    varietys().then((res) => {
      this.variety = res.data;
    });
  },
  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.selectList("start");
      });
    },
    async selectList(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 res = await selectTurnoverSxfPageList(
        JSON.parse(sessionStorage.getItem("pageData")),
        JSON.parse(sessionStorage.getItem("searchData"))
      );
      if (res.infoCode == 200) {
        this.tableData = res.data.items || [];
        this.tableData.forEach((d) => {
          d.jylxmc = ["采购", "销售"][d.jylx];
        });
        this.pageForm.total = res.data.total;
      }
    },
    chongzhi() {
      this.searchForm = {
        apcc: "",
        cczt: "",
        jylx: null,
        pzbm: null,
      };
      this.selectList();
    },
    submitUpload() {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要导入的模板！",
        });
        return;
      }
      this.$refs.upload.submit();
    },
    uploadSectionFile(param) {
      var fileObj = param.file;
      // FormData 对象
      var form = new FormData();
      // 文件对象
      form.append("file", fileObj);
      form.append("userId", this.userId);
      form.append("userName", this.userName);
      this.GLOBAL.POST("/hqx/knowledge/importKnowledge", form).then((res) => {
        if (res.data.success == true) {
          this.$message({
            type: "success",
            message: res.data.msg,
          });
          this.fileList = [];
        } else {
          this.$message({
            type: "success",
            message: res.data.msg,
          });
          this.fileList = [];
        }
      });
    },

    //点击搜索时
    searchInfo() {
      this.pageForm.pageNum = 1;
      this.selectList();
    },
    //点击查看 跳到detail页面
    showInfo(row, state) {
      this.$router.push({
        path: "detail",
        query: {
          jgbm: row.jgbm,
          jyrq: row.jyrq,
          wtfmc: row.wtfmc,
          jylxmc: row.jylxmc,
          pzmc: row.pzmc,
          apcc: row.apcc,
          state: state,
        },
      });
    },
    //分页
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.selectList();
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val;
      this.selectList();
    },

    //定义导出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 {
  color: white;
}
#out-table {
  border: 2px solid black;
}
</style>
