<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" v-if="currentTabs == 0">
          <div class="currentLeft-top">
            <div class="left">
              <img src="./components/images/icon2.png" alt="" />

              <AiAreaPicker v-model="areaId" ref="areaIds" :areaId="areaId" @select="areaSelect" style="color: #fff"></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)">
              <template #custom>
                <div class="cards-left">
                  <span class="walkName">走访对象：{{ item.createUserName }}</span>
                  <span class="tags">残疾人</span>
                  <span class="tags">居家看护</span>
                </div>

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

                <div class="imgs">
                  <img :src="items.url" alt="" v-for="(items, i) in item.images" :key="i" @click.stop="previewImage(item.images, items.url)" />
                </div>

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

              <template #menu>
                <div class="menu" @tap.stop="goDetail(item, 2)">编辑</div>
                <div class="menu" @tap.stop="toDetele(item)">删除</div>
              </template>
            </AiCard>
          </template>

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

            <div class="addBtn">点击<span class="toAdds" @click="goDetail">新增按钮</span>试试试吧~</div>
          </div>
        </div>

        <div class="currentRight" v-if="currentTabs == 1">
          <div class="currentLeft-top">
            <div class="left">
              <img src="./components/images/icon2.png" alt="" />

              <AiAreaPicker v-model="areaId" ref="areaIds" :areaId="areaId" @select="areaSelect" style="color: #fff"></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)">
              <template #custom>
                <div class="cards-left">
                  <span class="walkName">走访对象：{{ item.createUserName }}</span>
                  <span class="tags">残疾人</span>
                  <span class="tags">居家看护</span>
                </div>

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

                <div class="imgs">
                  <img :src="items.url" alt="" v-for="(items, i) in item.images" :key="i" @click.stop="previewImage(item.images, items.url)" />
                </div>

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

              <template #menu>
                <div class="menu" @tap.stop="goDetail(item, 2)">编辑</div>
                <div class="menu" @tap.stop="toDetele(item)">删除</div>
              </template>
            </AiCard>
          </template>

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

            <div class="addBtn">点击<span class="toAdds" @click="goDetail">新增按钮</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>

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

    <component v-else :is="comp" :params="params"></component>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import add from './add.vue'
import detail from './detail.vue'

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

            if (this.data) {
              this.data.map((item) => {
                if (item.images) {
                  item.images = JSON.parse(item.images || '[]')
                }
                return item
              })
            }
          }
        })
    },

    areaSelect(e) {
      if (e.type == 5) {
        this.areaId = e.id
      } else {
        return this.$u.toast('请选择到村')
      }
    },

    change(index) {
      this.areaId = this.user.areaId
      this.keyword = ''
      this.currentTabs = index
      this.getList()
    },

    goDetail(item, hint) {
      this.isList = false
      if (hint == 1 || hint == 2) {
        console.log('left', item.id, hint)
        this.comp = hint == 1 ? 'detail' : 'add'
        this.params = item
      } else {
        console.log('right', 2)
        this.comp = 'add'
        this.params = 111
      }
    },

    toDetele(item) {
      this.deletShow = true
      this.deletId = item.id
    },

    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,
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.AppWalkask {
  height: 100%;

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

      .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: 12px;
                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;
            }
          }
        }
      }

      .addBtn {
        text-align: center;
        color: #b7b7b7;
        margin-top: 5px;
        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;
  }

  .fixedImg {
    position: fixed;
    right: 0;
    top: 1004px;
    width: 128px;
    height: 128px;
  }
}
</style>
