<template>
  <div class="content-main">
    <div class="search-bm-boder">
      <el-row>
        <el-form
          :model="form"
          label-width="80px"
          ref="searchForm"
          :inline="true"
          class="demo-form-inline search-form"
        >
          <el-form-item label="任务名称:" prop="itemName">
            <el-input
              placeholder="请输入"
              v-model="form.itemName"
              class="inputCompatible"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="发起人:" prop="submitter">
            <el-input
              placeholder="请输入"
              v-model="form.submitter"
              class="inputCompatible"
              clearable
            ></el-input>
          </el-form-item>
          <div class="search-button">
            <span class="search-line"></span>
            <!--          <div class="search-select" @click="searchShowMore">更多条件 <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="search"
              size="small"
              class="iconfont iconchaxun"
              >查询</el-button
            >
            <el-button
              type="primary"
              @click="resetInfo('searchForm')"
              size="small"
              class="iconfont iconzhongzhi"
              >重置</el-button
            >
          </div>
        </el-form>
      </el-row>
    </div>

    <el-table
      :data="tableData"
      border
      stripe
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="itemName"
        label="任务名称"
        sortable
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="companyName"
        label="发起单位"
        sortable
        width="240"
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="submitter"
        label="发起人"
        sortable
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="itemStatusName"
        label="状态"
        sortable
        width="120"
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="reportDate"
        label="发起时间"
        sortable
        width="140"
      ></el-table-column>
      <!-- <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="assigner"
        label="交办人"
        sortable
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="assignTime"
        label="交办时间"
        sortable
      ></el-table-column>-->
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="light"
            content="办理"
            placement="top"
            v-if="scope.row.itemUrl"
          >
            <el-button
              type="primary"
              @click="manage(scope.row)"
              size="small"
              class="operate_edit_btn iconfont iconxiugai1"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <div class="pag-right clearFix">
      <el-pagination
        class="right"
        :current-page.sync="limit.page"
        :page-size="limit.size"
        :page-sizes="[10, 20, 30, 40]"
        :total="limit.total"
        @current-change="handleCurrentChangeTask"
        @size-change="handleSizeChangeTask"
        layout="total, sizes, prev, pager, next, jumper"
        v-if="paginationShow"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getDaiBanPagesNew,
  goPickTask,
} from "@/api/publicfunction/taskManager";
import { todoList } from "@/api/taskReport/index";
export default {
  data() {
    return {
      tableData: [],
      form: {
        itemName: "",
        submitter: "",
        userId: "",
      },
      limit: {
        page: 1,
        size: 10,
        total: 0,
      },
      paginationShow: true,
      listLoading: false,
      systeamList: [
        {
          value: "cbl",
          name: "储备粮管理",
        },
        {
          value: "ywjc",
          name: "业务检查",
        },
        {
          value: "zj",
          name: "质量管理",
        },
        {
          value: "qzzrz",
          name: "区长责任制",
        },
        {
          value: "jy",
          name: "粮油交易",
        },
        {
          value: "pm",
          name: "项目管理",
        },
        {
          value: "xxsh",
          name: "信息审核备案",
        },
        {
          value: "safe",
          name: "安全生产",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    resetInfo(formName) {
      this.$refs[formName].resetFields();
      this.form.submitter = "";
      this.form.itemName = "";
      this.limit.page = 1;
      this.limit.size = 10;
      this.getList();
    },
    getSysteamName(code) {
      let name = "";
      this.systeamList.forEach((item) => {
        if (item.value == code) {
          name = item.name;
        }
      });
      return name;
    },
    //办理
    manage(row) {
      if (row.sysCode == "ywjc") {
        if (row.itemStatusName == "待分配" || row.itemStatusName == "待检查") {
          let nameStrArray = row.itemId.split("-");
          let rwId = nameStrArray[0];
          let jgId = nameStrArray[0];
          let jhId = nameStrArray[1];
          let idKeyName = row.itemQuery.idKey;
          row.itemQuery = JSON.parse(
            JSON.stringify(row.itemQuery).replace(/idKey/g, row.itemQuery.idKey)
          );
          switch (idKeyName) {
            case "rwId":
              row.itemQuery[idKeyName] = rwId;
              if (row.itemQuery.jhId) {
                row.itemQuery.jhId = jhId;
              }
              break;
            case "jhId":
              row.itemQuery[idKeyName] = jhId;
              if (row.itemQuery.rwId) {
                row.itemQuery.rwId = rwId;
              }
              break;
            case "jgId":
              row.itemQuery[idKeyName] = jgId;
              if (row.itemQuery.rwId) {
                row.itemQuery.rwId = rwId;
              }
              break;
          }
        } else {
          if (row.itemStatusName == "待企业上传") {
            let nameStrArray = row.itemId.split("-");
            let jgId = nameStrArray[0];
            let rwId = nameStrArray[0];

            row.itemQuery = JSON.parse(
              JSON.stringify(row.itemQuery.management).replace(
                /idKey/g,
                row.itemQuery.management.idKey
              )
            );
            row.itemQuery.object = JSON.stringify({ jgId: jgId });
            if (row.itemQuery.rwId) {
              row.itemQuery.rwId = rwId;
            }
            row.itemUrl = row.itemUrl.split(",")[1];
          } else if (
            row.itemStatusName == "待审核" &&
            row.itemQuery.idKey == "id"
          ) {
            // let nameStrArray = row.itemId.split("-");
            row.itemId = row.itemId.substring(0, row.itemId.length - 2);
            if (row.itemQuery) {
              let idKeyName = row.itemQuery.idKey;
              row.itemQuery = JSON.parse(
                JSON.stringify(row.itemQuery).replace(
                  /idKey/g,
                  row.itemQuery.idKey
                )
              );
              row.itemQuery[idKeyName] = row.itemId;
            }
          } else {
            let nameStrArray = row.itemId.split("-");
            row.itemId = nameStrArray[0];
            if (row.itemQuery) {
              let idKeyName = row.itemQuery.idKey;
              row.itemQuery = JSON.parse(
                JSON.stringify(row.itemQuery).replace(
                  /idKey/g,
                  row.itemQuery.idKey
                )
              );
              row.itemQuery[idKeyName] = row.itemId;
            }
          }
        }
      } else {
        if (row.sysCode == "jy" && row.itemId.indexOf("-") != -1) {
          row.itemId = row.itemId.substring(0, row.itemId.indexOf("-"));
        }
        if (row.sysCode == "cbl" && row.itemId.indexOf("-") != -1) {
          row.itemId = row.itemId.substring(0, row.itemId.indexOf("-"));
        }
        if (row.sysCode == "xxsh" && row.itemId.indexOf("-") != -1) {
          row.itemId = row.itemId.substring(0, row.itemId.indexOf("-"));
        }
        if (row.sysCode == "safe" && row.itemId.indexOf("-") != -1) {
          row.itemId = row.itemId.substring(0, row.itemId.indexOf("-"));
        }
        if (row.itemQuery) {
          if (row.itemId.indexOf(",") != -1) {
            row.itemId = row.itemId.split(",");
            row.itemQuery.idKey = row.itemQuery.idKey.split(",");
            row.itemQuery.idKey.map((item, index) => {
              row.itemQuery[row.itemQuery.idKey[index]] = row.itemId[index];
            });
          } else {
            row.itemQuery[row.itemQuery.idKey] = row.itemId;
          }
        }
      }
      getLeftMenuList(this.getSysteamName(row.sysCode));
      sessionStorage.setItem("searchData", null);
      let query = "";
      if (row.itemUrl) {
        if (row.itemQuery) {
          for (let key in row.itemQuery) {
            query += key + "=" + row.itemQuery[key] + "&";
          }
          query += "db=1";
          window.location.href =
            location.host == "localhost:9000"
              ? "/#" + row.itemUrl + "?" + query
              : "/web/portal/#" + row.itemUrl + "?" + query;
        } else {
          window.location.href =
            location.host == "localhost:9000"
              ? "/#" + row.itemUrl
              : "/web/portal/#" + row.itemUrl;
        }
      }
    },
    pickUpTaskFun(row) {
      //拾取任务
      goPickTask(row.taskId).then((res) => {
        if (res.infoCode == 200) {
          this.$router.push({
            path: "/publicfunction/toDoQualification",
            query: {
              taskId: row.taskId,
              procInstId: row.procInstId,
              userCreditCode: row.userCreditCode,
              creditCode: row.creditCode,
              zgType: row.procName,
              actName: row.actName,
            },
          });
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 2000,
          });
        }
      });
    },
    handleCurrentChangeTask(val) {
      this.limit.page = val;
      this.getList();
    },
    handleSizeChangeTask(val) {
      this.limit.size = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      let data = {
        pageSize: this.limit.size,
        pageNum: this.limit.page,
        itemName: this.form.itemName ? this.form.itemName : "",
        submitter: this.form.submitter ? this.form.submitter : "",
        receiver: sessionStorage.getItem("userCode"),
        orgCode: sessionStorage.getItem("orgCode"),
      };
      todoList(data).then((res) => {
        if (res) {
          this.tableData = res.items;
          this.limit.total = res.total;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 2000,
          });
        }
        this.listLoading = false;
      });
    },
    //查询
    search() {
      this.paginationShow = false;
      this.handleCurrentChangeTask(1);
      this.$nextTick(function () {
        this.paginationShow = true;
      });
    },
  },
};
</script>
<style>
</style>
