<template>
  <ai-detail class="AppResidentManage">
    <template slot="title">
      <ai-title title="居民详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
    </template>
    <template slot="content">
      <div class="detail-top">
        <div class="detail-top__header">
          <div class="header-left">
            <img :src="info.avatar">
            <div class="header-left__right">
              <h2>{{ info.name }}</h2>
              <p>{{ info.corpFullName }}</p>
            </div>
          </div>
          <div class="header-right">
            <div class="header-right__item">
              <span>家庭积分</span>
              <h3>{{ info.residentInfo ? (info.residentInfo.resident.familyIntegral || 0) : '0' }}</h3>
            </div>
            <div class="header-right__item">
              <span>个人积分</span>
              <h3>{{ info.residentInfo ? (info.residentInfo.resident.personalIntegral || 0) : '0' }}</h3>
            </div>
            <el-button type="primary" size="mini" v-if="info.realName" @click="toDetail">查看居民档案</el-button>
          </div>
        </div>
        <div class="detail-top__content">
          <ai-wrapper
            label-width="80px">
            <ai-info-item label="添加渠道" :value="dict.getLabel('wxCustomerAddWay', info.addWay)"></ai-info-item>
            <ai-info-item label="添加时间" :value="info.createTime"></ai-info-item>
            <ai-info-item label="真实姓名" :value="info.realName"></ai-info-item>
            <ai-info-item label="手机号码"
                          :value="info.residentInfo ? info.residentInfo.resident.phone : '-'"></ai-info-item>
            <ai-info-item label="标签" isLine>
              <div class="table-tags">
                <el-tag type="info" v-for="(item, index) in info.tags" size="small" :key="index">{{ item.tagName }}
                </el-tag>
              </div>
            </ai-info-item>
          </ai-wrapper>
        </div>
      </div>
      <div class="detail-bottom">
        <div class="detail-bottom__left">
          <h2>居民动态</h2>
          <div class="step-list">
            <div class="step-item" v-for="(item, index) in logList" :key="index">
              <i class="step-item__left"></i>
              <div class="step-item__right">
                <h2>{{ dict.getLabel('wxCustomerLogType', item.type) }}</h2>
                <p>{{ item.createTime }}</p>
                <div class="step-item__right--bottom" v-html="item.content"></div>
              </div>
            </div>
            <div slot="empty" class="no-data" v-if="!logList.length" style="height:160px;"></div>
          </div>
        </div>
        <div class="detail-bottom__right">
          <div class="detail-table">
            <h2>所属员工</h2>
            <ai-table
              class="detail-table__table"
              :border="true"
              :tableData="tableData"
              :col-configs="colConfigs"
              :total="total"
              :stripe="false"
              :current.sync="search.current"
              :size.sync="search.size"
              @getList="getDynamicInfo">
              <el-table-column slot="userinfo" label="所属员工" width="268px" align="center">
                <template slot-scope="{ row }">
                  <div class="userinfo">
                    <img :src="row.avatar">
                    <h3>{{ row.userName }}</h3>
                  </div>
                </template>
              </el-table-column>
            </ai-table>
          </div>
          <div class="detail-table">
            <h2>所在群聊</h2>
            <ai-table
              class="detail-table__table"
              :border="true"
              :tableData="grooupTableData"
              :col-configs="groupColConfigs"
              :total="grooupTableData.length"
              :stripe="false"
              :current.sync="groupSearch.current"
              :size.sync="groupSearch.size"
              @getList="getInfo">
            </ai-table>
          </div>
        </div>
      </div>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

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

    data() {
      return {
        isShow: false,
        tags: ['青年', '郑村村', '郑村村', '青年', '郑村村', '郑村村'],
        info: {},
        search: {
          current: 1,
          size: 10
        },
        groupSearch: {
          current: 1,
          size: 10
        },
        groupTotal: 0,
        form: {
          explain: '',
          imgs: []
        },
        total: 0,
        logList: [],
        colConfigs: [
          {slot: 'userinfo'},
          {prop: 'addWay', label: '客户来源', align: 'center', formart: v => this.dict.getLabel('wxCustomerAddWay', v)},
          {prop: 'createTime', label: '添加时间', align: 'center'}
        ],
        groupColConfigs: [
          {prop: 'name', label: '群名称', align: 'left'},
          {prop: 'ownerName', label: '群主', align: 'center'},
          {prop: 'personCount', label: '群人数', align: 'left'},
          {prop: 'joinTime', label: '居民入群时间', align: 'left'}
        ],
        grooupTableData: [],
        tableData: []
      }
    },

    created() {
      if (this.params && this.params.id) {
        this.getInfo()
        this.dict.load(['wxCustomerLogType', 'wxCustomerAddWay']).then(() => {
          this.getWxcustomerlog()
          this.getDynamicInfo()
        })
      }
    },

    methods: {
      getInfo(id) {
        this.instance.post(`/app/wxcp/wxcustomer/queryCustomerById?id=${this.params.id}`).then(res => {
          if (res.code === 0) {
            this.info = res.data
            this.grooupTableData = res.data.groupInfos
          }
        })
      },

      getDynamicInfo(id) {
        this.instance.post(`/app/wxcp/wxcustomer/getDynamicInfoById?customerId=${this.params.id}`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code === 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      getWxcustomerlog() {
        this.instance.post(`/app/wxcp/wxcustomerlog/listAll?customerId=${this.params.id}`).then(res => {
          if (res.code === 0) {
            this.logList = res.data
          }
        })
      },

      toDetail() {
        this.$router.push({
          name: '居民档案',
          query: {
            id: this.info.residentInfo.resident.id,
            type: '0'
          }
        })
      },

      onClose() {
        this.form.explain = ''
      },

      confirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appgirdmemberevent/rejectEvent`, {
              eventId: this.params.id,
              explain: this.form.explain
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('驳回成功')
                setTimeout(() => {
                  this.cancel()
                }, 600)
              }
            })
          }
        })
      },

      cancel(isRefresh) {
        this.$emit('change', {
          type: 'list',
          isRefresh: isRefresh ? true : false
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .AppResidentManage {
    ::v-deep .ai-detail__content--wrapper {
      max-width: 100% !important;
      padding: 20px;
    }
    h2, h3 {
      margin: 0;
    }

    .userinfo {
      display: flex;
      align-items: center;
      padding-left: 16px;

      img {
        width: 40px;
        height: 40px;
        margin-right: 8px;
        border-radius: 2px;
      }

      h3 {
        font-weight: normal;
        color: #222222;
      }
    }

    .detail-bottom {
      display: flex;
      margin-top: 20px;

      .step-list {
        padding: 36px 40px 40px;

        .step-item {
          display: flex;
          position: relative;
          padding-bottom: 36px;

          &:last-child {
            padding-bottom: 0;

            &::after {
              display: none;
            }
          }

          &::after {
            position: absolute;
            left: 3px;
            top: 14px;
            width: 2px;
            height: 100%;
            background: #EEEEEE;
            content: ' ';
          }

          .step-item__right--bottom {
            color: #555;
            font-size: 14px;

            b {
              font-weight: normal;
            }

            ::v-deep i {
              font-style: normal;
              color: #2266FF;
            }
          }

          .step-item__right {
            flex: 1;

            h2 {
              line-height: 22px;
              color: #222222;
              font-size: 16px;
            }

            p {
              line-height: 22px;
              margin: 2px 0 4px;
              color: #888888;
              font-size: 14px;
            }
          }

          .step-item__left {
            position: relative;
            top: 4px;
            margin-right: 20px;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #2266FF;
          }
        }
      }

      .detail-bottom__left {
        flex-shrink: 1;
        width: 360px;
        height: fit-content;
        margin-right: 20px;
        background: #FFFFFF;
        box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
        border-radius: 2px;

        & > h2 {
          height: 56px;
          line-height: 56px;
          padding: 0 16px;
          color: #222222;
          font-size: 16px;
          font-weight: 700;
          border: 1px solid #EEEEEE;
        }
      }

      .detail-bottom__right {
        flex: 1;

        & > div {
          background: #FFFFFF;
          box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
          border-radius: 2px;

          &:last-child {
            margin-top: 20px;
          }

          & > h2 {
            height: 56px;
            line-height: 56px;
            padding: 0 16px;
            color: #222222;
            font-size: 16px;
            font-weight: 700;
            border: 1px solid #EEEEEE;
          }
        }

        .detail-table__table {
          padding: 16px;
        }
      }
    }

    .detail-top {
      padding: 30px 40px;
      background: #FFFFFF;
      box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
      border-radius: 2px;

      .detail-top__content {
        padding-top: 32px;
      }

      .detail-top__header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 32px;
        border-bottom: 1px solid #EEEEEE;

        .header-right {
          .header-right__item {
            width: 120px;
            margin-right: 8px;
            text-align: center;
          }

          div {
            text-align: center;

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

            span {
              display: block;
              margin-bottom: 10px;
              color: #888888;
            }
          }

          .el-button {
            height: 28px;
            margin-left: 8px;
            border-radius: 14px;
            font-size: 12px;
            padding: 7px 15px;
          }
        }

        .header-left, .header-right {
          display: flex;
          align-items: center;
        }

        .header-left {
          img {
            width: 64px;
            height: 64px;
            margin-right: 16px;
          }

          h2 {
            margin-bottom: 6px;
            color: #222222;
            font-size: 16px;
          }

          p {
            color: #2EA222;
            font-size: 14px;
          }
        }
      }
    }

    .table-tags {
      .el-tag {
        margin-right: 8px;

        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
</style>
