<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.status"
        :placeholder="$t('pages.userPhotoWallApproval.filter.status')"
        style="width: 240px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in approvalStatus"
          :key="item.value"
          :label="approvalStatusName(item.value)"
          :value="item.value"
        />
      </el-select>
      <div class="filter-item">
        <el-date-picker
          v-model="rangeDate"
          type="datetimerange"
          value-format="timestamp"
          :picker-options="pickerOptions"
          :range-separator="$t('pages.userPhotoWallApproval.date.rangeSeparator')"
          :start-placeholder="$t('pages.userPhotoWallApproval.date.start')"
          :end-placeholder="$t('pages.userPhotoWallApproval.date.end')"
          align="right"
        />
      </div>
      <div class="filter-item">
        <account-input v-model="listQuery.userId" :placeholder="$t('pages.userPhotoWallApproval.filter.userId')" />
      </div>
      <el-input
        v-model.trim="listQuery.userNickname"
        :placeholder="$t('pages.userPhotoWallApproval.filter.nickname')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-select
        v-model="listQuery.userSex"
        :placeholder="$t('pages.userPhotoWallApproval.filter.gender')"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in genders"
          :key="item.value"
          :label="genderName(item.value)"
          :value="item.value"
        />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="searchDisabled"
        @click="handleSearch"
      >
        {{ $t('pages.userPhotoWallApproval.action.search') }}
      </el-button>
    </div>
    <el-checkbox
      v-model="checkAll"
      :indeterminate="isIndeterminate"
      @change="handleCheckAllChange"
    >{{ $t('pages.userPhotoWallApproval.selection.all') }}</el-checkbox>
    <div style="margin: 15px 0;" />
    <el-row v-loading="listLoading" :gutter="10">
      <el-col
        v-for="(item, index) in list"
        :key="index"
        :span="4"
        style="margin-bottom: 10px;"
      >
        <el-card :body-style="{ padding: '0px' }">
          <div style="position: relative;">
            <el-image
              style="width:100%;height:300px;"
              :src="item.resourceUrl"
              :preview-src-list="[item.resourceUrl]"
            />
          </div>
          <div style="padding: 14px">
            <div class="bottom">
              <el-checkbox-group v-model="checkList">
                <el-checkbox
                  v-if="listQuery.status === 'SUSPECTED'"
                  :key="item.id"
                  :label="index"
                >{{ $t('pages.userPhotoWallApproval.selection.selected') }}</el-checkbox>
              </el-checkbox-group>
              <!--<el-table-column type="radio" width="55" />-->
              <div class="info" @click="handleClickCard(index)">
                <div>
                  {{ $t('pages.userPhotoWallApproval.label.nickname') }}:
                  <a
                    class="underline"
                    @click.stop="queryUserDetails(item.userId)"
                  >{{ item.userNickname }}</a>
                </div>
                <div class="line">
                  <div>{{ $t('pages.userPhotoWallApproval.label.gender') }}: {{ genderName(item.userSex, item.userSexName) }}</div>
                  <div>{{ $t('pages.userPhotoWallApproval.label.age') }}: {{ item.age }}</div>
                </div>
                <div>
                  {{ $t('pages.userPhotoWallApproval.label.tags') }}:
                  <el-tooltip placement="top-start">
                    <div slot="content">
                      <span v-html="cutout(item.labelNames)" />
                    </div>
                    <span>{{ item.labelNames }}</span>
                  </el-tooltip>
                </div>
              </div>
              <el-button
                type="text"
                class="button"
                @click="editUserForm(item.userId)"
              >{{ $t('pages.userPhotoWallApproval.action.editUser') }}</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div v-if="listQuery.status === 'SUSPECTED'" style="text-align:center;">
      <div style="padding:5px;font-size:14px;color:#909399">
        {{ $t('pages.userPhotoWallApproval.label.selectedCount') }}:<span style="color:#F00">{{ checkList.length }}</span>
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-folder-checked"
        @click="pass"
      >
        {{ $t('pages.userPhotoWallApproval.action.pass') }}
      </el-button>

      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-folder-delete"
        @click="notpass"
      >
        {{ $t('pages.userPhotoWallApproval.action.reject') }}
      </el-button>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

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

    <edit-user-form
      v-if="editUserFormVisible"
      :user-id="thatSelectedUserId"
      @close="editUserFormVisible = false"
      @success="renderDataSuccess"
    />
  </div>
</template>
<script>
import { getPotoWallTableSeach } from '@/api/app-user'
import {
  approvalPhotoWallPass,
  approvalPhotoWallNotPass
} from '@/api/approval'
import Pagination from '@/components/Pagination'
import { approvalStatus, genders } from '@/constant/user'
import EditUserForm from '@/components/data/EditUserForm'
import { pickerOptions } from '@/constant/el-const'
export default {
  name: 'UserPhotoWallApproval',
  components: { Pagination, EditUserForm },
  data() {
    return {
      searchDisabled: false,
      pickerOptions,
      approvalStatus,
      genders,
      userDeatilsDrawer: false,
      thatSelectedUserId: '',
      list: [],
      checkList: [],
      checkAll: false,
      isIndeterminate: false,
      editUserFormVisible: false,
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 30,
        userId: '',
        userNickname: '',
        userSex: '',
        status: 'SUSPECTED',
        startTime: '',
        endTime: ''
      },
      rangeDate: '',
      listLoading: true
    }
  },
  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() {
    this.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      that.listLoading = true
      that.checkList = []
      that.isIndeterminate = true
      if (isClean === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      getPotoWallTableSeach(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    // Format comma-separated labels for tooltip display.
    cutout(cellValue) {
      if (cellValue != null) {
        return cellValue.replace(/\,/g, '</br>')
      }
    },
    handleCheckAllChange(val) {
      const that = this
      that.checkList = []
      if (val) {
        that.list.forEach((item, index) => {
          that.checkList.push(index)
        })
      }
      this.isIndeterminate = false
    },
    pass() {
      const that = this
      if (that.checkList.length === 0) {
        that.$opsMessage.warn(that.$t('pages.userPhotoWallApproval.message.selectApprovalItem'))
        return
      }
      that
        .$confirm(that.$t('pages.userPhotoWallApproval.message.confirmApproval'), that.$t('pages.userPhotoWallApproval.confirm.title'), {
          type: 'warning'
        })
        .then(() => {
          that.listLoading = true
          approvalPhotoWallPass(that.getApprovalParams())
            .then(res => {
              that.listLoading = false
              that.$opsMessage.success()
              that.renderData()
            })
            .catch(() => {
              that.listLoading = false
            })
        })
        .catch(() => {})
    },
    notpass() {
      const that = this
      if (that.checkList.length === 0) {
        that.$opsMessage.warn(that.$t('pages.userPhotoWallApproval.message.selectApprovalItem'))
        return
      }
      that
        .$confirm(that.$t('pages.userPhotoWallApproval.message.confirmApproval'), that.$t('pages.userPhotoWallApproval.confirm.title'), {
          type: 'warning'
        })
        .then(() => {
          that.listLoading = true
          approvalPhotoWallNotPass(that.getApprovalParams())
            .then(res => {
              that.listLoading = false
              that.$opsMessage.success()
              that.renderData()
            })
            .catch(() => {
              that.listLoading = false
            })
        })
        .catch(() => {})
    },
    editUserForm(id) {
      this.thatSelectedUserId = id
      this.editUserFormVisible = true
    },
    renderDataSuccess() {
      this.$message({
        message: this.$t('pages.userPhotoWallApproval.message.success'),
        type: 'success'
      })
      this.renderData()
    },
    queryUserDetails(id) {
      this.thatSelectedUserId = id
      this.userDeatilsDrawer = true
    },
    handleClickCard(id) {
      const index = this.checkList.findIndex(nid => nid === id)
      if (index > -1) {
        this.checkList.splice(index, 1)
        return
      }
      this.checkList.push(id)
    },
    approvalStatusName(status) {
      return this.$t(`pages.userPhotoWallApproval.approvalStatus.${status}`)
    },
    genderName(gender, fallback = '') {
      if (gender === 0) {
        return this.$t('pages.userPhotoWallApproval.gender.female')
      }
      if (gender === 1) {
        return this.$t('pages.userPhotoWallApproval.gender.male')
      }
      return fallback
    },
    getApprovalParams() {
      const that = this
      const approvalParams = []
      that.checkList.forEach(item => {
        const checkData = that.list[item]
        approvalParams.push({
          userId: checkData.userId,
          contentId: checkData.id,
          content: checkData.resourceUrl,
          tags: checkData.labelNames
        })
      })
      return approvalParams
    }
  }
}
</script>
<style scoped lang="scss">
.bottom {
  color: #666;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 25px;
  .line {
    display: flex;
    > div {
      width: 100%;
    }
  }
  .info {
    cursor: pointer;
  }
}
</style>
