<template>
  <view class="content">
    <view class="search card">
      <u--input
          prefixIcon="search"
          placeholder="请输入关键字"
          border="surround"
          v-model="keyword"
          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="RISK_UNIT_NAME || '请选择'" @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.PARTSNAME }}</text>
            <u-tag v-if="item.LEVELID=='levelD'" text="低风险/D级"></u-tag>
            <u-tag v-if="item.LEVELID=='levelB'" text="较大风险/B级" type="warning"></u-tag>
            <u-tag v-if="item.LEVELID=='levelC'" text="一般风险/C级" bgColor="#eeca51" borderColor="#eeca51"></u-tag>
            <u-tag v-if="item.LEVELID=='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.DEPT_NAME }}</text>

          </view>
          <view class="see_btn">
            <u-button type="primary" text="查看" size="mini"
                      @click="fnNavigatorDetail(item.RISKPOINT_ID,item.DEPARTMENT_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 {
  getRiskUnitList,
  getRiskUnitCustomizedList,
  getInspectedDepartment
} 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 {
      keyword: '',
      pageSize: 10,
      currentPage: 1,
      totalPage: 0,
      list: [],
      CORPINFO_ID: '',
      popupShow: false,
      riskUnitShow: false,
      levelShow: false,
      deptRange: [],
      riskPointList: [],
      levelList: [
        [
          {BIANMA: 'levelA', NAME: '重大风险'},
          {BIANMA: 'levelB', NAME: '较大风险'},
          {BIANMA: 'levelC', NAME: '一般风险'},
          {BIANMA: 'levelD', NAME: '低风险'},
        ]
      ],
      DEPT: '',
      DEPT_NAME: '',
      RISK_UNIT_NAME: '',
      LEVEL_ID: '',
      LEVEL_NAME: '',
    }
  },
  onLoad(event) {
    this.CORPINFO_ID = event.CORPINFO_ID;
    this.resetList()
    this.fnGetDept()
    this.fnGetRiskUnitList()
  },
  methods: {
    async fnGetDept() {
      let resData = await getInspectedDepartment({'CORPINFO_ID': this.CORPINFO_ID});
      this.deptRange = JSON.parse(resData.zTreeNodes)
    },
    async fnGetRiskUnitList() {
      let resData = await getRiskUnitList({CORPINFO_ID: this.CORPINFO_ID});
      this.$set(this.riskPointList, 0, resData.varList)
    },
    async getData() {
      this.popupShow = false
      let resData = await getRiskUnitCustomizedList({
        CORPINFO_ID: this.CORPINFO_ID,
        DEPARTMENT_ID: this.DEPT,
        RISKUNITNAME: this.RISK_UNIT_NAME,
        LEVELID: this.LEVEL_ID,
        KEYWORDS: this.keyword,
        showCount: this.pageSize,
        currentPage: this.currentPage,
      });
      this.list = [...this.list, ...resData.varList];
      this.totalPage = resData.page.totalPage;
    },
    resetList() {
      this.pageSize = 10
      this.currentPage = 1
      this.list = []
      this.getData()
    },
    fnNavigatorDetail(RISKPOINT_ID, DEPARTMENT_ID) {
      uni.$u.route({
        url: '/pages/risk-level-control/risk-management/detail',
        params: {
          RISKPOINT_ID,
          DEPARTMENT_ID,
          CORPINFO_ID: this.CORPINFO_ID,
        }
      })
    },
    scrolltolower() {
      this.currentPage++;
      if (this.totalPage >= this.currentPage) this.getData();
    },
    showTree() {
      this.$refs.tkitree._show()
    },
    confirmTree(e) {
      this.DEPT = e[0].id
      this.DEPT_NAME = e[0].name
    },
    pickerConfirm(e, type) {
      if (type === 'riskUnit') {
        this.RISK_UNIT_NAME = e.value[0].RISKUNITNAME
        this.riskUnitShow = false;
      }
      if (type === 'level') {
        this.LEVEL_ID = 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_ID = ''
      this.LEVEL_NAME = ''
      this.keyword = ''
      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>
