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

    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.appVersionManager.filter.system')"
        style="width: 120px"
        class="filter-item filter-item--system"
        @change="handleSearch"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatformAlls"
          :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>
      <el-select
        v-model="listQuery.platform"
        style="width: 120px"
        class="filter-item filter-item--platform"
        :placeholder="$t('pages.appVersionManager.filter.platform')"
        @change="changeSelectPlatform"
      >
        <el-option
          v-for="(item, index) in platformOrigins"
          :key="index"
          :label="item.name"
          :value="item.value"
        />
      </el-select>
      <el-select
        v-if="selectedPlatform && selectedPlatform.channels && selectedPlatform.channels .length > 0"
        v-model="listQuery.channel"
        :placeholder="$t('pages.appVersionManager.filter.channel')"
        style="width: 200px"
        class="filter-item filter-item--channel"
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in selectedPlatform.channels"
          :key="index"
          :label="item.name"
          :value="item.value"
        />
      </el-select>
      <el-button
        :loading="searchLoading"
        class="filter-item filter-item--search"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('pages.appVersionManager.action.search') }}
      </el-button>
      <el-button
        class="filter-item filter-item--create"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleAdd"
      >
        {{ $t('pages.appVersionManager.action.add') }}
      </el-button>

      <el-input
        v-if="buildVersionInputVsiible"
        ref="saveTagInput"
        v-model="buildVersion"
        v-number
        class="filter-item filter-item--build-version input-new-tag"
        @blur="handleInputConfirm"
      />
      <el-button
        v-else
        v-loading="buildVersionLoading"
        class="filter-item filter-item--build-version button-new-tag"
        type="info"
        plain
        @click="showBuildVersionInput"
      > BuildVersion:{{ buildVersion }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.appVersionManager.loading')"
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
    >
      <el-table-column prop="platform" :label="$t('pages.appVersionManager.table.platform')" align="center" min-width="100px">
        <template slot-scope="scope">
          {{ scope.row.platform }}<span v-if="scope.row.channel">/{{ scope.row.channel }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="version" :label="$t('pages.appVersionManager.table.version')" align="center" min-width="100px" />
      <el-table-column prop="buildVersion" :label="$t('pages.appVersionManager.table.buildVersion')" align="center" min-width="80px" />
      <el-table-column :label="$t('pages.appVersionManager.table.forceUpdate')" align="center">
        <template slot-scope="scope">
          {{ getYesNoText(scope.row.forceUpdate) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.appVersionManager.table.review')" align="center">
        <template slot-scope="scope">
          {{ getYesNoText(scope.row.review) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.appVersionManager.table.appType')" align="center">
        <template slot-scope="scope">
          {{ getAppTypeText(scope.row.appType) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.appVersionManager.table.patch')" align="center">
        <template slot-scope="scope">
          {{ getYesNoText(scope.row.patch) }}
        </template>
      </el-table-column>
      <el-table-column prop="updateDescribe" :label="$t('pages.appVersionManager.table.updateDescribe')" align="center">
        <template slot-scope="scope">
          <div @click="copyTextContent(scope.row.updateDescribe)">
            <el-tooltip class="item" effect="dark" placement="top-start">
              <div slot="content" class="tooltip-content"><span v-html="formatUpdateDescribe(scope.row.updateDescribe)" /></div>
              <div class="el-icon-info cursor-pointer" />
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="apkSize" :label="$t('pages.appVersionManager.table.apkSize')" align="center" />
      <el-table-column :label="$t('pages.appVersionManager.table.downloadUrl')" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="copyTextContent(scope.row.downloadUrl)">{{ $t('pages.appVersionManager.action.copy') }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" :label="$t('pages.appVersionManager.table.createTime')" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.appVersionManager.table.actions')" align="center" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="hanldeEidt(scope.row)">{{ $t('pages.appVersionManager.action.edit') }}</el-dropdown-item>
              <el-dropdown-item @click.native="removeAppVersion(scope.row.id)">{{ $t('pages.appVersionManager.action.delete') }}</el-dropdown-item>
              <el-dropdown-item @click.native="clickSetServerBuildVersion(scope.row)">{{ $t('pages.appVersionManager.action.serviceForceUpdate') }}</el-dropdown-item>
              <el-dropdown-item @click.native="handleUpdateDescribeList(scope.row)">{{ $t('pages.appVersionManager.action.updateDescription') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
    <app-version-edit
      v-if="appVersionEditVisible"
      :update-data="updateData"
      :sys-origin="listQuery.sysOrigin"
      @close="appVersionEditVisible = false"
      @success="appVersionEditSuccess"
    />

    <app-update-content
      v-if="updateDescribeVisible"
      :version-id="thatSelectedVersionId"
      @close="updateDescribeVisible=false"
    />

  </div>
</template>

<script>
import { getAppVersionTable, delAppVersion, addServerBuildVersion, getServerBuildVersion } from '@/api/app-manager'
import Pagination from '@/components/Pagination'
import { copyText } from '@/utils'
import AppVersionEdit from '@/views/version/app/app-version-edit'
import { mapGetters } from 'vuex'
import AppUpdateContent from '@/views/version/app/app-update-content.vue'
import { platformOrigins } from '@/constant/type'

export default {
  name: 'AppUserTable',
  components: {
    Pagination,
    AppVersionEdit,
    AppUpdateContent
  },
  data() {
    return {
      platformOrigins,
      selectedPlatform: {},
      appVersionEditVisible: false,
      updateDescribeVisible: false,
      updateData: null,
      thisRow: {},
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        platform: 'iOS',
        sysOrigin: '',
        channel: ''
      },
      rangeDate: '',
      thatSelectedVersionId: '',
      listLoading: false,
      searchLoading: false,
      clickUserId: '',
      buildPlatform: 'iOS',
      buildVersion: 0,
      buildVersionInputVsiible: false,
      buildVersionLoading: false
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatformAlls', 'permissionsFirstSysOrigin'])
  },
  created() {
    if (!this.permissionsFirstSysOrigin) {
      return
    }
    this.listQuery.sysOrigin = this.permissionsFirstSysOrigin.value
    this.changeSelectPlatform(this.listQuery.platform)
    this.getBuildVersion()
  },
  methods: {
    renderData(isReset) {
      const that = this
      if (isReset === true) {
        that.listQuery.cursor = 1
      }
      if (that.listLoading === true) {
        return
      }
      that.listLoading = true
      getAppVersionTable(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.searchLoading = that.listLoading = false
      }).catch(er => {
        that.searchLoading = that.listLoading = false
      })
    },
    changeSelectPlatform(val) {
      this.selectedPlatform = {}
      this.listQuery.channel = ''
      this.selectedPlatform = this.platformOrigins.filter(item => item.value === val)[0] || {}
      if (this.selectedPlatform.channels && this.selectedPlatform.channels.length > 0) {
        this.listQuery.channel = this.selectedPlatform.channels[0].value
      }
      this.handleSearch()
    },
    getBuildVersion() {
      const that = this
      that.buildVersion = 0
      that.buildVersionLoading = true
      getServerBuildVersion({ sysOrigin: that.listQuery.sysOrigin, platform: that.listQuery.platform, channel: that.listQuery.channel }).then(res => {
        that.buildVersionLoading = false
        that.buildVersion = res.body || 0
      }).catch(er => {
        that.buildVersionLoading = false
      })
    },
    handleSearch() {
      this.searchLoading = true
      this.renderData(true)
      this.getBuildVersion()
      this.buildPlatform = this.listQuery.platform
    },
    handleUpdateDescribeList(row) {
      this.thatSelectedVersionId = String(row.id)
      this.updateDescribeVisible = true
    },
    copyTextContent(text) {
      const that = this
      copyText(text).then(() => {
        that.$message({
          message: that.$t('pages.appVersionManager.message.copySuccess'),
          type: 'success'
        })
      }).catch(() => {
        that.$message({
          message: that.$t('pages.appVersionManager.message.copyFail'),
          type: 'error'
        })
      })
    },
    handleAdd() {
      this.appVersionEditVisible = true
      this.updateData = null
    },
    hanldeEidt(row) {
      this.appVersionEditVisible = true
      this.updateData = row
    },
    handleMouseEnter(row) {
      this.thisRow = row
      this.thatSelectedUserId = row.id
    },
    appVersionEditSuccess(event) {
      this.updateData = null
      this.appVersionEditVisible = false
      this.renderData(event === 'create')
    },
    getYesNoText(value) {
      return value === true
        ? this.$t('pages.appVersionManager.word.yes')
        : this.$t('pages.appVersionManager.word.no')
    },
    getAppTypeText(value) {
      return value === 'CHAT'
        ? this.$t('pages.appVersionManager.appType.chat')
        : this.$t('pages.appVersionManager.appType.worship')
    },
    removeAppVersion(id) {
      const that = this
      this.$confirm(
        this.$t('pages.appVersionManager.confirm.deleteVersion'),
        this.$t('pages.appVersionManager.confirm.title'),
        {
          confirmButtonText: this.$t('pages.appVersionManager.action.confirm'),
          cancelButtonText: this.$t('pages.appVersionManager.action.cancel')
        }
      ).then(() => {
        delAppVersion(id).then(res => {
          that.renderData()
        })
      }).catch(() => {})
    },
    handleInputConfirm() {
      this.setServerBuildVersion({ sysOrigin: this.listQuery.sysOrigin,
        platform: this.listQuery.platform,
        channel: this.listQuery.channel,
        buildVersion: this.buildVersion
      })
    },
    showBuildVersionInput() {
      this.buildVersionInputVsiible = true
    },
    clickSetServerBuildVersion(row) {
      this.setServerBuildVersion({ sysOrigin: row.sysOrigin,
        platform: row.platform,
        channel: row.channel,
        buildVersion: row.buildVersion
      })
    },
    setServerBuildVersion(params) {
      const that = this
      that.buildVersionInputVsiible = false
      this.$confirm(
        this.$t('pages.appVersionManager.confirm.setServerBuildVersion'),
        this.$t('pages.appVersionManager.confirm.title'),
        {
          confirmButtonText: this.$t('pages.appVersionManager.action.confirm'),
          cancelButtonText: this.$t('pages.appVersionManager.action.cancel')
        }
      ).then(() => {
        addServerBuildVersion(params).then(res => {
          that.buildVersion = params.buildVersion
          that.$opsMessage.success()
        })
      }).catch(() => {})
    },
    formatUpdateDescribe(text) {
      text = text || ''
      return text.replace(/\\n/g, '<br/>')
    }
  }
}
</script>
<style scoped lang="scss">
.this-level {
  padding-bottom: 30px;
  display: flex;
  > div {
    width: 150px;
  }
}

.filter-item--system {
  order: 10;
}

.filter-item--platform {
  order: 20;
}

.filter-item--channel {
  order: 30;
}

.filter-item--build-version {
  order: 40;
}

.filter-item--search {
  order: 50;
}

.filter-item--create {
  order: 60;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
