<template>
  <div class="contradiction-div">
    <div class="head-title">重点领域</div>
    <div class="search-div">
      <div style="width:90%;float: left;">
        <Row class="mb10">
           <Col span="6">
            <span>纠纷类别：</span>
            <Select v-model="searchInfo.event_category_id" style="width:70%;" clearable>
              <Option v-for="(item, index) in dictData.list1" :value="item.value" :key="index">{{ item.label }}</Option>
            </Select>
          </Col>
          <Col span="6">
            <span>开始时间：</span>
            <DatePicker type="date" v-model="searchInfo.start_time" placeholder="请选择开始时间"></DatePicker>
          </Col>
          <Col span="6">
            <span>结束时间：</span>
            <DatePicker type="date" v-model="searchInfo.end_time" placeholder="请选择结束时间"></DatePicker>
          </Col>
        </Row>
        <Row class="">
          <Alert type="warning">依据系统数据统计，各类矛盾纠纷事件数量与上一年同期相比增长50%以上、与上一月相比增长30%以上，符合上述条件之一的，确定为“重点领域”，系统自动触发预警</Alert>
        </Row>
      </div>
      <div style="width:10%;float: left;">
        <Row>
          <Col span="12" offset="6">
            <Button type="primary" @click="searchData">搜索</Button>
          </Col>
        </Row>
      </div>
    </div>
    <Table stripe :columns="columns" :data="dataList">
      <template slot-scope="{row, index}" slot="on_year">
        <div>{{row.on_year}}%</div>
      </template>
      <template slot-scope="{row, index}" slot="link_relative_ratio">
        <div>{{row.link_relative_ratio}}%</div>
      </template>
      <template slot-scope="{row, index}" slot="type">
        <div v-if="row.type === '2'">已解除</div>
        <div v-if="row.type === '1'">未解除</div>
      </template>
      <template slot-scope="{row, action}" slot="action">
        <Button type="primary" class="mr5" @click="getInfo(row)">详情</Button>
<!--        <Button type="primary" v-if="row.type === '1'" @click="liftRisk(row)">解除风险</Button>-->
      </template>
    </Table>
    <Page class="page_style"  :total="params.totalRecord" :current="params.pageNo"
            :page-size="params.pageSize" @on-change="changePageNo" show-total  show-sizer @on-page-size-change="size"/>
    <Modal v-model="modal" width="1000" title="预警详情">
      <div class="search-div">
        <!-- <div style="float: left;"> -->
          <Row class="mb10">
            <Col span="8">
              <span>纠纷大类：</span>
              <Select v-model="searchInfo1.info" @on-clear="clearDispute('info')" @on-change="changeDispute(searchInfo1.info)" :clearable="true" style="width:70%">
                <Option v-for="item in dictData.list1" :value="JSON.stringify(item)" :key="item.value">{{ item.label }}</Option>
              </Select>
            </Col>
            <Col span="8">
              <span>纠纷小类：</span>
              <Select v-model="searchInfo1.eventTypeId" :clearable="true" style="width:70%">
                <Option v-for="item in dictData.list2" :value="item.value" :key="item.value">{{ item.label }}</Option>
              </Select>
            </Col>
            <Col span="7">
              <span>统计月份：</span>
              <DatePicker type="date" v-model="searchInfo1.occurrenceDate" placeholder="请选择统计月份"></DatePicker>
            </Col>
            <Col span="1">
              <Button type="primary" @click="searchInfoData">搜索</Button>
            </Col>
          </Row>
          <Row class="mb10">
            <Col span="8">
              <span>统计范围：</span>
              <Select v-model="searchInfo.cityId" @on-change="changeArea('2',searchInfo.cityId)" :clearable="true" placeholder="请选择市" style="width:70%">
                <Option v-for="item in areaData.list1" :value="item.parent_id" :key="item.parent_id">{{ item.name }}</Option>
              </Select>
            </Col>
            <Col span="5" class="mr5">
              <Select v-model="searchInfo.areaId" @on-change="changeArea('3',searchInfo.areaId)" :clearable="true" placeholder="请选择区" style="width:100%">
                <Option v-for="item in areaData.list2" :value="item.parent_id" :key="item.parent_id">{{ item.name }}</Option>
              </Select>
            </Col>
            <Col span="5" class="mr5">
              <Select v-model="searchInfo.streetId" @on-change="changeArea('4',searchInfo.streetId)" :clearable="true" placeholder="请选择街道" style="width:100%">
                <Option v-for="item in areaData.list3" :value="item.parent_id" :key="item.parent_id">{{ item.name }}</Option>
              </Select>
            </Col>
            <Col span="5">
              <Select v-model="searchInfo.communityId" placeholder="请选择社区" :clearable="true" style="width:100%">
                <Option v-for="item in areaData.list4" :value="item.parent_id" :key="item.parent_id">{{ item.name }}</Option>
              </Select>
            </Col>
          </Row>
      </div>
      <Table stripe :columns="columns1" :data="monthFocusList">
        <template slot-scope="{ row, index }" slot="riskLevel">
          <div class="flex-div">
            <div v-if="row.riskLevel == '一般矛盾纠纷'">
              <img class="level-option-img" src="@/assets/images/warning4.png" alt="">
              <span style="padding-left: 10px;">一般矛盾纠纷</span>
            </div>
            <div v-if="row.riskLevel == '复杂矛盾纠纷'">
              <img class="level-option-img" src="@/assets/images/warning5.png" alt="">
              <span style="padding-left: 10px;">复杂矛盾纠纷</span>
            </div>
            <div v-if="row.riskLevel == '重大矛盾纠纷'">
              <img class="level-option-img" src="@/assets/images/warning6.png" alt="">
              <span style="padding-left: 10px;">重大矛盾纠纷</span>
            </div>
          </div>
        </template>
        <template slot-scope="{row, index}" slot="isResolve">
          <div v-if="row.isResolve === '1'">已化解</div>
          <div v-if="row.isResolve === '2'">未化解</div>
        </template>
        <template slot-scope="{row, index}" slot="action">
          <Button type="primary" class="mr5" @click="getEventInfo(row)">详情</Button>
        </template>
      </Table>
      <Page class="page_style"  :total="params1.totalRecord" :current="params1.pageNo"
              :page-size="params1.pageSize" @on-change="changePageNo1" show-total  show-sizer @on-page-size-change="size1"/>
    </Modal>
  </div>
</template>
<script>
import {
  findFocusAreasTypeList, selectDictList, selectCityList,
  findEventFocusAreasList, updateFocusAreasType
} from '@/api/contradiction.js'
import { date2str } from '@/api/eventlist.js'
export default {
  name: 'register',
  data () {
    return {
      modal: false,
      dictData: {
        list1: [],
        list2: []
      },
      areaData: {
        list1: [],
        list2: [],
        list3: [],
        list4: []
      },
      user_types: [],
      searchInfo: {},
      searchInfo1: {},
      monthFocusList: [],
      columns1: [
        { type: 'index', title: '序号', align: 'center', width: '80px' },
        { title: '风险等级', slot: 'riskLevel', align: 'center' },
        { title: '纠纷大类', key: 'eventCategoryName', align: 'center' },
        { title: '纠纷小类', key: 'eventTypeName', align: 'center' },
        { title: '纠纷发生地', key: 'address', align: 'center' },
        { title: '纠纷描述', key: 'disputeDescription', align: 'center' },
        { title: '纠纷发生时间', key: 'occurrenceTime', align: 'center' },
        { title: '是否化解', slot: 'isResolve', align: 'center' },
        { title: '操作', fixed: 'right', slot: 'action', align: 'center' }
      ],
      columns: [
        { type: 'index', title: '序号', align: 'center', width: '80px' },
        { title: '纠纷大类', key: 'event_category_name', align: 'center' },
        { title: '统计月份', key: 'occurrence_date', align: 'center' },
        { title: '当期发生数量（年初以来）', key: 'year_num', align: 'center' },
        { title: '去年同比', slot: 'on_year', align: 'center' },
        { title: '上月环比', slot: 'link_relative_ratio', align: 'center' },
        { title: '风险状态', slot: 'type', align: 'center' },
        { title: '操作', slot: 'action', align: 'center' }
      ],
      dataList: [],
      params: {
        pageNo: 1,
        pageSize: 10,
        totalRecord: 0
      },
      params1: {
        pageNo: 1,
        pageSize: 10,
        totalRecord: 0,
        event_category_id: ''
      },
      id: ''
    }
  },
  mounted () {
    // this.getDictList('1')
    // this.getCityData('1')
    // this.getList()
  },
  methods: {
    getDictList (level, id) {
      selectDictList({ type: 'event_category', parent_id: id }).then(res => {
        if (res.data.errcode === 0) {
          this.dictData.list = res.data.data
          this.dictData['list' + level] = res.data.data
          return
        }
        this.$Message.error(res.errmsg)
      })
    },
    clearDispute (res) { // 清空纠纷大类
      console.log(res, 'dddddd')
      this.$nextTick(() => {
        // clearable后会直接把searchInfo里的该字段删除掉
        this.$set(this.searchInfo1, 'event_category_id', '')
        this.$set(this.searchInfo1, res, res === 'info' ? '{}' : '')
      })
    },
    changeDispute (item) {
      this.searchInfo1.event_category_id = JSON.parse(item).value
      this.getDictList('2', JSON.parse(item).id)
    },
    getCityData (level, id) {
      selectCityList({ parent_id: id }).then(res => {
        if (res.data.errcode === 0) {
          this.areaData['list' + level] = res.data.data
          return
        }
        this.$Message.error(res.errmsg)
      })
    },
    changeArea (level, parent_id) {
      this.getCityData(level, parent_id)
    },
    getList (item) {
      if (item) {
        this.params = Object.assign(this.params, item)
        if (this.params.start_time) {
          this.params.start_time = date2str(this.params.start_time)
        }
        if (this.params.end_time) {
          this.params.end_time = date2str(this.params.end_time)
        }
      }
      findFocusAreasTypeList(this.params).then(res => {
        if (res.data.errcode === 0) {
          this.dataList = res.data.data.results
          this.params.totalRecord = res.data.data.totalRecord
          return
        }
        this.$Message.error(res.data.errmsg)
      })
    },
    size (pageSize) {
      this.params.pageSize = pageSize
      this.changePageNo()
    },
    changePageNo (pageNo) {
      this.params.pageNo = pageNo
      this.getList(this.params)
    },
    searchData () { // 搜索
      this.getList(this.searchInfo)
    },
    getInfo (item) {
      console.log(item, 'dddd')
      this.getInfoList(item.id)
      this.id = item.event_category_id
      this.params1.event_category_id = item.event_category_id
      this.params1.occurrence_date = item.occurrence_date
      this.modal = true
      // this.dictData.list1 = []
      // this.dictData.list2 = []
    },
    getInfoList (id, item) {
      if (item) {
        this.params1 = Object.assign(this.params1, item)
        if (this.params1.occurrenceDate) {
          this.params1.occurrenceDate = date2str(this.params1.occurrenceDate)
        }
      } else {
        this.params1 = this.params1
      }
      findEventFocusAreasList(this.params1).then(res => {
        if (res.data.errcode === 0) {
          this.monthFocusList = res.data.data.results
          this.params1.totalRecord = res.data.data.totalRecord
          return
        }
        this.$Message.error(res.data.errmsg)
      })
    },
    size1 (pageSize) {
      this.params1.pageSize = pageSize
      this.changePageNo1()
    },
    changePageNo1 (pageNo) {
      this.params1.pageNo = pageNo
      this.getInfoList(this.params1)
    },
    searchInfoData () {
      this.params1.pageNo = 1
      this.params1.pageSize = 10
      this.params1.totalRecord = 0
      this.getInfoList(this.id, this.searchInfo1)
    },
    liftRisk (item) { // 解除风险
      if (confirm('确定解除风险吗？')) {
        updateFocusAreasType({ id: item.id, type: '2' }).then(res => {
          if (res.data.errcode === 0) {
            this.$Message.success('已成功解除风险')
            this.getList()
            return
          }
          this.$Message.error(res.data.errmsg)
        })
      }
    },
    getEventInfo (row) { // 详情页矛盾列表看详情
      this.$router.push({
        name: 'contradictionRegisterAdd',
        params: {
          what: 'edit',
          id: row.id,
          level: '',
          type: this.type
        }
      })
    }
  },
  computed: {
    serverUserInfo () {
      return this.$store.state.user.severUserInfo
    }
  }
}
</script>
<style scoped lang="less">
  .contradiction-div {
    padding: 10px;
  }
  .search-div {
    float: left;
    width: 100%;
    padding: 15px 30px 15px 15px;
    // min-height: 200px;
    border: 1px solid skyblue;
    border-radius: 5px;;
    background-color: #f6fdfd;
    margin-bottom: 15px;
  }
  .mb10 {
    margin-bottom: 10px;
  }
  .mt8 {
    margin-top: 8px;
  }
  .mr15 {
    margin-right: 15px;
  }
  .mr5 {
    margin-right: 5px;
  }
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .flex-div {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .level-option-img{
    width: 20px;
    height: 20px;
    vertical-align: middle;
    margin-bottom: 3px;
  }
  .head-title{
    width: 100%;
    height: 35px;
    font-size: 16px;
    line-height: 30px;
  }
</style>
