<template>
  <div class="content-main">
    <el-form
      :inline="true"
      :model="searchForm"
      ref="searchForm"
      class="search-bm-boder"
    >
      <el-row>
        <el-form-item label="委托编号:" prop="wtbh">
          <el-input
            clearable
            v-model="searchForm.wtbh"
            style="width: 170px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="委托单位:" prop="wtfmc">
          <el-input
            clearable
            v-model="searchForm.wtfmc"
            style="width: 170px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="品种:" prop="pzbm">
          <el-cascader
            style="width: 170px"
            :options="variety"
            :props="pzProps"
            clearable
            :show-all-levels="false"
            v-model="searchForm.pzbm"
            placeholder="请选择品种"
          ></el-cascader>
        </el-form-item> -->
        <el-form-item label="交易方向:" prop="jyfx">
          <el-select
            v-model="searchForm.jyfx"
            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="resetForm"
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-row>
      <el-row v-show="searchShowMoreBool">
        <el-form-item label="交易方式:" prop="jyfs">
          <el-select
            v-model="searchForm.jyfs"
            style="width: 170px"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in tradeTypesFs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="zt">
          <label slot="label">状&nbsp;&nbsp;&nbsp;态:</label>
          <el-select
            v-model="searchForm.zt"
            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-form-item prop="jyrq">
          <label slot="label">交易日期:</label>
          <el-date-picker
            v-model="searchForm.jyrq"
            style="width: 170px"
            type="date"
            placeholder="请选择"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row class="addBtnBg" v-if="buttonNameList.indexOf('新增') > -1">
      <el-button
        type="primary"
        class="iconfont iconxinzeng"
        size="small"
        @click="handleAddTodo"
        v-if="buttonNameList.indexOf('新增') > -1"
        >新增</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="140"
          label="委托编号"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="wtfmc"
          width="320"
          label="委托单位"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="jyfxmc"
          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="jyfsmc"
          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="wtrq"
          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="ztmc"
          label="状态"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="操作" width="147">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                @click="handleDetail(scope.row)"
                class="operate_show_btn iconfont iconchakan2"
                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.ztmc == '待提交' || scope.row.ztmc == '签章驳回'
                "
                type="primary"
                @click="handleUpdate(scope.row)"
                class="operate_edit_btn iconfont iconxiugai1"
                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.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"
            >
              <el-button
                v-if="
                  scope.row.ztmc == '待盖章' || scope.row.ztmc == '受理人已驳回'
                "
                @click="handleSeal(scope.row)"
                type="primary"
                class="operate_edit_btn iconfont icongaizhang"
                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.ztmc == '待受理人审核' ||
                  scope.row.ztmc == '审核人已驳回'
                "
                @click="handleAudit(scope.row)"
                type="primary"
                class="operate_edit_btn iconfont iconshenhe"
                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.ztmc == '待审核人审核'"
                @click="handleAudit(scope.row)"
                type="primary"
                class="operate_edit_btn iconfont iconshenhe"
                size="small"
              ></el-button>
            </el-tooltip>
            <!-- <el-tooltip class="item" effect="light" content="确认" placement="top">
                        <el-button v-if="scope.row.ztmc == '已提交'" @click="handleConfirm(scope.row)" type="primary" class="operate_show_btn iconfont icongaizhang" size="small"></el-button>
            </el-tooltip>-->
            <!-- <el-tooltip class="item" effect="light" content="发布" placement="top">
                        <el-button v-if="scope.row.ztmc == '已确认'" @click="handlePublish(scope.row)" type="primary" class="operate_show_btn iconfont icongaizhang" 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.ztmc == '已发布'"
                @click="handlePublished(scope.row)"
                type="primary"
                class="operate_show_btn iconfont iconxitonggonggao"
                size="small"
              ></el-button>
            </el-tooltip> -->
            <el-tooltip
              class="item"
              effect="light"
              content="导出"
              placement="top"
            >
              <el-button
                v-if="scope.row.ztmc != '待盖章' && scope.row.ztmc != '待提交'"
                @click="doExport(scope.row)"
                type="primary"
                class="operate_show_btn iconfont icondaochu11"
                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.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>
    <el-dialog
      title="选择交易方向和方式"
      :visible.sync="centerDialogVisible"
      :close-on-click-modal="false"
      width="30%"
      centerk
    >
      <el-form ref="addForm" :model="addForm" :rules="rules">
        <el-form-item prop="yslx">
          <el-radio-group style="width: 100%" v-model="addForm.jyfsAndlx">
            <el-radio class="radioSize" label="1">采购 竞价</el-radio>
            <el-radio class="radioSize" label="2">采购 挂牌</el-radio>
            <el-radio class="radioSize" label="3">销售 竞价</el-radio>
            <el-radio class="radioSize" label="4">销售 挂牌</el-radio>
            <el-radio class="radioSize" label="5">购销双向 竞价 挂牌</el-radio>
          </el-radio-group>
          <!-- <el-select
            clearable
            style="width: 100%"
            v-model="addForm.yslx"
            placeholder="请选择"
          >
            <el-option
              v-for="item in yslxList"
              :key="item.dictCode"
              :label="item.dictName"
              :value="item.dictCode"
            ></el-option>
          </el-select> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleAdd">确 认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  varietys,
  list,
  del,
} from "@/api/transaction/tradeEntrust/outInDepotEntrust/index.js";
import { dateTimeFormat, getParameter } from "@/utils/index";
import { authorityControl } from "@/api/menuList";
const status = {
  // ['91000048','91000008','91000001','91000004']
  91000001: "待提交",
  91000300: "待盖章",
  91000103: "签章驳回",
  91000400: "待受理人审核",
  91000401: "受理人已驳回",
  91000500: "待审核人审核",
  91000405: "审核人已驳回",
  91000600: "已确认",
};
export default {
  data() {
    return {
      pzProps: {
        label: "dictName",
        value: "dictCode",
        children: "subDataDictionarys",
        // checkStrictly: true,
        emitPath: false,
      },
      searchShowMoreBool: false,
      variety: [], // 品种列表
      tradeTypes: [
        {
          label: "采购",
          value: "0",
        },
        {
          label: "销售",
          value: "1",
        },
      ],
      tradeTypesFs: [
        {
          label: "竞价",
          value: "0",
        },
        {
          label: "挂牌",
          value: "1",
        },
        {
          label: "购销双向",
          value: "2",
        },
      ],
      types: Object.keys(status).map((item) => ({
        code: item,
        name: status[item],
      })),
      listLoading: false,
      searchForm: {
        page: 0,
        // zts: Object.keys(status),
        wtbh: "",
        wtfmc: "",
        pzbm: "",
        jylx: "",
        zt: "",
        jyrq: "",
        // zts:['91000048','91000008','91000001','91000004']
      },
      pageForm: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      buttonNameList: [],
      addForm: {
        jyfsAndlx: "1",
      },
      centerDialogVisible: false,
      rules: {},
    };
  },
  created() {
    this.getTypes();
    this.getresourceCode();
  },
  methods: {
    handleAddTodo() {
      this.centerDialogVisible = true;
      this.addForm.jyfsAndlx = "1";
    },
    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");
      });
    },
    resetForm() {
      this.$refs["searchForm"].resetFields();
      this.getList();
    },
    handlePublish(row) {
      this.$router.push({
        path: "publish",
        query: {
          state: 0,
          wtbm: row.wtbm,
        },
      });
    },
    handlePublished(row) {
      this.$router.push({
        path: "publish",
        query: {
          state: 1,
          wtbm: row.wtbm,
        },
      });
    },
    async handleDel(row) {
      await this.$confirm("此操作将删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      var res = await del(row.wtbm);
      if (res && res.infoCode === 200) {
        this.$message.success("删除成功！");
        this.getList();
        return;
      }
      this.$message.error(res.msg);
    },
    handleUpdate(row) {
      this.$router.push({
        name: "outInDepotEntrustUpdate",
        query: {
          state: 1,
          wtbm: row.wtbm,
          zt: row.zt,
        },
      });
    },
    handleAudit(row) {
      this.$router.push({
        name: "outInDepotEntrustAudit",
        query: {
          state: 5,
          wtbm: row.wtbm,
          zt: row.zt,
        },
      });
    },
    doExport(row) {
      this.$router.push({
        path: "detail",
        query: {
          state: 6,
          wtbm: row.wtbm,
        },
      });
    },
    handleSeal(row) {
      this.$router.push({
        name: "outInDepotEntrustConfirm",
        query: {
          state: 4,
          wtbm: row.wtbm,
          zt: row.zt,
        },
      });
    },
    handleDetail(row) {
      this.$router.push({
        path: "detail",
        query: {
          state: 2,
          wtbm: row.wtbm,
          zt: row.zt,
        },
      });
    },
    handleAdd() {
      this.$router.push({
        path: "add",
        query: {
          state: 0,
          jyfsAndlx: this.addForm.jyfsAndlx,
        },
      });
    },
    handleConfirm(row) {
      this.$router.push({
        path: "confirm",
        query: {
          state: 3,
          wtbm: row.wtbm,
        },
      });
    },
    getTypes() {
      varietys(11).then((res) => {
        this.variety = res.data;
      });
    },
    doSearch() {
      this.pageForm.pageNum = 1;
      var tbrq = this.searchForm.tbrq;
      this.getList();
    },
    async getList(type) {
      console.log(this.pageForm,"222");
      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",
            "zt",
            "jyrq",
            "wtbh",
            "wtfmc",
            "page",
          ]);
          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;
}
.radioSize {
  height: 30px;
  width: 40%;
}
</style>
