<template>
  <div class="AppWalkask">
    <template v-if="isList">
      <div class="header-top">
        <u-tabs :list="tabList" :is-scroll="false" :current="currentTabs" height="96" bg-color="#3975C6" inactive-color="#fff" active-color="#fff" @change="change"></u-tabs>
      </div>

      <div class="header-middle">
        <div class="currentLeft">
          <div class="currentLeft-top">
            <div class="left">
              <AiAreaPicker v-model="areaId" :areaId="user.areaId" @select="areaSelect" :name.sync="areaName" style="color: #fff">
                <img :src="$cdn + 'location.svg'" alt="" style="width: 18px; height: 18px; margin-right: 4px; vertical-align: middle" />
                <span class="label" v-if="areaName">{{ areaName }}</span>
                <span v-else>请选择</span>
                <u-icon name="arrow-down" color="#fff" size="28" style="margin-left: 4px" />
              </AiAreaPicker>
            </div>

            <u-search v-model="keyword" :clearabled="true" placeholder="请输入标题" :show-action="false" bg-color="#1F5CAF" search-icon-color="#E2E8F1" color="#E2E8F1" height="58" @search="handerSearch" @clear="handerClear"></u-search>
          </div>

          <template v-if="data.length > 0">
            <AiCard v-for="(item, i) in data" :key="i" @click.native="goDetail(item, 1)" :ref="item.id">
              <template #custom>
                <div class="cards-left">
                  <span class="walkName">走访对象：{{ item.name }}</span>
                  <span class="tags" v-if="item.menuLevel3Name">{{ item.menuLevel3Name }}</span>
                  <span class="tags" v-if="item.reality">{{ $dict.getLabel('realityStatus', item.reality) }}</span>
                </div>

                <div class="cards-hint">{{ item.title }}</div>

                <div class="imgs">
                  <img :src="items.url" alt="" v-for="(items, i) in JSON.parse(item.images || '[]')" :key="i" @click.stop="previewImage(JSON.parse(item.images || '[]'), items.url)" v-if="i < 3" />
                </div>

                <div class="cards-bottom">
                  <span>走访人：{{ item.createUserName }}</span>
                  <span style="margin-left: 8px">{{ item.createTime }}</span>
                </div>
              </template>

              <template #menu v-if="item.createUserId == user.id">
                <div class="menu" @tap.stop="toAdd(item, 2)">编辑</div>
                <div class="menu" @tap.stop="toDetele(item)">删除</div>
              </template>
            </AiCard>
          </template>

          <div v-else>
            <AiEmpty description="您还未添加过入户走访慰问" class="emptyWrap"></AiEmpty>

            <div class="addBtns">
              <span> 点击</span>
              <span class="toAdds" @click="toAdd()">新增按钮</span>
              <span> 试试试吧~</span>
            </div>
          </div>
        </div>
      </div>

      <u-modal v-model="deletShow" content="您确认要删除该条信息吗？" :show-cancel-button="true" :mask-close-able="true" :show-title="false" @confirm="delet"></u-modal>

      <div class="addBtn iconfont iconfont-iconfangda" @tap.stop="toAdd()" />
    </template>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'AppWalkask',
  appName: '走访慰问',
  components: {},
  props: {},
  data() {
    return {
      data: [],
      tabList: [
        {
          name: '全部',
        },
        {
          name: '我的走访',
        },
      ],
      currentTabs: 0,
      show: false,
      keyword: '',
      deletShow: false,
      isList: true,
      comp: '',
      params: null,
      current: 1,
      areaId: '',
      areaName: '',
      size: 10,
      deletId: '',
      pages: 0,
    }
  },
  computed: {
    ...mapState(['user']),
  },
  onLoad() {
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    this.$dict.load('realityStatus').then(() => {
      this.getList()
    })
    uni.$on('updateList', () => {
      this.current = 1
      this.getList()
    })
  },
  onShow() {
    document.title = '走访慰问'
  },
  mounted() {},
  methods: {
    getList() {
      this.$http
        .post('/app/appvisitvondolence/list', null, {
          params: {
            size: this.size,
            current: this.current,
            createUserId: this.currentTabs == 1 ? this.user.id : '',
            title: this.keyword,
          },
        })
        .then((res) => {
          if (res.code == 0) {
            if (this.current > res.data.pages) {
              return
            }
            this.data = this.current > 1 ? [...this.data, ...res.data.records] : res.data.records
            this.pages = res.data.pages
          }
        })
    },

    areaSelect(e) {
      this.data = []
      this.areaId = e
      this.current = 1
      this.getList()
    },

    change(index) {
      this.data = []
      this.areaId = this.user.areaId
      this.keyword = ''
      this.currentTabs = index
      this.current = 1
      this.getList()
    },

    goDetail(item, hint) {
      if (item?.id) {
        this.$refs?.[item.id]?.[0]?.handleClose()
      }
      if (hint == 1) {
        uni.navigateTo({ url: `./detail?id=${item.id}` })
      }
    },

    toAdd(item, hint) {
      if (item?.id) {
        this.$refs?.[item.id]?.[0]?.handleClose()
      }
      if (hint == 2) {
        uni.navigateTo({ url: `./add?id=${item.id}` })
      } else {
        uni.navigateTo({ url: `./add` })
      }
    },

    toDetele(item) {
      this.deletShow = true
      this.deletId = item.id
      this.$refs?.[item.id]?.[0]?.handleClose()
    },

    delet() {
      this.$http.post(`/app/appvisitvondolence/delete?ids=${this.deletId}`).then((res) => {
        if (res.code == 0) {
          this.$u.toast('删除成功！')
          this.getList()
        }
      })
    },

    handerSearch(e) {
      this.keyword = e
      this.current = 1
      this.getList()
    },

    handerClear() {
      this.keyword = ''
      this.current = 1
      this.getList()
    },

    previewImage(images, img) {
      uni.previewImage({
        urls: images.map((v) => v.url),
        current: img,
      })
    },
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
uni-page-body {
  height: 100%;
  background: #fff;
}

.AppWalkask {
  height: 100%;

  .header-middle {
    .currentLeft,
    .currentRight {
      padding-bottom: 56px;

      .currentLeft-top {
        display: flex;
        align-items: center;
        background: #3975c6;
        padding: 24px 30px;

        .left {
          width: 40%;
          display: flex;
          align-items: center;

          img {
            width: 48px;
            height: 48px;
          }
        }
      }

      .AiCard {
        ::v-deep .start {
          border-bottom: 1px solid #dddddd;

          .fill {
            .cards-left {
              .walkName {
                font-size: 32px;
                font-weight: 500;
                margin-right: 16px;
                color: #333;
              }

              .tags {
                display: inline-block;
                padding: 6px 16px;
                background: #f3f4f7;
                border-radius: 4px;
                font-size: 28px;
                color: #999999;
                margin-left: 16px;
              }
            }

            .cards-hint {
              margin-top: 16px;
              font-size: 30px;
              color: #666666;
              line-height: 1.4;
              text-overflow: -o-ellipsis-lastline;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 2;
              -webkit-box-orient: vertical;
            }

            .imgs {
              margin-top: 20px;

              img {
                width: 221px;
                height: 221px;
                margin-right: 8px;
              }

              img:nth-child(3n + 0) {
                margin-right: 0;
              }
            }

            .cards-bottom {
              margin: 24px 0 16px 0;
              font-size: 24px;
              color: #999999;
            }
          }
        }

        ::v-deep .mask {
          .moreMenu {
            .menu {
              text-align: center;
              line-height: 80px;
              width: 192px;
              height: 80px;
              font-size: 28px;
              font-weight: 400;
              color: #333333;
            }
          }
        }
      }

      .addBtns {
        text-align: center;
        color: #b7b7b7;
        margin-top: 10px;
        font-weight: 800;

        .toAdds {
          color: #467dfe;
        }
      }
    }
  }

  .addBtn {
    width: 96px;
    height: 96px;
    flex-shrink: 0;
    background: $uni-color-primary;
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
    font-size: 48px;
    color: #fff;
    border-radius: 50%;
    justify-content: center;
    align-items: center;
    display: flex;
    position: fixed;
    right: 6px;
    bottom: 106px;
    z-index: 9;
  }
}
</style>
