<template>
  <div class="unRead pagePadding">
    <ex-table
      v-loading="loading"
      v-if=" powers.some((i) => i == '001')"
      :table="table"
      :data="tableData"
      :columns="readUnRead"
      :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="{ selection }">
        <el-row :gutter="10" style="padding: 0 0 0 80px; width: 100%">
          <el-col :span="10" style="width: 230px">
            <el-select
              clearable
              :size="'mini'"
              style="width: 100%"
              v-model="parmValue.order_type"
              placeholder="订单类型"
              @click="
                pageInfo.curr = 1;
                parmValue.page = 1;
                searchList();
              "
            >
              <el-option
                v-for="item in process_router_list"
                :key="item.type"
                :label="item.name"
                :value="item.type"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="10" style="width: 400px; padding: 0 0 0 10px">
            <el-input
              :size="'mini'"
              v-model="parmValue.orderCode"
              placeholder="订单编号"
               maxlength="40"
              @blur="
                pageInfo.curr = 1;
                parmValue.page = 1;
                searchList();
              "
            ></el-input>
          </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: 90px; float: right">
            <el-button
              type="primary"
              :size="searchSize"
              style="float: right"
              @click="changeStatus(selection, '全部标记为已读', 0)"
            >
              全部已读
            </el-button>
          </el-col>
          <el-col :span="3" style="width: 66px; float: right">
            <el-button
              type="primary"
              :size="searchSize"
              style="float: right"
              @click="changeStatus(selection, '批量标记为已读', 1)"
            >
              批量已读
            </el-button>
          </el-col>
        </el-row>
      </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((i) => i == '007')"
          effect="dark"
          content="详情"
          placement="top"
        >
          <i class="el-icon-view tb-icon" @click="wentRead(scope.row)"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/process/unRead";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { routerList, columns,readUnRead } 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(
          (item) => item.menu_route == "unRead"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      process_router_list: JSON.parse(JSON.stringify(routerList)),
      loading: true,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        is_read: "0",
        order_type: "",
        orderCode: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          type: "selection",
          fixed: "left",
          _noset_: true,
        },
        
        ...columns,
      ],

      readUnRead,
    };
  },
  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.columns.unshift();
    this.searchList();
  },
  methods: {
    getRouter(row) {
       console.log(row)
      const { order_type, order_id, orderCode } = 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" ? orderCode : order_id,
        };
        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("暂未找到相关流程！");
      }


      // if (toRouter && queryId) {
      //   let model = {
      //     type: "view",
      //     id: queryId,
      //   };
      //    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("暂未找到相关流程！");
      // }
    },

    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        is_read: "0",
        order_type: "",
        orderCode: "",
      };
      this.searchList();
    },
    /**
     * 启用/禁用
     * @param {String} id id
     * @param {String} status 0-禁用 1-启用
     */
    async changeStatus(selection, title, type) {
      if (type === 1 && selection && selection.length === 0) {
        this.$message.warning("至少选择一条数据！");
        return;
      }
      await this.$confirm(`确定要${title}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (type === 1) {
            let sitem = { id: [] };
            selection.forEach((a) => {
              sitem.id.push(a.id);
            });
            await this.set_status(title, type, sitem);
          } else {
            await this.set_status(title, type, {});
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async set_status(title, type, sitem) {
      let res = {};
      if (type === 1) {
        res = await asyncRequest.status(sitem);
      } else {
        res = await asyncRequest.allStatus({});
      }
      const { code } = res;
      if (code === 0) {
        this.$notify.success({
          title: title + "成功！",
          message: "",
        });
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        this.searchList();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },

    async wentRead(item) {
      console.log(item)
      // return;
      const { toRouter, queryId, id ,orderCode} = item;

      const { code, data, message } = await asyncRequest.detail({ id: id });
      if (code === 0) {
        console.log(toRouter)
        this.getRouter(item);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },

    // 刷新表格
    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].queryId =
              to == "code" ? v.order_code : v.order_id;

             
          } else {
            this.tableData[i].process_name = "";
            this.tableData[i].toRouter = "";
            this.tableData[i].queryId = "";
          }
        });
        //  console.log("1111111111",this.process_router_list)
        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;
    },
  },
};
</script>

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