<template>
  <div class="content-main">
    <el-form :inline="true" :model="searchForm" class="search-bm-boder">
      <el-row>
        <el-form-item label="交易性质:" prop="jylx">
          <el-select
            v-model="searchForm.jylx"
            placeholder="请选择交易性质"
            clearable
            class="input-w-220"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select
            v-model="searchForm.tkzt"
            class="w170"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in ztOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            ></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-form>
    <!-- <el-row class="addBtnBg">
      <el-button
        type="primary"
        class="iconfont iconpiliangweicaiyong"
        size="small"
        @click="handleAdd"
        :disabled="!tableData.length"
      >批量退款</el-button>
    </el-row>-->
    <el-row>
      <el-table
        :data="tableData"
        stripe
        v-loading="listLoading"
        border
        fit
        id="out-table"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <!-- <el-table-column align="center" type="selection" width="55"></el-table-column> -->
        <el-table-column
          align="center"
          label="序号"
          width="70"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="apcc"
          label="场次"
          show-overflow-tooltip
        ></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="jyrq"
          label="交易日期"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.jyrq && scope.row.jyrq.slice(0, 10) }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="pzmc"
          label="品种"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="sl"
          label="数量"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jybzj"
          label="交易保证金"
          show-overflow-tooltip
        >
        <template slot-scope="scope">
          {{
            String(scope.row.jybzj).indexOf(".") > -1
              ? Number(scope.row.jybzj).toFixed(2)
              : scope.row.jybzj
          }}
        </template>
      </el-table-column>
        <el-table-column
          align="center"
          prop="lybzj"
          label="履约保证金"
          show-overflow-tooltip
        >
        <template slot-scope="scope">
          {{
            String(scope.row.lybzj).indexOf(".") > -1
              ? Number(scope.row.lybzj).toFixed(2)
              : scope.row.lybzj
          }}
        </template>
      </el-table-column>
        <el-table-column
          align="center"
          prop="tkztmc"
          label="状态"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="操作" width="140">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="light"
              content="查看退款"
              placement="top"
            >
              <el-button
                type="primary"
                @click="handleDetail(scope.row, 'show')"
                class="operate_show_btn iconfont iconchakan2"
                size="small"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="light"
              content="退款"
              placement="top"
            >
              <el-button
                v-if="scope.row.tkzt == ztOptions[4].value"
                type="primary"
                @click="handleDetail(scope.row, 'edit')"
                class="operate_edit_btn iconfont iconbiangeng"
                size="small"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </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 {
  varietys,
  list,
  del,
} from "@/api/transaction/tradeEntrust/goodsSourceTable/index.js";
import {
  enumStatus,
  getMarginRefundList,
  selectPassBackPageList,
} from "@/api/transaction/ensureManage";
import { dateTimeFormat } from "@/utils/index";
import { authorityControl } from "@/api/menuList";
import { status } from "@/utils/statusList";

export default {
  data() {
    return {
      searchShowMoreBool: false,
      variety: [], // 品种列表
      ztOptions: [
        status.djyjb,
        status.djysh,
        status.dcwsp,
        status.djssh,
        status.dcwqp,
        status.wcgtk,
      ],
      options: [
        {
          value: 0,
          label: "采购",
        },
        {
          value: 1,
          label: "销售",
        },
      ],
      types: Object.keys(status).map((item) => ({
        code: item,
        name: status[item],
      })),
      listLoading: false,
      searchForm: {
        // page: 0,
        // zts: Object.keys(status),
        apcc: "",
        dwmc: "",
        tkzt: null,
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      buttonNameList: [],
    };
  },
  created() {
    sessionStorage.removeItem("mxbt");
    sessionStorage.removeItem("mxzw");
    sessionStorage.removeItem("mxfbr");
    this.getTypes();
    this.getresourceCode();
  },
  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.getList("start");
      });
    },
    setPublishDetail(row) {
      if (!row.mxbt) return;
      sessionStorage.setItem("mxbt", row.mxbt);
      sessionStorage.setItem("mxfbr", row.mxfbr);
      sessionStorage.setItem("mxzw", row.mxzw);
    },
    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);
    },
    handleDetail(row, type) {
      this.$router.push({
        path: "detail",
        query: {
          state: type,
          tkbm: row.tkbm,
          ccbm: row.ccbm,
        },
      });
    },
    // handleAdd() {
    //   this.$router.push({
    //     path: "add",
    //     query: {
    //       state: 0
    //     }
    //   });
    // },
    handleConfirm(row) {
      this.$router.push({
        path: "confirm",
        query: {
          state: 3,
          wtbm: row.wtbm,
          jyrq: row.jyrq,
        },
      });
    },
    getTypes() {
      varietys(11).then((res) => {
        this.variety = res.data;
      });
    },
    reSearch() {
      this.searchForm = {
        // page: 0,
        // zts: Object.keys(status),
        // apcc: "",
        // dwmc: "",
        tkzt: null,
      };
      this.doSearch();
    },
    doSearch() {
      this.pageForm.pageNum = 1;
      var tbrq = this.searchForm.tbrq;
      this.searchForm.tkzt == ""
        ? (this.searchForm.tkzt = null)
        : (this.searchForm.tkzt = this.searchForm.tkzt);
      this.getList();
    },
    getList(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 params = {
        pageIndex: this.pageForm.pageNum,
        pageSize: this.pageForm.pageSize,
        // wthbh: self.searchForm.wthbh,
        // wtlx: self.searchForm.wtlx,
        // tkfl: 1, // 未成交
        pageType: 5, // 业务端
        ...this.searchForm,
      };
      this.listLoading = true;
      selectPassBackPageList(params).then((response) => {
        this.listLoading = false;
        if (response.infoCode == 200) {
          this.tableData = response.data.items;
          this.pageForm.total = response.data.total;
        }
      });
      // getMarginRefundList(params)
      //   .then(actionMethod)
      //   .catch(actionMethod);

      // function actionMethod(response) {
      //   self.listLoading = false;
      //   if (response && response.infoCode === 200) {
      //     self.tableData = response.data.items;
      //     self.pageForm.total = response.data.total;
      //   } else {
      //     self.tableData = [];
      //     self.pageForm.total = 0;
      //   }
      // }
    },
    // async getList() {
    // 	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 res = await list({
    // 		...this.searchForm,
    // 		pageNum,
    // 		pageSize,
    // 		page: 2,
    // 	})
    // 	this.listLoading = false
    // 	if (res && res.infoCode === 200) {
    // 		this.pageForm.total = res.data.total
    // 		this.tableData = res.data.items
    // 		return
    // 	}
    // 	this.pageForm.total = 0
    // 	this.tableData = []
    // },
    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;
}
.w170 {
  width: 170px !important;
}
</style>
