<template>
  <ai-list class="list">
    <ai-title slot="title" title="健康上报" v-if="search.areaId" isShowBottomBorder :instance="instance" :disabledLevel="disabledLevel" isShowArea v-model="search.areaId" @change="changeArea"></ai-title>
    <template slot="content">
      <div class="statistics-top">
        <div class="statistics-top__item">
          <span>上报人数</span>
          <h2 style="color: #2266FF;">{{ info.total }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>今日上报</span>
          <h2 style="color: #22AA99;">{{ info.today }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>今日未报</span>
          <h2 style="color: #F8B425">{{ info.unReport }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>今日异常</span>
          <h2 style="color: red">{{ info.unusual }}</h2>
        </div>
      </div>
      <div class="content">
        <ai-search-bar bottomBorder>
          <template #left>
            <ai-select
              v-model="search.checkResult"
              clearable
              placeholder="请选择检查结果"
              :selectList="dict.getDict('epidemicRecentTestResult')"
              @change="search.current = 1, getList()">
            </ai-select>
            <ai-select
              v-model="search.today"
              clearable
              placeholder="今日是否上报"
              :selectList="dictList"
              @change="search.current = 1, getList()">
            </ai-select>
            <ai-download :instance="instance" url="/app/appepidemicreportmember/export" :params="search" fileName="健康上报" :disabled="tableData.length == 0">
              <el-button icon="iconfont iconExported" :disabled="tableData.length == 0">导出</el-button>
            </ai-download>
          </template>
          <template #right>
            <el-input
              v-model="search.name"
              size="small"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="search.current = 1, getList()"
              @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: 16px;"
          :current.sync="search.current"
          :size.sync="search.size"
          @getList="getList">
          <el-table-column slot="healthCode" align="center" label="健康码">
            <template slot-scope="{ row }">
              <ai-uploader
                v-if="row.healthCode"
                :instance="instance"
                :value="JSON.parse(row.healthCode)"
                disabled
                :limit="1">
              </ai-uploader>
            </template>
          </el-table-column>
          <el-table-column slot="options" width="140px" fixed="right" label="操作" align="center">
            <template slot-scope="{ row }">
              <div class="table-options">
                <el-button type="text" @click="toDetail(row.id)">详情</el-button>
                <el-button type="text" @click="remove(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
      </div>
    </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: '',
          checkResult: '',
          areaId: '',
          today: ''
        },
        dictList: [{
          dictName: '否',
          dictValue: '0'
        }, {
          dictName: '是',
          dictValue: '1'
        }],
        info: {},
        colConfigs: [
          { prop: 'name', label: '姓名' },
          { prop: 'phone', align: 'center', label: '手机号码' },
          { prop: 'areaName', align: 'center', label: '所属地区', width: '200px' },
          { prop: 'reportTime', align: 'center', label: '上报时间', width: '200px' }, 
          {
            prop: 'vaccine',
            align: 'center',
            label: '已接种情况',
            render: (h, {row}) => {
              return h('span', {
                style: {
                  color: row.status === '0' ? 'red' : '#333'
                }
              }, row.today === '0' ? '-' : (row.vaccine || 0 + '次'))
            }
          }, 
          { prop: 'healthCode', align: 'center', label: '健康码', formart: v => v ? this.dict.getLabel('epidemicHealthCode', v) : '-' }, 
          { prop: 'checkTime', align: 'center', label: '核酸日期', formart: v => v ? v.split(' ')[0] : '-' }, 
          { prop: 'checkResult', align: 'center', label: '检测结果', formart: v => v ? this.dict.getLabel('epidemicRecentTestResult', v) : '-' }, 
          {
            prop: 'status',
            align: 'center',
            label: '健康状态',
            render: (h, {row}) => {
              return h('span', {
                style: {
                  color: row.status === '0' ? 'red' : '#333'
                }
              }, row.today === '0' ? '-' : (row.status === '0' ? '异常' : '正常'))
            }
          },
          { prop: 'today', align: 'center', label: '今日上报', formart: v => v === '0' ? '未上报' : '已上报' },
        ],
        tableData: [],
        total: 0,
        loading: false,
        disabledLevel: 0
      }
    },

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

      param () {
        return {

        }
      }
    },

    created () {
      this.disabledLevel = this.user.info.areaList.length - 1
      this.search.areaId = this.user.info.areaId
      this.loading = true
      this.dict.load(['epidemicTouchInFourteen', 'epidemicRecentHealth', 'epidemicRecentTestResult', 'epidemicHealthCode', 'epidemicVaccineTime']).then(() => {
        this.getList()
      })
    },

    methods: {
      getList () {
        this.instance.post(`/app/appepidemicreportmember/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
            this.loading = false
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
        
        this.getTotalInfo()
      },

      remove(id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appepidemicreportmember/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getTotalInfo()
              this.getList()
            }
          })
        })
      },

      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      },

      changeArea () {
        this.search.current = 1

        this.$nextTick(() => {
          this.getList()
          this.getTotalInfo()
        })
      },

      getTotalInfo () {
        this.instance.post(`/app/appepidemicreportmember/statistic`, null, {
          params: {
            areaId: this.search.areaId
          }
        }).then(res => {
          if (res.code == 0) {
            this.info = res.data
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .list {
    ::v-deep .ai-list__content {
      padding: 0!important;

      .ai-list__content--right-wrapper {
        background: transparent!important;
        box-shadow: none!important;
        margin: 0!important;
        padding: 12px 16px 12px!important;
      }
    }
    .statistics-top {
      display: flex;
      align-items: center;
      margin-bottom: 20px;

      & > div {
        flex: 1;
        height: 96px;
        line-height: 1;
        margin-right: 20px;
        padding: 16px 24px;
        background: #FFFFFF;
        box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
        border-radius: 4px;

        &:last-child {
          margin-right: 0;
        }

        h3 {
          font-size: 24px;
        }

        span {
          display: block;
          margin-bottom: 16px;
          color: #888888;
          font-size: 16px;
        }
      }
    }

    .content {
      padding: 16px;
      background: #FFFFFF;
      box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
    }
  }
</style>
