<template>
  <div class="content-main">
    <el-form :inline="true" :model="searchForm" class="search-bm-boder">
      <el-row>
        <el-form-item label="委托方:">
          <el-input
            clearable
            v-model="searchForm.wtfmc"
            style="width: 170px"
            placeholder="请输入委托方"
          ></el-input>
        </el-form-item>
        <el-form-item 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 label="交易性质:">
          <el-select
            v-model="searchForm.jylx"
            style="width: 170px"
            clearable
            placeholder="请选择交易性质"
          >
            <el-option
              v-for="item in tradeTypes"
              :key="item.value"
              :label="item.label"
              :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-row v-show="searchShowMoreBool">
        <el-form-item>
          <label slot="label">状&nbsp;&nbsp;&nbsp;态:</label>
          <el-select
            v-model="searchForm.hybzt"
            style="width: 170px"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in types"
              :key="item.name"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </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-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"
          label="序号"
          width="70"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="wtbh"
          width="170"
          label="委托编号"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button @click="handleDetail(scope.row)" type="text">{{
              scope.row.wtbh
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="wtfmc"
          label="委托方"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jylxmc"
          label="交易性质"
          show-overflow-tooltip
        ></el-table-column>
        <!-- <el-table-column align="center" prop="jsfsmc" label="损耗计算方式" show-overflow-tooltip></el-table-column> -->
        <el-table-column
          align="center"
          prop="pzmc"
          label="品种"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jyrq"
          label="交易日期"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jgrq"
          label="交割日期"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="hybztmc"
          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="handlePlanDetail(scope.row)"
                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.hybztmc == '待安排'" type="primary" @click="handlePlan(scope.row)" class="operate_edit_btn iconfont iconxiugai" size="small"></el-button>
            </el-tooltip>-->
            <!-- <el-tooltip class="item" effect="light" content="提交" placement="top">
                        <el-button v-if="scope.row.hybztmc == '待提交'" type="primary" @click="handleUpdate(scope.row)" class="operate_edit_btn iconfont icontijiao" 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.hybztmc == '已提交'"
                @click="handleConfirm(scope.row)"
                type="primary"
                class="operate_show_btn iconfont iconqueren"
                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.hybztmc == '已确认' ||
                  scope.row.hybztmc == '清单已撤回'
                "
                @click="handlePublish(scope.row)"
                type="primary"
                class="operate_show_btn iconfont iconfabu"
                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.hybztmc == '已发布'"
                @click="handlePublished(scope.row)"
                type="primary"
                class="operate_show_btn iconfont iconshengchenghuizongbiao"
                size="small"
              ></el-button>
            </el-tooltip>
            <!-- <el-tooltip class="item" effect="light" content="公告" placement="top">
                        <el-button v-if="scope.row.ztmc == '已发布'" @click="handlePublished(scope.row)" type="primary" class="operate_show_btn iconfont iconxitonggonggao" 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 { dateTimeFormat, getParameter } from "@/utils/index";
import { authorityControl } from "@/api/menuList";
const status = {
  // ['91000048','91000008','91000001','91000004']
  91000048: "已发布",
  91000008: "已驳回",
  // 91000001: '待提交',
  91000004: "已提交",
  // 91000049: '待安排',
  91000018: "已确认",
  91000051: "清单已撤回",
};
export default {
  data() {
    return {
      searchShowMoreBool: false,
      variety: [], // 品种列表
      tradeTypes: [
        {
          label: "不限",
          value: null,
        },
        {
          label: "采购",
          value: "0",
        },
        {
          label: "销售",
          value: "1",
        },
      ],
      // tradeTypes: [{
      //     label: '竞购',
      //     value: '0'
      // }, {
      //     label: '竞销',
      //     value: '1'
      // }],
      types: Object.keys(status).map((item) => ({
        code: item,
        name: status[item],
      })),
      listLoading: false,
      searchForm: {
        page: 2,
        zts: Object.keys(status),
        jylx: 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);
    },
    handlePublish(row) {
      this.setPublishDetail(row);
      this.$router.push({
        path: "publish",
        query: {
          state: 0,
          wtbm: row.wtbm,
          jylx: row.jylx,
          jyrq: row.jyrq,
          lsxzbm: row.lsxzbm,
        },
      });
    },
    handlePublished(row) {
      this.setPublishDetail(row);
      this.$router.push({
        path: "publish",
        query: {
          state: 1,
          wtbm: row.wtbm,
          jyrq: row.jyrq,
        },
      });
    },
    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);
    },
    handlePlan(row) {
      this.$router.push({
        path: "plan",
        query: {
          state: 0,
          wtbm: row.wtbm,
          jylx: row.jylx,
          lsxzbm: row.lsxzbm,
        },
      });
    },
    handleUpdate(row) {
      this.$router.push({
        path: "plan",
        query: {
          state: 1,
          wtbm: row.wtbm,
          jylx: row.jylx,
        },
      });
    },
    handleDetail(row) {
      this.$router.push({
        path: "detail",
        query: {
          state: 2,
          wtbm: row.wtbm,
        },
      });
    },
    handlePlanDetail(row) {
      this.$router.push({
        path: "plan",
        query: {
          state: 2,
          wtbm: row.wtbm,
        },
      });
    },
    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,
          lsxzbm: row.lsxzbm,
          wtfbm: row.wtfbm,
        },
      });
    },
    getTypes() {
      varietys(11).then((res) => {
        this.variety = res.data;
      });
    },
    reSearch() {
      this.searchForm = {
        page: 2,
        zts: Object.keys(status),
        jylx: null,
      };
      this.doSearch();
    },
    doSearch() {
      this.pageForm.pageNum = 1;
      var tbrq = this.searchForm.tbrq;
      this.getList();
    },
    async getList(type) {
      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 data = {
        ...this.searchForm,
        pageNum,
        pageSize,
      };
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(data));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          let searchData = JSON.parse(sessionStorage.getItem("searchData"));
          this.searchForm = getParameter(searchData, [
            "pzbm",
            "jylx",
            "wtfmc",
            "page",
            "zts",
          ]);
          this.pageForm = getParameter(searchData, ["pageNum", "pageSize"]);
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(data));
        }
      }
      let res = await list(JSON.parse(sessionStorage.getItem("searchData")));
      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;
}
</style>
