<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="交易日期" prop="cjrq">
          <el-date-picker
            v-model="searchForm.cjrq"
            type="date"
            style="width: 150px"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            class="input-w"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="交易方向:">
          <el-select
            clearable
            v-model="searchForm.jyfx"
            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-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-select
            clearable
            v-model="searchForm.jyfs"
            style="width: 150px"
            placeholder="请选择交易方式"
          >
            <el-option
              v-for="(item, index) in jyfsList"
              :key="index"
              :label="item.dictName"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="委托单位:">
          <el-input
            v-model="searchForm.wtfmc"
            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-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="cjrq"
        label="交易日期"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jyfxmc"
        label="交易方向"
        align="center"
        show-overflow-tooltip
      >
        <!-- <template slot-scope="scope">
          <span v-if="scope.row.jyfx == 0">采购</span>
          <span v-if="scope.row.jyfx == 1">销售</span>
          <span v-if="scope.row.jyfx == '0,1'">采购、销售</span>
        </template> -->
      </el-table-column>
      <el-table-column
        prop="jyfsmc"
        label="交易方式"
        align="center"
        show-overflow-tooltip
      >
        <!-- <template slot-scope="scope">
          <span v-if="scope.row.jyfs == 0">竞价</span>
          <span v-if="scope.row.jyfs == 1">挂牌</span>
          <span v-if="scope.row.jyfs == 2">购销双向</span>
        </template> -->
      </el-table-column>
      <el-table-column
        prop="wtfmc"
        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 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,
} from "@/api/transaction/statisticalReport/dealStatistical/index";
import { getPzOptions } from "@/api/transaction/statisticalReport/commissionStatistical/index";
export default {
  data() {
    return {
      pzProps: {
        label: "dictName",
        value: "dictCode",
        children: "subDataDictionarys",
        emitPath: false,
      },
      pzOptions: [],

      searchShowMoreBool: false,
      statusList: [
        {
          dictCode: "0",
          dictName: "采购",
        },
        {
          dictCode: "1",
          dictName: "销售",
        },
        {
          dictCode: "0,1",
          dictName: "采购,销售",
        },
      ], //引用json状态
      jyfsList: [
        {
          dictCode: "0",
          dictName: "竞价",
        },
        {
          dictCode: "1",
          dictName: "挂牌",
        },
        {
          dictCode: "2",
          dictName: "购销双向",
        },
      ],
      tableData: [],
      listLoading: true,
      searchForm: {
        apcc: "",
        cjrq: "",
        jyfx: "",
        jyfs: "",
        wtfmc: "",
        pzbm: "",
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 1,
      },
      buttonNameList: [],
    };
  },
  beforeCreate() {
    getPzOptions(11).then((res) => {
      this.pzOptions = 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: "1",
      };
      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 = {
        apcc: "",
        cjrq: "",
        jyfx: "",
        jyfs: "",
        wtfmc: "",
        pzbm: "",
      };
      this.pageForm.pageNum = 1;
      this.getCcList();
    },

    //点击搜索时
    searchInfo() {
      this.pageForm.pageNum = 1;
      this.getCcList();
    },
    //点击查看
    showInfo(row) {
      this.$router.push({
        path: "/ywbb/statisticalReport/dealStatistical/details",
        query: {
          apcc: row.apcc,
          data: JSON.stringify(row),
        },
      });
    },
    //导出
    exportInfo(row) {
      let data = {
        apcc: row.apcc,
      };
      exportDeal(data)
        .then((res) => {
          let exportName = data.apcc+ "成交统计表.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();
    },
  },
};
</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>
