<template>
  <view class="content">
    <view class="search card">
      <u--input
          prefixIcon="search"
          placeholder="请输入关键字"
          border="surround"
          v-model="keyWords"
          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="DEPT_NAME || '请选择'" @click="showTree"></u--text>
              <tki-tree ref="tkitree" :range="deptRange" rangeKey="name" selectParent @confirm="confirmTree"/>
            </u-form-item>
            <u-form-item label="风险点（单元）" borderBottom>
              <u--text :text="riskUnitName || '请选择'" @click="riskUnitShow = true"></u--text>
              <u-picker :show="riskUnitShow" :columns="riskPointList" keyName="riskUnitName"
                        @cancel="riskUnitShow = false"
                        @confirm="pickerConfirm($event,'riskUnit')"></u-picker>
            </u-form-item>
            <u-form-item label="风险等级" borderBottom>
              <u--text :text="LEVEL_NAME || '请选择'" @click="levelShow = true"></u--text>
              <u-picker :show="levelShow" :columns="levelList" keyName="NAME"
                        @cancel="levelShow = false"
                        @confirm="pickerConfirm($event,'level')"></u-picker>
            </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.identificationpartsName }}</text>
            <u-tag v-if="item.level ==='levelD'" text="低风险/D级"></u-tag>
            <u-tag v-if="item.level ==='levelB'" text="较大风险/B级" type="warning"></u-tag>
            <u-tag v-if="item.level ==='levelC'" text="一般风险/C级" bgColor="#eeca51" borderColor="#eeca51"></u-tag>
            <u-tag v-if="item.level ==='levelA'" text="重大风险/A级" type="error"></u-tag>
          </view>
          <view class="flex-between mt-10 subtitle">
            <text>风险（点）单元：{{ item.riskUnitName }}</text>
          </view>
          <view class="flex-between mt-10 subtitle">
            <text>管控部门：{{ item.departmentName }}</text>
          </view>
          <view class="see_btn">
            <u-button type="primary" text="查看" size="mini"
                      @click="fnNavigatorDetail(item.id)"></u-button>
          </view>
        </view>
      </u-list-item>
    </u-list>
    <empty v-else></empty>
    <fab-button type="search" @click="popupShow = true"/>
  </view>
</template>

<script>
import {
  getRiskUnitCustomizedList,
  getInspectedDepartment, getRiskUnitList
} 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 {
      keyWords: '',
      pageSize: 10,
      currentPage: 1,
      totalPage: 0,
      list: [],
      companyId: '',
      popupShow: false,
      riskUnitShow: false,
      levelShow: false,
      deptRange: [],
      riskPointList: [],
      levelList: [
        [
          {BIANMA: 'levelA', NAME: '重大风险'},
          {BIANMA: 'levelB', NAME: '较大风险'},
          {BIANMA: 'levelC', NAME: '一般风险'},
          {BIANMA: 'levelD', NAME: '低风险'},
        ]
      ],
      departmentId: '',
      DEPT_NAME: '',
      riskUnitName: '',
      riskUnitId: '',
      level: '',
      LEVEL_NAME: '',
    }
  },
  onLoad(event) {
    this.companyId = event.companyId;
    this.resetList()
    this.fnGetDept()
    this.fnGetRiskUnitList()
  },
  methods: {
    async fnGetDept() {
      let resData = await getInspectedDepartment({'CORPINFO_ID': this.companyId});
      this.deptRange = JSON.parse(resData.zTreeNodes)
    },
    async fnGetRiskUnitList() {
      let { data } = await getRiskUnitList({ corpinfoId: this.companyId });
      this.$set(this.riskPointList, 0, data)
    },
    async getData() {
      this.popupShow = false
      let { varList, page } = await getRiskUnitCustomizedList({
        corpinfoId: this.companyId,
        departmentId: this.departmentId,
        riskUnitId: this.riskUnitId,
        level: this.level,
        keyWords: this.keyWords,
        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(id) {
      uni.$u.route({
        url: '/pages/risk-level-control/risk-management/detail',
        params: {
          id,
          companyId: this.companyId,
        }
      })
    },
    scrolltolower() {
      this.currentPage++;
      if (this.totalPage >= this.currentPage) this.getData();
    },
    showTree() {
      this.$refs.tkitree._show()
    },
    confirmTree(e) {
      this.departmentId = e[0].id
      this.DEPT_NAME = e[0].name
    },
    pickerConfirm(e, type) {
      if (type === 'riskUnit') {
        this.riskUnitName = e.value[0].riskUnitName
        this.riskUnitId = e.value[0].id
        this.riskUnitShow = false;
      }
      if (type === 'level') {
        this.level = e.value[0].BIANMA
        this.LEVEL_NAME = e.value[0].NAME
        this.levelShow = false;
      }
    },
    reset() {
      this.popupShow = false
      this.DEPT = ''
      this.DEPT_NAME = ''
      this.RISK_UNIT_NAME = ''
      this.level = ''
      this.LEVEL_NAME = ''
      this.keyWords = ''
      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>
