<template>
  <div class="oing pagePadding">
    <ex-table
      v-loading="loading"
      v-if="powers.some((i) => i == '001')"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%; height: 30px">
          <el-row :gutter="10" style="padding-left: 80px">
            <!-- <el-col :span="7" style="width: 350px">
              <period-date-picker
                :type="1"
                :width="'155px'"
                :size="searchSize"
                :start="parmValue.start"
                :end="parmValue.end"
                @timeReturned="handleTime"
              />
            </el-col> -->
            <!-- <el-col :span="5" style="width: 205px; float: left">
              <el-select
                v-model="parmValue.order_code"
                :size="'mini'"
                style="width: 205px"
                placeholder="流程名称"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in routerList"
                  :key="'status' + item.type"
                  :label="item.name"
                  :value="item.type"
                />
              </el-select>
            </el-col> -->
            <el-col :span="3" style="width: 66px; float: right">
              <el-button
                type="primary"
                :size="searchSize"
                style="float: right"
                @click="searchList"
              >
                刷新
              </el-button>
            </el-col>
            <el-col
              :span="3"
              style="width: 66px; float: right"
              v-if="powers.some((i) => i == '003')"
            >
              <el-button
                :size="searchSize"
                type="success"
                style="float: right"
                @click="openModal('add', false)"
              >
                添加
              </el-button>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #current_handle_name="{ scope }">
          {{scope.row.handle_user_list ? scope.row.handle_user_list.join(',') : ''}}
      </template>

      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((i) => i == '007')"
          effect="dark"
          content="详情"
          placement="top"
        >
          <i class="el-icon-view tb-icon" @click="getRouter(scope.row)"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/process/oing";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { routerList, columns } from "@/views/process/columns";
export default {
  name: "role",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran = this.$store.getters.btnList.find((i) => i.menu_route == "oing") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      process_router_list: JSON.parse(JSON.stringify(routerList)),
      loading: true,
      routerList,
      modelId: 0,
      parmValue: {
        // xiugai1: "",
        // xiugai2: "",
        // start: "",
        order_code: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      const { page, size } = this.parmValue;
      this.pageInfo = {
        size: size,
        curr: page,
        total: 0,
      };
    }
    this.searchList();

    // this.searchProList();
  },
  methods: {
    getRouter(row) {
      const { order_type, order_id, order_code } = row;
      let index = this.process_router_list.findIndex((y) => y.type === order_type);
      if (index !== -1) {
        const { toRouter, to } = this.process_router_list[index];
        let model = {
          type: "view",
          id: to === "code" ? order_code : order_id,
        };

        console.log(this.parmValue);
        let routerModel = {
          options: JSON.parse(JSON.stringify(this.parmValue)),
          router: this.$route.path,
        };
        model.preModel = JSON.stringify(routerModel);
        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },
    // async handleTime(e) {
    //   this.parmValue.start = e.startTime || "";
    //   this.parmValue.end = e.endTime || "";
    //   if (
    //     (this.parmValue.start !== "" && this.parmValue.end !== "") ||
    //     (this.parmValue.start === "" && this.parmValue.end === "")
    //   ) {
    //     this.pageInfo.curr = 1;
    //     this.parmValue.page = 1;
    //     await this.searchList();
    //   }
    // },

    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        // xiugai1: "",
        // xiugai2: "",
        // start: "",
        order_code: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // this.searchProList();
      this.searchList();
    },

    // 刷新表格
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        // this.tableData.forEach((v, i) => {
        //   let index = this.process_router_list.findIndex(
        //     (y) => y.type === v.order_type
        //   );
        //   if (index !== -1) {
        //     const { name, toRouter, to } = this.process_router_list[index];
        //     this.tableData[i].process_name = name;
        //     this.tableData[i].toRouter = toRouter;
        //     this.tableData[i].typetoRouter = toRouter;
        //     this.tableData[i].queryId =
        //       to == "code" ? v.order_code : v.order_id;
        //   } else {
        //     this.tableData[i].process_name = "";
        //     this.tableData[i].toRouter = "";
        //     this.tableData[i].queryId = "";
        //   }
        // });
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    // 刷新表格
    async searchProList() {
      this.loading = true;
      const res = await asyncRequest.processList({
        page: 1, // 页码
        size: 50, // 每页显示条数
      });
      if (res && res.code === 0 && res.data) {
        console.log(res.data);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
