<template>
  <div class="oing pagePadding">
    <ex-table
      v-loading="loading"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '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">
            <el-col :span="24">
              <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((item) => item == '003')"
              >
                <el-button
                  :size="searchSize"
                  type="success"
                  style="float: right"
                  @click="openModal('add', false)"
                >
                  添加
                </el-button>
              </el-col>
            </el-col>
          </el-row>
        </div>
      </template>
      <!-- <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template> -->

      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((item) => item == '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() {
      let tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "oing") ||
        {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      process_router_list: JSON.parse(JSON.stringify(routerList)),
      loading: true,

      modelId: 0,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  mounted() {
    console.log(this.process_router_list);
    this.searchProList();
    this.searchList();
  },
  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,
        };

        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },

    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        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>
