<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.familyList.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;">
            <sys-origin-icon
              :icon="item.value"
              :desc="item.value"
            /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>
      <div class="filter-item">
        <account-input
          v-model="listQuery.userId"
          :sys-origin="listQuery.sysOrigin"
          :placeholder="$t('pages.familyList.filter.userId')"
        />
      </div>
      <el-input
        v-model.trim="listQuery.familyAccount"
        :placeholder="$t('pages.familyList.filter.familyAccount')"
        style="width: 200px;"
        class="filter-item"
      />
      <div class="filter-item">
        <el-date-picker
          v-model="rangeDate"
          value-format="timestamp"
          type="datetimerange"
          :picker-options="pickerOptions"
          :range-separator="$t('pages.familyList.dateRange.separator')"
          :start-placeholder="$t('pages.familyList.dateRange.start')"
          :end-placeholder="$t('pages.familyList.dateRange.end')"
        />
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="searchDisabled"
        @click="handleSearch"
      >
        {{ $t('pages.familyList.action.search') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.familyList.loading')"
      fit
      highlight-current-row
    >
      <el-table-column prop="sysOrigin" :label="$t('pages.familyList.table.system')" align="center">
        <template slot-scope="scope">
          <sys-origin-icon
            :icon="scope.row.sysOrigin"
            :desc="scope.row.sysOrigin"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.familyList.table.owner')" align="center">
        <template slot-scope="scope">
          <el-link
            v-if="scope.row.userBaseInfo.id"
            @click="queryUserDetails(scope.row.userBaseInfo.id)"
          >
            {{ scope.row.userBaseInfo.userNickname }} /
            {{ scope.row.userBaseInfo.account }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.familyList.table.familyAccount')" prop="familyAccount" align="center" />
      <el-table-column :label="$t('pages.familyList.table.familyAvatar')" align="center" width="150">
        <template slot-scope="scope">
          <avatar :url="scope.row.familyAvatar" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.familyList.table.familyName')" prop="familyName" align="center" />
      <el-table-column :label="$t('pages.familyList.table.memberCount')" prop="memberCount" align="center" />
      <el-table-column :label="$t('pages.familyList.table.familyLevel')" align="center">
        <template slot-scope="scope">
          <span v-for="(item, index) in familyLevelKeyOptions" :key="index">
            <span v-if="item.value == scope.row.levelKey">{{ item.name }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.familyList.table.totalContribution')" prop="familyExp" align="center" />
      <el-table-column
        prop="createTime"
        :label="$t('pages.familyList.table.createdAt')"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.familyList.table.actions')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click.native="handleFamilyMember(scope.row)"
          >{{ $t('pages.familyList.action.memberList') }}</el-button>
          <el-button
            type="text"
            @click.native="delFamilyData(scope.row)"
          >{{ $t('pages.familyList.action.disbandFamily') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <user-deatils-drawer
      v-if="userDeatilsDrawerVisible"
      :user-id="thatSelectedUserId"
      @close="userDeatilsDrawerVisible = false"
    />

    <family-member
      v-if="familyMemberDialogVisible"
      :family-id="thatSelectedFamilyId"
      @close="familyMemberDialogVisible = false"
    />
  </div>
</template>

<script>
import { pageFamily, delFamily } from '@/api/family'
import Pagination from '@/components/Pagination'
import { pickerOptions } from '@/constant/el-const'
import { familyLevelKeys } from '@/constant/family'
import FamilyMember from '@/views/family/list/family-member'
import { mapGetters } from 'vuex'

export default {
  name: 'FamilyList',
  components: { Pagination, FamilyMember },
  data() {
    return {
      searchDisabled: false,
      pickerOptions,
      userDeatilsDrawerVisible: false,
      thatSelectedUserId: '',
      familyMemberDialogVisible: false,
      thatSelectedFamilyId: '',
      list: [],
      total: 0,
      rangeDate: [],
      listQuery: {
        cursor: 1,
        limit: 20,
        userId: '',
        familyAccount: '',
        startTime: '',
        endTime: '',
        sysOrigin: ''
      },
      listLoading: true,
      clickUserId: ''
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms']),
    familyLevelKeyOptions() {
      return familyLevelKeys.map(item => ({
        ...item,
        name: this.$t(`pages.familyConfig.levelKey.${item.value}`)
      }))
    }
  },
  watch: {
    rangeDate: {
      immediate: true,
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.listQuery.startTime = newVal[0]
          this.listQuery.endTime = newVal[1]
          return
        }
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.listQuery.sysOrigin = querySystem.value
    that.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      if (isClean === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      pageFamily(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    queryUserDetails(id) {
      this.userDeatilsDrawerVisible = true
      this.thatSelectedUserId = id
    },
    handleFamilyMember(row) {
      this.thatSelectedFamilyId = String(row.id)
      this.familyMemberDialogVisible = true
    },
    delFamilyData(item) {
      const that = this
      that
        .$confirm(this.$t('pages.familyList.confirm.disbandFamily'), this.$t('pages.familyList.confirm.title'), {
          type: 'warning',
          confirmButtonText: this.$t('pages.familyList.confirm.confirm'),
          cancelButtonText: this.$t('pages.familyList.confirm.cancel')
        })
        .then(() => {
          that.$message({
            message: this.$t('pages.familyList.message.disbanding'),
            type: 'warning'
          })
          that.listLoading = true
          delFamily(item.id)
            .then(res => {
              that.listLoading = false
              that.$message({
                message: this.$t('pages.familyList.message.disbandSuccess'),
                type: 'success'
              })
              that.renderData()
            })
            .catch(er => {
              console.error(er)
              that.listLoading = false
            })
        })
        .catch(er => {
          console.error(er)
          that.listLoading = false
        })
    }
  }
}
</script>
<style scoped lang="scss">
.store-table-expand {
  font-size: 0;
  label {
    width: 90px;
    color: #99a9bf;
    .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 50%;
    }
  }
}
</style>
