<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.wtbh"
            clearable
            style="width: 170px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <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-input
            v-model="searchForm.wtfmc"
            clearable
            style="width: 170px"
            placeholder="请输入委托单位"
          ></el-input>
        </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.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 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="交易日期" prop="jyrq">
          <el-date-picker
            v-model="searchForm.jyrq"
            type="date"
            style="width: 150px"
            value-format="yyyy-MM-dd"
            class="input-w"
          ></el-date-picker>
        </el-form-item>
      </el-row>
    </el-form>

    <!-- <el-row class="addBtnBg">
      <el-button
        type="primary"
        class="iconfont iconxinzeng"
        size="small"
        @click="handleAdd"
        >新增</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="wtbh"
        label="委托编号"
        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="apcc"
        label="场次"
        align="center"
        show-overflow-tooltip
      ></el-table-column> -->
      <el-table-column
        prop="wtfmc"
        label="委托单位"
        align="center"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="jyfx"
        label="交易方向"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.jyfx == '0'">不限</span> -->
          <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="jyfs"
        label="交易方式"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.jyfx == '0'">不限</span> -->
          <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="pz"
        label="品种"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jyrq"
        label="交易日期"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="jysj"
        label="交易时间"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="apzt"
        label="状态"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.apzt == null">待编辑</span>
          <span v-if="scope.row.apzt == 91000001">待提交</span>
          <span v-if="scope.row.apzt == 91000300">待盖章</span>
          <span v-if="scope.row.apzt == 91000600">已确认</span>
          <span v-if="scope.row.apzt == 91000103">签章驳回</span>
        </template>
      </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, 1)"
              class="operate_show_btn iconfont iconchakan2"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="编辑底价"
            placement="top"
            v-if="buttonNameList.indexOf('编辑') > -1"
          >
            <el-button
              v-if="
                scope.row.apzt == null ||
                scope.row.apzt == 91000001 ||
                scope.row.apzt == 91000103
              "
              type="primary"
              size="small"
              @click="updateInfo(scope.row, 2)"
              class="operate_edit_btn iconfont iconxiugai"
            ></el-button>
          </el-tooltip>
          <!-- <el-tooltip
              class="item"
              effect="light"
              content="删除"
              placement="top"
              v-if="buttonNameList.indexOf('删除') > -1"
            >
              <el-button
                v-if="
                  scope.row.ztmc == '待提交' || scope.row.ztmc == '签章驳回'
                "
                @click="handleDel(scope.row)"
                type="primary"
                class="operate_delete_btn iconfont iconshanchu"
                size="small"
              ></el-button>
            </el-tooltip> -->
          <el-tooltip
            class="item"
            effect="light"
            content="盖章"
            placement="top"
            v-if="buttonNameList.indexOf('盖章') > -1"
          >
            <!--条件  v-if="scope.row.shzt == status1.toBeReview.value" -->
            <el-button
              v-if="scope.row.apzt == 91000300"
              type="primary"
              class="operate_edit_btn iconfont icongaizhang"
              @click="handleSeal(scope.row, 3)"
              size="small"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="导出"
            placement="top"
            v-if="buttonNameList.indexOf('导出') > -1"
          >
            <el-button
              v-if="scope.row.apzt == 91000600 && false"
              type="primary"
              @click="exportFile(scope.row, 4)"
              class="operate_download_btn iconfont icondaochu11"
              size="small"
            ></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.applyNum"
        :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 {
  getScalePlanUpdate,
  deleteListItem,
  selectOwnList,
  getScalePlanDetails,
  selectParentList,
  excalOut,
  varietys,
} from "@/api/transaction/trading/transactionSignupFood/index";
import {
  selectList,
  delTrademark,
  selectPage,
} from "@/api/transaction/trading/fieldInfo/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: "0",
          dictName: "采购",
        },
        {
          dictCode: "1",
          dictName: "销售",
        },
      ], //引用json状态
      jyfsList: [
        {
          dictCode: "0",
          dictName: "竞价",
        },
        {
          dictCode: "1",
          dictName: "挂牌",
        },
        {
          dictCode: "2",
          dictName: "购销双向",
        },
      ],
      ztOptions: [],
      tableData: [],
      listLoading: true,
      showType: false,
      searchForm: {
        wtbh: "",
        // wtbm:"",
        apcc: "",
        wtfmc: "",
        jyfx: "",
        jyfs: "",
        jyrq: "",
        cczt: "",
        jylx: null,
        pz: 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) {
      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));
        }
      }
      // pageType 1 场次  2：底价
      let pageType = 2;
      let data = {
        ...JSON.parse(sessionStorage.getItem("searchData")),
        pageType,
      };
      let res = await selectList(
        JSON.parse(sessionStorage.getItem("pageData")),
        data
      );
      if (res.infoCode == 200) {
        // console.log(res,'底价列表')
        this.tableData = res.data.items || [];
        this.tableData.forEach((d) => {
          d.jylxmc = ["采购", "销售"][d.jylx];
        });
        this.pageForm.total = res.data.total;
      }
    },
    handleAdd() {
      this.$router.push({ path: "detail", query: { state: 0 } });
    },
    chongzhi() {
      (this.searchForm = {
        wtbh: "",
        apcc: "",
        wtfmc: "",
        jyfx: "",
        jyfs: "",
        jyrq: "",
        cczt: "",
        jylx: null,
        pz: 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, type) {
      // console.log(type,'type')
      this.$router.push({
        path: "/transaction/tradingFloorPrice/detail",
        query: {
          ccbm: row.ccbm,
          data: row,
          type: "show",
        },
      });
    },
    exportFile(row, type) {
      // console.log(type,'type')
      this.$router.push({
        path: "/transaction/tradingFloorPrice/detail",
        query: {
          ccbm: row.ccbm,
          data: row,
          type: "export",
        },
      });
    },
    //去detail页面 进行数据添加
    updateInfo(row, type) {
      this.$router.push({
        path: "/transaction/tradingFloorPrice/detail/update",
        query: {
          ccbm: row.ccbm,
          data: row,
          type: "update",
        },
      });
    },
    // 盖章
    handleSeal(row, type) {
      this.$router.push({
        path: "/transaction/tradingFloorPrice/detail/update",
        query: {
          ccbm: row.ccbm,
          data: row,
          type: type,
        },
      });
    },
    //点击删除按钮
    deleteInfo(id) {
      this.$confirm("此操作将删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("999", id);
          delTrademark(id)
            .then((res) => {
              console.log("res", res);
              if (res.infoCode == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.selectList();
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败!",
                });
              }
            })
            .catch((err) => {
              console.log("err", err);
            });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //分页
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.selectList();
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val;
      this.selectList();
    },

    biaodi() {
      this.$router.push({
        name: "transactionSignupFoodDetail",
        query: {
          type: "signa",
        },
      });
    },
    //定义导出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;
}

.iconfont {
  font-size: 13px;
}
#out-table {
  border: 2px solid black;
}
</style>
