<template>
  <ai-list class="early-warning">
    <template slot="title">
      <ai-title title="SOS求助" isShowArea isShowBottomBorder v-model="search.areaId" :instance="instance" @change="search.current = 1, getList()">
      </ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-date-picker
            @change="search.current = 1, getList()"
            v-model="search.createTimeRange"
            type="daterange"
            size="small"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </template>
        <template slot="right">
          <el-input
            v-model="search.name"
            size="small"
            v-throttle="() => {search.current = 1, getList()}"
            placeholder="请输入成员姓名、设备号"
            clearable
            @clear="search.current = 1, search.name = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        v-loading="loading"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="options" width="160px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="$router.push({name: '监护地图', query: {id: row.deviceId, lat: row.lat, lng: row.lng}})">地图查看</el-button>
              <el-button type="text" @click="toMonitor(row.deviceId)">监测数据</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'List',

    props: {
      instance: Function,
      dict: Object
    },

    data() {
      return {
        search: {
          current: 1,
          size: 10,
          name: '',
          areaId: '',
          createTimeRange: []
        },
        currIndex: 0,
        isShow: false,
        loading: false,
        total: 0,
        colConfigs: [
          { prop: 'name', label: '姓名' },
          { prop: 'mid', label: '设备号' },
          {
            prop: 'departmentNames',
            label: '年龄',
            align: 'center',
            render: (h, { row }) => {
              return h('span', {}, this.getIdInfo(row.idNumber, 3))
            }
          },
          { prop: 'sex', align: 'center', label: '性别', formart: v => v === '1' ? '男' : '女' },
          { prop: 'phone', align: 'center', label: '联系方式' },
          { prop: 'areaName', align: 'center', label: '所属地区' },
          { prop: 'type', align: 'center', label: '预警类型' },
          { prop: 'gpsDesc', align: 'center', width: 150, label: '预警地点' },
          { prop: 'createTime', align: 'center', label: '预警时间' }
        ],
        tableData: []
      }
    },

    computed: {
      ...mapState(['user'])
    },

    mounted() {
      this.search.areaId = this.user.info.areaId
      this.getList()
    },

    methods: {
      getList () {
        this.loading = true
        this.instance.post(`/app/appintelligentguardianshipalarm/list`, null, {
          params: {
            ...this.search,
            item: 5,
            type: 1,
            createTimeRange: (this.search.createTimeRange && this.search.createTimeRange.length) ? this.search.createTimeRange.join(',') : ','
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records.map(v => {
              return {
                ...v,
                type: 'SOS'
              }
            })
            this.total = res.data.total

            this.$nextTick(() => {
              this.loading = false
            })
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      },

      getIdInfo (UUserCard, num) {
        if (num == 1) {
          var birth = UUserCard.substring(6, 10) + '-' + UUserCard.substring(10, 12) + '-' + UUserCard.substring(12, 14)
          return birth
        }
        if (num == 2) {
          if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
            return '1'
          } else {
            return '0'
          }
        }

        if (num == 3) {
          var myDate = new Date()
          var month = myDate.getMonth() + 1
          var day = myDate.getDate()
          var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
          if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
            age ++
          }

          return age
        }
      },

      onConfirm () {

      },

      toMonitor (id) {
        this.$emit('change', {
          type: 'Monitor',
          params: {
            id: id || ''
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.early-warning {
  .rules-form {
    margin-top: 20px;
  }
}
</style>
