<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--机构数据-->
      <!-- <el-col :span="4" :xs="24">
        <org-tree
          v-model="queryParams.orgId"
          @defaultOrg="getDefaultOrg"
          @checkChange="checkChange"
          @click="clickTreeNode"
        ></org-tree>
      </el-col> -->
      <el-col :span="24" :xs="24">
        <div class="main-right-box">
          <!--    搜索条件    -->
          <div class="main-search-box">
            <el-form
              :model="queryParams"
              ref="queryParams"
              size="small"
              :inline="true"
              v-show="showSearch"
            >
              <el-form-item label="履职机构">
                  <org-tree
                    v-model="queryParams.orgId"
                    @defaultOrg="getDefaultOrg"
                    @checkChange="checkChange"
                    @click="clickTreeNode"
                  ref="orgTree"
                  ></org-tree>
              </el-form-item>

              <!-- <el-row>
                <el-col :span="5"> -->
              <el-form-item label="任务名称" prop="planId">
                <el-select
                  v-model="queryParams.planId"
                  filterable
                  placeholder="请选择任务名称"
                  clearable
                >
                  <el-option
                    v-for="item in planList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- </el-col>
                <el-col :span="5"> -->
              <el-form-item label="履职人员" prop="executeRole">
                <el-select
                  v-model="queryParams.executeRole"
                  placeholder="请选择履职人员"
                  clearable
                >
                  <el-option
                    v-for="item in roleList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <!-- </el-col>
                <el-col :span="5"> -->
              <el-form-item label="任务进度" prop="status">
                <el-select
                  v-model="queryParams.status"
                  placeholder="请选择任务进度"
                  clearable
                >
                  <el-option
                    v-for="item in dict.type.resumption_status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <!-- </el-col>
                <el-col :span="9"> -->
              <el-form-item class="searchTitle" label="任务时间">
                <DataRangePicker
                  v-model="queryParams.range"
                  key="daterange"
                  type="daterange"
                  :clearable="timeClearable"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </el-form-item>
              <!-- </el-col>
              </el-row> -->
            </el-form>
            <!--    按纽    -->
            <el-row :gutter="10" >
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="getList"
                  >搜索</el-button
                >
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetQuery"
                  >重置</el-button
                >
                <el-button
                  type="primary"
                  icon="el-icon-download"
                  size="mini"
                  @click="exportExcel()"
                  v-hasPermi="['resumption:taskManager:export']"
                  >导出数据</el-button
                >
              </el-col>

              <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"

              ></right-toolbar>
            </el-row>
          </div>
          <el-table
            border
            height="594"
            size="small"
            v-loading="loading"
            :data="pageData"
          >
            <el-table-column
              align="center"
              fixed
              width="60px"
              label="序号"
              v-if="columns[0].visible"
            >
              <template slot-scope="scope">
                {{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="name"
              label="任务名称"
              min-width="120"
              v-if="columns[1].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="orgName"
              label="履职机构"
              min-width="120"
              v-if="columns[2].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="roleName"
              label="履职人员"
              width="150"
              v-if="columns[3].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="status"
              label="任务进度"
              width="100px"
              v-if="columns[4].visible"
            >
              <template slot-scope="r">
                {{ getLabel(dict.type.resumption_status, r.row.status) }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="planStartTime"
              label="任务时间"
              width="270px"
              v-if="columns[5].visible"
            >
              <template slot-scope="r">
                {{ dayjs(r.row.planStartTime).format("YYYY年MM月DD日") }}-{{
                  dayjs(r.row.planEndTime).format("YYYY年MM月DD日")
                }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="abnormalDataNums"
              label="隐患问题数"
              width="110"
              v-if="columns[6].visible"
            >
              <template slot-scope="r">
                <span v-if="r.row.status == 2 || r.row.status == 3">{{
                  r.row.abnormalDataNums
                }}</span>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              label="操作"
              fixed="right"
              min-width="120px"
            >
              <template slot-scope="r">
                <!-- <el-button v-if="
                            r.row.status == 'WAIT' &&
                            queryParams.type == 'QUARTER'
                        " type="text" @click="onEdit(r.row.id)">履职</el-button>
                        <el-button v-else-if="
                            r.row.status != 'NA' &&
                            r.row.status != 'NOT' &&
                            r.row.status != 'WAIT'
                        " type="text" @click="onEdit(r.row.id, { isRead: true })">查看</el-button> -->
                <el-button
                  type="text"
                  icon="el-icon-view"
                  @click="showDetail(r.row)"
                  v-show="r.row.status == 2 || r.row.status == 3"
                  v-hasPermi="['resumption:taskManager:query']"
                  >详情</el-button
                >
                <el-button
                  type="text"
                  icon="el-icon-tickets"
                  @click="showscanRecord(r.row)"
                  v-show="r.row.status == 2 || r.row.status == 3"
                  v-hasPermi="['resumption:taskManager:query']"
                  >扫描记录</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <!-- 详情 -->
    <dialog-detail
      ref="detaildialog"
      :statusOptions="dict.type.resumption_status"
    ></dialog-detail>
    <dialog-scan-record ref="scanrecorddialog"></dialog-scan-record>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
// import { Message } from "element-ui";
import DialogDetail from "./dialog.detail";
import DialogScanRecord from "./dialog.scanrecord";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
// import OrgTree from "@/components/orgTree";
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import * as api from "@/api/resumption/taskManger.js";
import { getLabel } from "@/views/commonOption.js";
import dayjs from "dayjs";

export default {
  name: "overviewDetialList",
  dicts: ["resumption_plan_cycle", "resumption_status"],
  components: {
    DialogDetail,
    DialogScanRecord,
    DataRangePicker,
    OrgTree
  },
  data() {
    let sDate = new Date();
    let eDate = new Date();

    return {
      loading: false,
      showSearch: true,
      total: 0,
      timeClearable: false,
      options: [],
      roleList: [],
      planList: [],
      queryParams: {
        checkSub: true,
        orgId: null,
        range: [sDate, eDate],
        executeRole: null,
        status: null,
        planId: null,
        pageNum: 1,
        pageSize: 10,
      },
      selectedOrgName: null,
      pageData: [],
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `任务名称`, visible: true },
        { key: 2, label: `履职机构`, visible: true },
        { key: 3, label: `履职人员`, visible: true },
        { key: 4, label: `任务进度`, visible: true },
        { key: 5, label: `任务时间`, visible: true },
        { key: 6, label: `隐患问题数`, visible: true },
      ],
    };
  },
  props: {},
  watch: {
    // org: {
    //   deep: true,
    //   immediate: true,
    //   handler(nOrg) {
    //     if (nOrg.id) {
    //       this.queryParams.orgId = nOrg.id;
    //     } else {
    //       this.queryParams.orgId = this.loginUser.org.id;
    //     }
    //     this.roleVal = "all";
    //     this.planVal = -1;
    //     this.loadRoles(this.queryParams.orgId);
    //   },
    // },
    statusVal(value) {
      if (value == undefined) return;
      if (value == "all") this.queryParams.status = null;
      else this.queryParams.status = value;
    },
    planVal(value) {
      if (value == undefined) return;
      if (value == -1) this.queryParams.planId = null;
      else this.queryParams.planId = value;
    },
    $route(v) {
      //this.queryParams.type = this.$route.params.type.toUpperCase();
    },
  },
  computed: {
    ...mapGetters(["orgName","orgId"]),
  },
  methods: {
    getLabel,
    dayjs,
    async exportExcel() {
      this.$tab.getCurrentTabName();
      if (!this.isTableHaveData) {
        this.$message.error({
          message: `无可用数据导出！`,
          type: "error",
          duration: 2000,
        });
        return;
      }

      this.download(
        "/core/resumption/record/export",
        {
          ...this.queryParams,
        },
        `${this.selectedOrgName}-${this.$tab.getCurrentTabName()}-${dayjs(
          new Date()
        ).format("YYYYMMDD")}.xlsx`
      );

      // await api.exportResumptionRecord(this.queryParams);
    },
    showDetail(row) {
      this.$refs.detaildialog.show(row.resumptionId, row.name);
      //this.$refs.detaildialog.show("75246a09eba74e018b60ade0b1f336f8", row.planName);
    },
    showscanRecord(row) {
      this.$refs.scanrecorddialog.show(row.resumptionId, row.orgId);
    },

    getDefaultOrg(node) {
      this.queryParams.orgId = node.id;
      this.selectedOrgName = node.shortName;
      this.getList();
      this.loadRoles(node.id);
      this.loadPlanList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;

      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      if(data==null){
        return;
      }
      this.queryParams.orgId = data.id;
      this.selectedOrgName = data.shortName;
      this.loadPlanList();
      this.loadRoles(data.id);
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.range=[new Date(),new Date()]
      this.queryParams.orgId = this.orgId;
      this.selectedOrgName = this.orgName;
      this.queryParams.checkSub = true;
      this.$refs["orgTree"].setCheckSub(this.queryParams.checkSub)
      // this.$refs.tree.setCurrentKey(null);
      this.getList();
    },
    async getList() {
      this.loading = true;
      api
        .page(this.queryParams)
        .then((r) => {
          this.isTableHaveData = r.rows.length > 0;
          this.pageData = r.rows;
          this.total = r.total;
          this.loading = false;
        })
        .catch((e) => {
          this.loading = false;
        });
    },
    loadRoles() {
      api
        .getRoles({ orgId: this.queryParams.orgId })
        .then((r) => (this.roleList = r.data));
    },

    loadPlanList() {
      api
        .getPlans({
          orgId: this.queryParams.orgId,
        })
        .then((r) => {
          this.planList = r.data;
          if (!this.planList.find((p) => p.id == this.queryParams.planId)) {
            this.queryParams.planId = null;
          }
        });
    },

    weekChanged() {
      this.updateWeekDateRange(this.week);
      this.resetRangeParam();
    },
    monthChanged() {
      this.resetRangeParam();
    },
    updateWeekDateRange(week) {
      let oneDayLong = 24 * 60 * 60 * 1000;
      let c_day = week.getDay();
      this.weekStart = new Date(
        week.valueOf() - (c_day == 0 ? 6 : c_day - 1) * oneDayLong
      );
      this.weekEnd = new Date(
        week.valueOf() + (c_day == 0 ? 0 : 7 - c_day) * oneDayLong
      );
    },
    statusColor(status, isBackground) {
      let color = "";
      switch (status) {
        case "1":
          color = "#00B0F0";
          break;
        case "2":
          color = "#FFC000";
          break;
        case "3":
          color = "#00B050";
          break;
        case "4":
          color = "#FF0000";
          break;
      }

      if (isBackground) {
        return "background-color:" + color;
      } else {
        return "color:" + color;
      }
    },
  },
  async mounted() {
    this.queryParams.orgId=this.orgId
    console.info("this.queryParams.orgId",this.queryParams.orgId)
    // let tabs=this.$tab;
    // debugger
    // const { params, query } = this.$route;
    // if (this.loginUser && this.loginUser.org && this.loginUser.org.id) {
    //   if (query == undefined || query.orgId == undefined) {
    //     this.queryParams.orgId = this.loginUser.org.id;
    //   }
    // }
    // this.loadRoles(this.queryParams.orgId);
    // await this.loadtypes();
    // this.loadPlanList();
    // await this.refresh();
  },
};
</script>

<style lang="scss" scoped>
.el-form--inline .el-form-item {
  display: inline-flex;
}
</style>
