<template>
  <view class="content">
    <view class="search card">
      <u--input
          class="ml-10"
          prefixIcon="search"
          placeholder="请输入关键字"
          border="surround"
          v-model="hiddendescr"
          clearable
          shape="circle"
      ></u--input>
      <view class="bth-mini ml-10">
        <u-button type="success" text="确定" @click="resetList"></u-button>
      </view>

    </view>
    <u-popup :show="popupShow" @close="popupShow = false" mode="right" :customStyle="{width:'85vw'}">
      <view class="card">
        <view class="pl-10 pr-10">
          <u--form labelPosition="left">
            <u-form-item label="隐患发现开始时间" borderBottom>
              <u--text :text="start || '请选择'"
                       @click="startDataShow = true"></u--text>
              <u-datetime-picker :show="startDataShow"
                                 mode="datetime"
                                 :value="Number(new Date())"
                                 @cancel="startDataShow = false"
                                 @confirm="startDataConfirm"></u-datetime-picker>
            </u-form-item>
            <u-form-item label="隐患发现结束时间" borderBottom>
              <u--text :text="end || '请选择'"
                       @click="endDataShow = true"></u--text>
              <u-datetime-picker :show="endDataShow"
                                 mode="datetime"
                                 :value="Number(new Date())"
                                 @cancel="endDataShow = false"
                                 @confirm="endDataConfirm"></u-datetime-picker>
            </u-form-item>
            <u-form-item label="发现人" borderBottom>
              <u--input
                  inputAlign="right"
                  placeholder="请输入发现人"
                  border="none"
                  v-model="creatorName"
                  clearable
              ></u--input>
            </u-form-item>
            <u-form-item label="确认人" borderBottom>
              <u--input
                  inputAlign="right"
                  placeholder="请输入确认人"
                  border="none"
                  v-model="keyConfirmUser"
                  clearable
              ></u--input>
            </u-form-item>
            <u-form-item label="发现人部门" borderBottom>
              <u--text :text="departmentName || '请选择'" @click="showTree('tkitree')"></u--text>
              <tki-tree ref="tkitree" :range="deptRange" rangeKey="name" selectParent
                        @confirm="confirmTree($event,'dept')"/>
            </u-form-item>
            <u-form-item label="隐患类型" borderBottom>
              <u--text :text="hiddenTypeName || '请选择'" @click="showTree('tkitree1')"></u--text>
              <tki-tree ref="tkitree1" :range="HazardTypeRange" rangeKey="name" selectParent children-name="nodes"
                        @confirm="confirmTree($event,'type')"/>
            </u-form-item>
          </u--form>
        </view>
        <view class="mt-10 flex-between">
          <view class="flex1">
            <u-button type="info" text="重置" @click="reset"></u-button>
          </view>
          <view class="ml-10 flex1">
            <u-button type="primary" text="查询" @click="resetList"></u-button>
          </view>
        </view>
      </view>
    </u-popup>
    <u-list @scrolltolower="scrolltolower" v-if="list.length > 0">
      <u-list-item v-for="(item, index) in list" :key="index">
        <view>
          <view class="flex-between main-title">
            <text>{{ item.hiddendescr }}</text>
            <u-tag :text="item.hiddenlevelName" type="warning"></u-tag>
          </view>
          <view class="flex-between mt-10 subtitle">
            <text>
              来源：
              <template v-if="item.source === 1">隐患快报</template>
              <template v-if="item.source === 2 || item.source === 3">清单排查</template>
              <template v-if="item.source === 4">安全环保检查(监管端)</template>
              <template v-if="item.source === 5">安全环保检查(企业端)</template>
              <template v-if="item.source === 6">消防检查</template>
            </text>
          </view>
          <view class="flex-between mt-10 subtitle">
            <text>隐患发现人：{{ item.hiddenDiscoverer }}</text>
            <text>隐患发现时间：{{ item.discoverytime }}</text>
          </view>
          <view class="flex-between mt-10 subtitle">
            <text>隐患确认人：{{ item.confirmUserName }}</text>
          </view>
          <view class="see_btn">
            <u-button type="primary" text="查看" size="mini"
                      @click="fnNavigatorDetail(item.hiddenId)"></u-button>
          </view>
        </view>
      </u-list-item>
    </u-list>
    <empty v-else></empty>
    <fab-button type="search" @click="popupShow = true"/>
  </view>
</template>

<script>
import {getCorpinfoDeptListTree, getHiddenType, getIgnoreHiddenList} from "@/api";
import tkiTree from "@/components/tki-tree/tki-tree.vue"
import FabButton from "@/components/fab_button/index.vue";

export default {
  components: {FabButton, tkiTree},
  data() {
    return {
      hiddendescr: '',
      pageSize: 10,
      currentPage: 1,
      totalPage: 0,
      list: [],
      companyId: '',
      popupShow: false,
      startDataShow: false,
      endDataShow: false,
      deptRange: [],
      HazardTypeRange: [],
      keyFindUserDept: '',
      departmentName: '',
      start: '',
      keyConfirmUser: '',
      creatorName: '',
      end: '',
      hiddenType: '',
      hiddenTypeName: '',
    }
  },
  onLoad(event) {
    this.companyId = event.companyId;
    this.resetList()
    this.fnGetDept()
    this.fnGetHiddenType()
  },
  methods: {
    async fnGetDept() {
      let resData = await getCorpinfoDeptListTree({'CORPINFO_ID': this.companyId});
      this.deptRange = JSON.parse(resData.zTreeNodes)
    },
    async fnGetHiddenType() {
      let resData = await getHiddenType();
      this.HazardTypeRange = JSON.parse(resData.zTreeNodes)
    },
    async getData() {
      this.popupShow = false
      let { varList, page } = await getIgnoreHiddenList({
        companyId: this.companyId,
        keyFindUserDept: this.keyFindUserDept,
        dates: this.start != null && this.start !== '' && this.end != null && this.end !== '' ? [this.start, this.end] : null,
        keyConfirmUser: this.keyConfirmUser,
        creatorName: this.creatorName,
        keyHiddenType: this.hiddenType != null && this.hiddenType !== '' ? [this.hiddenType] : null,
        hiddendescr: this.hiddendescr,
        showCount: this.pageSize,
        currentPage: this.currentPage,
      });
      this.list = [...this.list, ...varList];
      this.totalPage = page.totalPage;
    },
    resetList() {
      this.pageSize = 10
      this.currentPage = 1
      this.list = []
      this.getData()
    },
    fnNavigatorDetail(hiddenId) {
      uni.$u.route({
        url: '/pages/general-hidden-management/ignore-hidden/detail',
        params: {
          hiddenId,
        }
      })
    },
    scrolltolower() {
      this.currentPage++;
      if (this.totalPage >= this.currentPage) this.getData();
    },
    showTree(ref) {
      this.$refs[ref]._show()
    },
    confirmTree(e, type) {
      if (type === 'dept') {
        this.keyFindUserDept = e[0].id
        this.departmentName = e[0].name
      } else if (type === 'type') {
        this.hiddenType = e[0].id
        this.hiddenTypeName = e[0].name
      }
    },
    startDataConfirm(e) {
      this.start = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM')
      this.startDataShow = false
    },
    endDataConfirm(e) {
      this.end = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM')
      this.endDataShow = false
    },
    reset() {
      this.popupShow = false
      this.keyword = ''
      this.keyConfirmUser = ''
      this.creatorName = ''
      this.start = ''
      this.end = ''
      this.keyFindUserDept = ''
      this.departmentName = ''
      this.hiddenType = ''
      this.hiddenTypeName = ''
      this.resetList()
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  display: flex;

  .ml-10 {
    margin-left: 10rpx;
  }

  .bth-mini {
    width: 100rpx;
  }

}

.see_btn {
  width: 100rpx;
  float: right;
}

</style>
