<template>
  <div class="app-container">

    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.propsStore.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in permissionsSysOriginPlatforms"
          :key="index"
          :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>
      <el-select
        v-model="listQuery.propsType"
        :placeholder="$t('pages.propsStore.filter.propsType')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in propsCommodityTypeOptions"
          :key="index"
          :label="item.name"
          :value="item.value"
        />
      </el-select>
      <el-select
        v-model="listQuery.shelfStatus"
        :placeholder="$t('pages.propsStore.filter.shelfStatus')"
        clearable
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option v-for="item in showcaseTypeOptions" :key="item.value" :label="item.name" :value="item.value" />
      </el-select>

      <el-select
        v-model="listQuery.currencyTypes"
        :placeholder="$t('pages.propsStore.filter.currencyType')"
        style="width: 120px"
        class="filter-item"
        clearable
        @change="handleSearch"
      >
        <el-option v-for="item in propsCurrencyTypeOptions" :key="item.value" :label="item.name" :value="item.value" />
      </el-select>

      <el-input
        v-model.trim="listQuery.id"
        v-number
        :placeholder="$t('pages.propsStore.filter.commodityId')"
        clearable
        style="width: 200px;"
        class="filter-item"
      />

      <el-input
        v-model.trim="listQuery.sourceId"
        v-number
        :placeholder="$t('pages.propsStore.filter.resourceId')"
        clearable
        style="width: 200px;"
        class="filter-item"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('pages.propsStore.action.search') }}
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        {{ $t('pages.propsStore.action.add') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <h2>{{ $t('pages.propsStore.section.resourceInfo') }}</h2>
          <el-form v-if="scope.row.propsSource" label-position="left" class="store-table-expand" style="margin-left:20px;">
            <el-form-item>
              <el-col :span="12">
                <el-form-item label="ID">
                  <span>{{ scope.row.propsSource.id }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.code')">
                  <span>{{ scope.row.propsSource.code }}</span>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item>
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.name')">
                  <span>{{ scope.row.propsSource.name }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.basePrice')">
                  <span>{{ scope.row.propsSource.amount }}</span>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item v-if="scope.row.commodity.propsType !== 'NOBLE_VIP'">
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.shelfStatus')">
                  <span>{{ getShelfStatusLabel(!scope.row.propsSource.del) }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.salePrice')">
                  <el-tag>{{ scope.row.salePrice }}</el-tag>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item v-if="scope.row.commodity.propsType === 'NOBLE_VIP'">
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.shelfStatus')">
                  <span>{{ getShelfStatusLabel(!scope.row.propsSource.del) }}</span>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item v-if="scope.row.commodity.propsType === 'NOBLE_VIP'">
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.originalPrice')">
                  <el-tag>{{ scope.row.salePrice }}</el-tag>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :label="$t('pages.propsStore.detail.discountedPrice')">
                  <el-tag>{{ scope.row.saleDiscountPrice }}</el-tag>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-form>
          <div v-if="scope.row.commodity.propsType === 'NOBLE_VIP'">
            <h2>{{ $t('pages.propsStore.section.nobleAbility') }}</h2>
            <el-form v-if="scope.row.propsAbility && scope.row.propsAbility.nobleVipAbility" label-position="left" class="store-table-expand" style="margin-left:20px;">
              <el-form-item>
                <el-col :span="12">
                  <el-form-item label="ID">
                    <span>{{ scope.row.propsAbility.nobleVipAbility.id }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('pages.propsStore.form.vipType')">
                    <span>{{ scope.row.propsAbility.nobleVipAbility.vipType }}</span>
                  </el-form-item>
                </el-col>
              </el-form-item>
              <el-form-item>
                <el-col :span="12">
                  <el-form-item :label="$t('pages.propsStore.form.vipLevel')">
                    <span>{{ scope.row.propsAbility.nobleVipAbility.vipLevel }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('pages.propsStore.detail.memberCount')">
                    <span>{{ scope.row.propsAbility.nobleVipAbility.roomMaxMember }}</span>
                  </el-form-item>
                </el-col>
              </el-form-item>
              <el-form-item>
                <el-col :span="12">
                  <el-form-item :label="$t('pages.propsStore.detail.loginRewardCount')">
                    <span>{{ scope.row.propsAbility.nobleVipAbility.loginRewardsAmount }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('pages.propsStore.detail.props')">

                    <div class="vip-props flex-l">

                      <div v-if="scope.row.propsAbility.avatarFrame" class="props">
                        <div class="preview-img">
                          <el-image
                            style="width: 100%; height: 100%"
                            :src="scope.row.propsAbility.avatarFrame.cover"
                            :preview-src-list="[scope.row.propsAbility.avatarFrame.cover]"
                          />
                          <div class="preview-svga">
                            <svgaplayer
                              type="popover"
                              :url="scope.row.propsAbility.avatarFrame.sourceUrl"
                            />
                          </div>
                          <div class="props-lable">{{ $t('pages.propsStore.propsTypes.AVATAR_FRAME') }}</div>
                        </div>
                      </div>

                      <div v-if="scope.row.propsAbility.car" class="props">
                        <div class="preview-img">
                          <el-image
                            style="width: 100%; height: 100%"
                            :src="scope.row.propsAbility.car.cover"
                            :preview-src-list="[scope.row.propsAbility.car.cover]"
                          />
                          <div class="preview-svga">
                            <svgaplayer
                              type="popover"
                              :url="scope.row.propsAbility.car.sourceUrl"
                            />
                          </div>
                          <div class="props-lable">{{ $t('pages.propsStore.propsTypes.RIDE') }}</div>
                        </div>
                      </div>

                      <div v-if="scope.row.propsAbility.chatBubble" class="props">
                        <div class="preview-img">
                          <el-image
                            style="width: 100%; height: 100%"
                            :src="scope.row.propsAbility.chatBubble.cover"
                            :preview-src-list="[scope.row.propsAbility.chatBubble.cover]"
                          />
                          <div class="preview-svga">
                            <svgaplayer
                              type="popover"
                              :url="scope.row.propsAbility.chatBubble.sourceUrl"
                            />
                          </div>
                          <div class="props-lable">{{ $t('pages.propsStore.propsTypes.CHAT_BUBBLE') }}</div>
                        </div>
                      </div>

                      <div v-if="scope.row.propsAbility.dataCard" class="props">
                        <div class="preview-img">
                          <el-image
                            style="width: 100%; height: 100%"
                            :src="scope.row.propsAbility.dataCard.cover"
                            :preview-src-list="[scope.row.propsAbility.dataCard.cover]"
                          />
                          <div class="preview-svga">
                            <svgaplayer
                              type="popover"
                              :url="scope.row.propsAbility.dataCard.sourceUrl"
                            />
                          </div>
                          <div class="props-lable">{{ $t('pages.propsStore.propsTypes.DATA_CARD') }}</div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </el-form>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="commodity.id" width="200" label="ID" align="center" />
      <el-table-column :label="$t('pages.propsStore.table.platform')" width="80" align="center">
        <template slot-scope="scope">
          <sys-origin-icon :icon="scope.row.commodity.sysOrigin" :desc="scope.row.commodity.sysOrigin" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.propsStore.table.currencyType')" align="center" width="100">
        <template slot-scope="scope">
          {{ getCurrencyTypesLabel(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column prop="commodity.validDays" :label="$t('pages.propsStore.table.validDays')" align="center" min-width="100" />
      <el-table-column :label="$t('pages.propsStore.table.cover')" width="80" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.propsSource" class="preview-img">
            <el-image
              style="width: 100%; height: 100%"
              :src="scope.row.propsSource.cover"
              :preview-src-list="[scope.row.propsSource.cover]"
            />
            <div class="preview-svga">
              <svgaplayer
                type="popover"
                :url="scope.row.propsSource.sourceUrl"
              />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="100" :label="$t('pages.propsStore.table.shelfStatus')" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.commodity.shelfStatus"
            :active-value="true"
            :inactive-value="false"
            @change="handleSwitchChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="commodity.label" width="100" :label="$t('pages.propsStore.table.label')" align="center" min-width="100" />
      <el-table-column prop="commodity.sort" width="70" :label="$t('pages.propsStore.table.sort')" align="center" min-width="70" />
      <el-table-column prop="commodity.createTime" width="200" :label="$t('pages.propsStore.table.createTime')" align="center">
        <template slot-scope="scope">
          {{ scope.row.commodity.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column prop="commodity.updateTime" width="200" :label="$t('pages.propsStore.table.updateTime')" align="center">
        <template slot-scope="scope">
          {{ scope.row.commodity.updateTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.propsStore.table.actions')" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click.native="handleUpdate(scope.row)">{{ $t('pages.propsStore.action.edit') }}</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"
    />

    <form-edit
      v-if="formEditVisable"
      :row="thisRow"
      @close="formEditVisable=false"
      @success="formEditSuccess"
    />
  </div>
</template>

<script>
import { pagePropsStore, addOrUpdatePropsStore } from '@/api/props'
import Pagination from '@/components/Pagination'
import { propsCurrencyType, propsCommodityTypes } from '@/constant/type'
import FormEdit from './form-edit'
import { mapGetters } from 'vuex'

export default {
  components: { Pagination, FormEdit },
  data() {
    return {
      propsCommodityTypes,
      showcaseTypes: [
        { value: false },
        { value: true }
      ],
      formEditVisable: false,
      thisRow: {},
      thatSelectedUserId: {},
      propsCurrencyType,
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        id: '',
        sourceId: '',
        sysOrigin: 'HALAR',
        propsType: 'AVATAR_FRAME',
        shelfStatus: true,
        currencyTypes: ''
      },
      listLoading: true
    }
  },
  computed: {
    propsCommodityTypeOptions() {
      return this.propsCommodityTypes.map(item => ({
        ...item,
        name: this.$t(`pages.propsStore.propsTypes.${item.value}`)
      }))
    },
    propsCurrencyTypeOptions() {
      return this.propsCurrencyType.map(item => ({
        ...item,
        name: this.$t(`pages.propsStore.currencyTypes.${item.value}`)
      }))
    },
    showcaseTypeOptions() {
      return this.showcaseTypes.map(item => ({
        ...item,
        name: this.getShelfStatusLabel(item.value)
      }))
    },
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  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
      that.listLoading = true
      if (isClean === true) {
        that.listQuery.cursor = 1
      }
      pagePropsStore(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      }).catch(er => {
        that.listLoading = false
      })
    },
    handleCreate() {
      this.thisRow = {
        commodity: {
          shelfStatus: false,
          sysOrigin: this.listQuery.sysOrigin,
          propsType: this.listQuery.propsType
        }
      }
      this.formEditVisable = true
    },
    handleUpdate(row) {
      this.formEditVisable = true
    },
    handleSwitchChange(row) {
      addOrUpdatePropsStore(row.commodity)
        .then(res => {})
        .catch(er => {
          row.shelfStatus = !row.shelfStatus
        })
    },
    getShelfStatusLabel(value) {
      return this.$t(value ? 'pages.propsStore.status.onShelf' : 'pages.propsStore.status.offShelf')
    },
    getCurrencyTypesLabel(row) {
      const currencyTypes = row && row.commodity && row.commodity.currencyTypes
      if (!currencyTypes) {
        return row.currencyTypesName
      }
      return currencyTypes.split(',').map(type => this.$t(`pages.propsStore.currencyTypes.${type}`)).join(',')
    },
    handleSearch() {
      this.renderData(true)
    },
    queryUserDetails(row) {
      this.userDeatilsDrawer = true
      this.thatSelectedUserId = row.id
    },
    handleMouseEnter(row) {
      this.thisRow = row
      this.thatSelectedUserId = row.id
    },
    formEditSuccess() {
      this.formEditVisable = false
      this.renderData(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%;
    }
   }
   .preview-img {
    .props-lable {
      position: absolute;
      bottom: 0px;
      width: 100%;
      color: #FFFFFF;
      padding: 2px 5px;
      line-height: 12px;
      border-radius: 5px;
      font-size: 12px;
      background-color: rgba(0, 0, 0, 0.2);
    }
   }
 }
</style>
