﻿﻿﻿﻿﻿﻿<template>
  <div style="height:100%;width:100%;">
    <a-layout style="height:100%;width:100%;">
      <a-layout-sider
        :width="leftWidth"
        theme="light"
        style="border-right:2px solid #d5d5ff;float:left"
        :style="{ 'max-height': testHeight + 'px', 'height': testHeight + 'px' }">
        <div class="tree" style="width: 100%; height: 100%; overflow: auto; display: inline-flex;">
          <a-tree
            :tree-data="treeData"
            @select="treeNodeClickHandler"
            :fieldNames="{ children: 'children', title: 'pnm', key: 'id' }"
            :autoExpandParent="false"
            ref="jigou_tree"
            style="height:100%;">
            <template #custom="item">
              <span style="float:left;">{{ item.pnm }}</span>
            </template>
          </a-tree>
        </div>
      </a-layout-sider>
      <a-layout-content id="dxdcListMain_qsgc" style="padding:5px 20px;position:relative;">
        <a-row>
          <a-col :span="24">
            <a-form
              layout="inline"
              class="demo-form-inline"
              style="padding-left: 10px;">
              <a-form-item label="督查状态" style="min-width: 200px;">
                <a-select v-model:value="dcType" placeholder="督查状态" allow-clear>
                  <a-select-option value="0">未督查</a-select-option>
                  <a-select-option value="1">督查中</a-select-option>
                  <a-select-option value="2">已督查</a-select-option>
                </a-select>
              </a-form-item>
              <!-- <a-form-item label="行政区名称" style="width: 23%;">
                                    <addvSearch
                                      v-model="adName"
                                      :maxHeight="300"
                                      :maxWidth="250"
                                      :rootAddCode="curAdcode_long"
                                      :level="&quot;3&quot;"
                                      :nameOrCode="'name'"></addvSearch>
                                  </a-form-item> -->
              <a-form-item label="对象名称" style="min-width: 23%;">
                <a-input v-model:value="name" placeholder="对象名称" />
              </a-form-item>
              <!-- <a-form-item label="工程规模" style="width: 23%;">
                      <a-select v-model:value="engScale" placeholder="工程规模" allow-clear>
                        <a-select-option v-for="item in engScales" :key="item.id">{{ item.name }}</a-select-option>
                      </a-select>
                    </a-form-item> -->
              <!-- <a-form-item label="所在组" style="width: 23%;">
                      <a-auto-complete
                        v-model="groupName"
                        allow-clear
                        placeholder="请输入内容"
                        :filter-option="querySearchAsync"
                        @select="handleSelect"/>
              </a-form-item> -->
              <a-form-item label="督查时间" style="min-width: 23%;">
                <div style="display: flex;">
                  <a-range-picker v-model:value="dateRange" format="YYYY-MM-DD" separator="至" allow-clear />
                </div>
              </a-form-item>
              <a-form-item>
                <a-button type="primary" @click="onSubmit" size="small" >查询</a-button>
              </a-form-item>
            </a-form>
          </a-col>
        </a-row>
        <a-table
          ref="table"
          :columns="columns"
          :data-source="skDataList"
          :loading="expLoading"
          :pagination="false"
          row-key="rgstrId"
          style="width: 100%;"
          :style="{height: tableHeight + 'px'}"
          :scroll="{x: 'max-content', y: tableHeight - 60}"
          bordered>
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'nm'">
              <a-tooltip placement="top">
                <template #title>
                  <span>{{ record.nm }}</span>
                </template>
                <span class="ellipsis150">{{ record.nm }}</span>
              </a-tooltip>
            </template>
            <template v-else-if="column.dataIndex === 'groupName'">
              <a-tooltip placement="top">
                <template #title>
                  <span>{{ record.groupName }}</span>
                </template>
                <span class="ellipsis150">{{ record.groupName }}</span>
              </a-tooltip>
            </template>
            <template v-else-if="column.dataIndex === 'intm'">
              <a-tooltip placement="top">
                <template #title>
                  <span>{{ dateFormat(record.intm, 'yyyy-MM-dd') }}</span>
                </template>
                <span class="ellipsis120">{{ dateFormat(record.intm,'yyyy-MM-dd') }}</span>
              </a-tooltip>
            </template>
            <template v-else-if="column.dataIndex === 'location'">
              <a-tooltip placement="top">
                <template #title>
                  <span>{{ record.location }}</span>
                </template>
                <span class="ellipsis200">{{ record.location }}</span>
              </a-tooltip>
            </template>
            <template v-else-if="column.dataIndex === 'state'">
              <span
                style="padding: 3px 10px;background:#F56C6C;border-radius:15px;color:rgba(255,255,255,1)"
                v-if="record.state == 0">未督查</span>
              <span
                style="padding: 3px 10px;background:#FBB56D;border-radius:15px;color:rgba(255,255,255,1)"
                v-if="record.state == 1">督查中</span>
              <span
                style="padding: 3px 10px;background:#63C25E;border-radius:15px;color:rgba(255,255,255,1)"
                v-if="record.state == 2">已督查</span>
            </template>
            <template v-else-if="fillTableDataKeys.includes(column.dataIndex)">
              <span
                style="cursor:pointer;color:#F56C6C;"
                v-if="record[column.dataIndex] == '0' || !record[column.dataIndex]">待填报</span>
              <span style="cursor:pointer;color:#fbb56d" v-if="record[column.dataIndex] == '1'">未完成</span>
              <span style="cursor:pointer;color:#67C23A" v-if="record[column.dataIndex] == '2'">已完成</span>
            </template>
            <template v-else-if="column.dataIndex === 'action'">
              <a-button size="small" v-if="!record.rgstrId" ghost @click="toAddMember('填报',record)" type="primary">填报
              </a-button>
              <a-button
                size="small"
                v-if="record.rgstrId"
                ghost
                @click="toAddMember('编辑',record)"
                :disabled="record.state == '2'"
                type="primary">督查</a-button>
              <a-button
                size="small"
                type="danger"
                v-if="record.rgstrId"
                ghost
                @click="deleteMember(record)"
                :disabled="record.state == '2'">删除</a-button>
              <a-button
                size="small"
                type="danger"
                v-if="record.rgstrId"
                ghost
                @click="submitHandler(record)"
                :disabled="record.state == '2'">提交</a-button>
              <a-button size="small" v-if="record.rgstrId && record.state == '2'" ghost @click="toCheck(record)" type="primary">
                查看
              </a-button>
            </template>
          </template>
        </a-table>
        <a-pagination
          show-size-changer
          v-model:pageSize="pageSize"
          :total="total"
          :page-size-options="['10','20', '30', '40', '50','100']"
          :show-total="total => `共${total}条`"
          @showSizeChange="handleSizeChange"
          @change="pageIndexChange"
          style="margin-top: 10px;" />
        <div class="open_board">
          <a-icon type="left" v-if="isOpen" @click="closeLeft" />
          <a-icon type="right" v-else @click="openLeft" />
        </div>
      </a-layout-content>
    </a-layout>
    <quShuiGongChengTianBao
      v-if="showquShuiGongChengTianBao"
      @cancelHandler="cancelHandler"
      @addDiFangSuccess="addDiFangSuccess"
      :rowData="rowData"
      :openType="openType" />
  </div>
</template>

<script>
import {
  deleteObjByObjId,
  getrgstrIdObject_qsgc,
} from '../../api/duChaTianBao.js';
import {
  curAdcode_long,
  curAdcode_short,
} from '../../utils/global_variable.js';
import request from '../../utils/gw_ajax_request.js';
import { dateFormat } from '../../utils/gw_date.js';
import addvSearch from '../../widgets/addvSearch.vue';
import quShuiGongChengTianBao from '../duChaTianBao/quShuiGongChengTianBao.vue';
export default {
  components: {
    addvSearch,
    quShuiGongChengTianBao,
  },
  data() {
    return {
      treeData: [],
      skDataList: [],
      pageIndex: 1,
      pageSize: 20,
      total: 0,
      dateRange: [],
      groups: [],
      leftWidth: '320px',
      isOpen: true,
      expLoading: false,
      testHeight: window.innerHeight - 111,
      currentNodeId: '',
      dcType: '',
      adName: '',
      openType: true,
      name: '',
      groupName: '',
      groupId: '',
      tableHeight: 0,
      showquShuiGongChengTianBao: false,
      fillTableData: [{ value: 'stnState', label: '整改提升检查情况' }],
      columns: [
        {
          title: '对象名称',
          dataIndex: 'nm',
          width: 150,
          fixed: 'left',
          align: 'center',
        },
        {
          title: '所在组',
          dataIndex: 'groupName',
          width: 150,
          align: 'center',
        },
        {
          title: '督查时间',
          dataIndex: 'intm',
          width: 120,
          align: 'center',
        },
        {
          title: '所在位置',
          dataIndex: 'location',
          width: 200,
          align: 'center',
        },
        {
          title: '督查状态',
          dataIndex: 'state',
          width: 200,
          align: 'center',
        },
      ],
    };
  },
  computed: {
    persId() {
      return localStorage.getItem('persId')
        ? localStorage.getItem('persId')
        : '1098101939614724096';
    },
    curAdcode_long() {
      return curAdcode_long;
    },
    fillTableDataKeys() {
      return this.fillTableData.map((item) => item.value);
    },
  },
  created() {
    this.fillTableData.forEach((item) => {
      const obj = {
        title: item.label,
        dataIndex: item.value,
        width: 140,
        align: 'center',
      };
      this.columns.push(obj);
    });
    if (this.ownPriv('manage_qskc')) {
      this.columns.push({
        title: '操作',
        dataIndex: 'action',
        width: 330,
        align: 'center',
        fixed: 'right',
      });
    }
  },
  mounted() {
    this.$nextTick(function () {
      this.tableHeight =
        window.innerHeight - this.$refs.table.$el.offsetTop - 200;
      window.onresize = () => {
        this.tableHeight =
          window.innerHeight - this.$refs.table.$el.offsetTop - 200;
      };
    });
    this.getLeftTreeData();
    this.getDfDataList();
    this.getAllGroup();
  },
  watch: {
    currentNodeId(n, o) {
      this.getDfDataList();
    },
    groupName(n, o) {
      if (n == '') {
        this.groupId = '';
      }
    },
  },
  methods: {
    submitHandler(rowData) {
      getrgstrIdObject_qsgc(rowData.objId).then((res) => {
        if (res.data.stnState == '0') {
          this.$message({
            message: '请填写 整改提升检查情况',
            type: 'warning',
          });
          return;
        }
        this.$confirm({
          title: '提示',
          content: '请确认所有信息填报完毕？',
          okType: 'warning',
          onOk: () => {
            const formObj = {
              rgstrId: rowData.rgstrId,
              id: rowData.rgstrId,
              state: '2',
            };
            request.post('/bis/insp/gtwtjx/update', formObj).then((res) => {
              if (res.success) {
                request
                  .get('/dc/insp/pblm/submit/updateState/' + rowData.objId)
                  .then((res) => {
                    if (res.success) {
                      this.$message.success('提交成功');
                      this.getDfDataList();
                    } else {
                      this.cancelHandler();
                    }
                  });
              } else {
                this.$emit('cancelHandler');
              }
            });
          },
        });
      });
    },
    dateFormat,
    getAllGroup() {
      request
        .get('/dc/user/getGroupByPersid', {
          params: { persid: this.persId, ptype: '64' },
        })
        .then((res) => {
          if (res.success) {
            this.groups = res.data;
          }
        });
    },
    closeLeft() {
      this.isOpen = false;
      this.leftWidth = '0';
    },
    openLeft() {
      this.isOpen = true;
      this.leftWidth = '320px';
    },
    querySearchAsync(queryString, cb) {
      var restaurants = this.groups;
      var results = queryString
        ? restaurants.filter(this.createStateFilter(queryString))
        : restaurants;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1500 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return state.nm.toLowerCase().indexOf(queryString.toLowerCase()) != -1;
      };
    },
    getLeftTreeData() {
      request
        .get('/dc/organization/base/getAllNode', {
          params: { userId: this.persId, orgType: '064' },
        })
        .then((res) => {
          let data = res.data;
          data.forEach((ele) => {
            if (ele.pid == null || ele.pid == '') {
              ele.pid = '064';
            }
          });
          data = this.getTrees(data);
          this.treeData = data;
        });
    },
    // 格式化树形数据
    getTrees(list) {
      // @wxcwater重写方法 适应多个树
      let highLevelLength = 99; // 预设最大节点id长度，目前最大应该为12，所以设置为99保证正常
      list.forEach((item, index) => {
        item['pidLength'] = item.pid && item.pid != '' ? item.pid.length : 0;
        if (highLevelLength > item.pidLength) {
          highLevelLength = item.pidLength;
        }
      });
      const heightLevelNode = [];
      list.forEach((item, index) => {
        if (highLevelLength >= item.pidLength) {
          heightLevelNode.push(item);
        }
      });
      list.forEach((item, index) => {
        heightLevelNode.forEach((pItem) => {
          this.addTreeNode(pItem, item);
        });
      });
      return heightLevelNode;
    },
    addTreeNode: function (treeNode, leafNode) {
      if (!Object.hasOwn(treeNode, 'children')) {
        treeNode['children'] = [];
      }
      if (leafNode.pid == treeNode.id) {
        treeNode.children.push(leafNode);
        return;
      }
      treeNode.children.forEach((item) => {
        this.addTreeNode(item, leafNode);
      });
    },
    treeNodeClickHandler: function (data, node, tree) {
      this.currentNodeId = data[0];
    },
    handleSelect(item) {
      this.groupId = item.id;
    },
    onSubmit() {
      this.getDfDataList();
    },
    pageIndexChange(val) {
      this.pageIndex = val;
      this.getDfDataList();
    },
    handleSizeChange(size, val) {
      this.pageSize = val;
      this.getDfDataList();
    },
    getDfDataList: function () {
      let paramStartDate = '';
      let paramEndDate = '';
      if (
        this.dateRange &&
        this.dateRange.length > 1 &&
        this.dateRange[0] != '' &&
        this.dateRange[1] != ''
      ) {
        paramStartDate = this.dateRange[0];
        paramEndDate = this.dateRange[1];
      }
      const paramObj = {
        // "type": "stnd",
        presId: this.persId,
        pType: '64',
        pageSize: this.pageSize,
        pageNum: this.pageIndex,
        plnaId: this.currentNodeId,
        groupId: this.groupId, // 分组
        state: this.dcType,
        adName: this.adName,
        sttm: paramStartDate,
        entm: paramEndDate,
      };
      request.post(`/dc/insp/rsvrRgstr/list/type`, paramObj).then((res) => {
        if (res.success) {
          this.skDataList = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    toAddMember(type, rowData) {
      this.rowData = rowData;
      this.showquShuiGongChengTianBao = true;
      this.openType = true;
    },
    cancelHandler() {
      this.showquShuiGongChengTianBao = false;
    },
    addDiFangSuccess() {
      this.getDfDataList();
      this.showquShuiGongChengTianBao = false;
    },
    toCheck(rowData) {
      this.rowData = rowData;
      this.showquShuiGongChengTianBao = true;
      this.openType = false;
    },
    deleteMember(obj) {
      this.$confirm({
        title: '提示',
        content: '此操作将督查对象从列表中删除，确认删除该对象吗？',
        okType: 'warning',
        onOk: () => {
          request
            .get(
              `/supervision/plan/cleanObjDataByObjId?objType=64&objId=${obj.objId}`,
            )
            .then((res) => {
              if (res.success) {
                this.pageIndexChange();
                this.$message.success('删除成功');
              } else {
                this.$message.error(res.message);
              }
            });
        },
      });
    },
  },
};
</script>

<style lang='less' scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

#dxdcListMainsk {
  overflow-y: hidden;
}

/deep/ .ant-table-pagination {
  float: left;
}

/deep/ .ant-layout-sider,
/deep/ .ant-layout-content {
  background: #fff;
}

/deep/ .ant-table-thead>tr>th,
/deep/ .ant-table-tbody>tr>td {
  padding: 0;
  height: 60px;
}

.ellipsis200 {
  display: block;
  width: 198px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.ellipsis120 {
  display: block;
  width: 118px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.ellipsis150 {
  display: block;
  width: 148px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

/deep/ .ant-table-scroll {
  position: relative;
  z-index: 1;
}

/deep/.ant-table-fixed-left,
/deep/ .ant-table-fixed-right {
  z-index: 2;
}

.open_board {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 24px;
  height: 30px;
  line-height: 30px;
  border: solid 1px #ddd;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  text-align: center;
  background-color: #bbebff;
  cursor: pointer;
}
</style>
