<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"
      v-loading="listLoading"
      border
      stripe
      highlight-current-row
      @row-click="tableRowClick"
      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="100"
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="reportDate"
        label="发起时间"
        sortable
        width="140"
      ></el-table-column>
      <!-- <el-table-column label="操作" align="center" width="80">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" content="查看" placement="top">
            <el-button
              type="primary"
              @click="check(scope.row)"
              size="small"
              class="operate_show_btn iconfont iconchakan2"
            ></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 { getYiBanPages } from "@/api/publicfunction/taskManager";
import { haveDoneList } from "@/api/taskReport/index";

export default {
  data() {
    return {
      listLoading: false,
      paginationShow: true,
      tableData: [],
      form: {
        bbq: "",
        itemName: "",
        submitter: "",
        userId: ""
      },
      limit: {
        page: 1,
        size: 10,
        total: 0
      },
      systeamList: [
        {
          value: "cbl",
          name: "储备粮管理"
        },
        {
          value: "ywjc",
          name: "业务检查"
        },
        {
          value: "zj",
          name: "质量管理"
        },
        {
          value: "qzzrz",
          name: "区长责任制"
        },
        {
          value: "jy",
          name: "粮油交易"
        },
        {
          value: "pm",
          name: "项目管理"
        },
        {
          value: "xxsh",
          name: "信息审核备案"
        }
      ]
    };
  },
  created() {
    this.getDoneList();
  },
  methods: {
    resetInfo(formName) {
      this.$refs[formName].resetFields();
      this.form.submitter = '';
      this.form.itemName = '';
      this.limit.page = 1;
      this.limit.size = 10;
      this.getDoneList();
    },
    tableRowClick(row, column, event) {
      if (event.label != "操作") {
        this.check(row);
      }
    },
    getSysteamName(code) {
      let name = "";
      this.systeamList.forEach(item => {
        if (item.value == code) {
          name = item.name;
        }
      });
      return name;
    },
    //查看
    check(row) {
      if (row.sysCode == "ywjc") {
        if (row.itemStatusName == "待分配" || row.itemStatusName == "待检查" || row.itemStatusName == "已下发") {
          let nameStrArray = row.itemId.split("-");
          let rwId = nameStrArray[0];
          let jgId = nameStrArray[0];
          let jhId = nameStrArray[1];
          let idKeyName = row.itemQueryHaveDone.idKey;
          row.itemQueryHaveDone = JSON.parse(
            JSON.stringify(row.itemQueryHaveDone).replace(/idKey/g, row.itemQueryHaveDone.idKey)
          );
          switch (idKeyName) {
            case "rwId":
              row.itemQueryHaveDone[idKeyName] = rwId;
              if (row.itemQueryHaveDone.jhId) {
                row.itemQueryHaveDone.jhId = jhId;
              }
              break;
            case "jhId":
              row.itemQueryHaveDone[idKeyName] = jhId;
              if (row.itemQueryHaveDone.rwId) {
                row.itemQueryHaveDone.rwId = rwId;
              }
              break;
            case "jgId":
              row.itemQueryHaveDone[idKeyName] = jgId;
              if (row.itemQueryHaveDone.rwId) {
                row.itemQueryHaveDone.rwId = rwId;
              }
              break;
          }
        }
        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.itemQueryHaveDone) {
            let idKeyName = row.itemQueryHaveDone.idKey;
            row.itemQueryHaveDone = JSON.parse(
              JSON.stringify(row.itemQueryHaveDone).replace(
                /idKey/g,
                row.itemQueryHaveDone.idKey
              )
            );
            row.itemQueryHaveDone[idKeyName] = row.itemId;
          }
        }
      }

      if (row.sysCode == "pm" || row.sysCode == "qzzrz") {
        getLeftMenuList(this.getSysteamName(row.sysCode));
        sessionStorage.setItem("searchData", null);
        if(row.sysCode == "qzzrz" && row.itemUrl == "/accountability/taskAudit" && !row.itemUrlHaveDone){
          row.itemUrlHaveDone = "/accountability/selfAudit"
        }
        window.location.href =
          location.host == "localhost:9000"
            ? "/#" + row.itemUrlHaveDone
            : "/web/portal/#" + row.itemUrlHaveDone;
      } 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.itemId.indexOf(",") != -1) {
          row.itemId = row.itemId.split(",");
          row.itemQueryHaveDone.idKey = row.itemQueryHaveDone.idKey.split(",");
          row.itemQueryHaveDone.idKey.map((item, index) => {
            row.itemQueryHaveDone[row.itemQueryHaveDone.idKey[index]] =
              row.itemId[index];
          });
        } else {
          row.itemQueryHaveDone[row.itemQueryHaveDone.idKey] = row.itemId;
        }
        getLeftMenuList(this.getSysteamName(row.sysCode));
        sessionStorage.setItem("searchData", null);
        let query = "";
        for (let key in row.itemQueryHaveDone) {
          query += key + "=" + row.itemQueryHaveDone[key] + "&";
        }
        query += "db=1"

        window.location.href =
          location.host == "localhost:9000"
            ? "/#" + row.itemUrlHaveDone + "?" + query
            : "/web/portal/#" + row.itemUrlHaveDone + "?" + query;
      }
    },
    getDoneList() {
      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 : '',
        handlerId: sessionStorage.getItem("userCode"),
        receiverId: sessionStorage.getItem("userCode")
      };
      haveDoneList(data).then(res => {
        if (res) {
          this.tableData = res.items;
          this.limit.total = res.total;
        } else {
          this.$message({
            message: res.msg,
            type: "error"
          });
        }
        this.listLoading = false;
      });
    },
    //查询
    search() {
      this.paginationShow = false;
      this.handleCurrentChangeTask(1);
      this.$nextTick(function() {
        this.paginationShow = true;
      });
    },
    handleCurrentChangeTask(val) {
      this.limit.page = val;
      this.getDoneList();
    },
    handleSizeChangeTask(val) {
      this.limit.size = val;
      this.getDoneList();
    }
  }
};
</script>
<style>
</style>
