<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <!-- 新增 -->
        <el-button
          slot="left"
          v-permission="['admin','sysAppUpdate:add']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-upload"
          @click="crud.toAdd"
        >上传
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.add ? '文件上传' : '编辑文件'" width="500px">
        <el-form ref="form" :model="form" size="small" label-width="80px">

          <el-form-item label="项目名称">
            <el-input v-model="form.appName" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="版本代码">
            <el-input v-model="form.versionCode" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="版本名称">
            <el-input v-model="form.versionName" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="更新内容">
            <el-input v-model="form.modifyContent" style="width: 370px;" type="textarea" />
          </el-form-item>

          <!--   上传文件   -->
          <el-form-item v-if="crud.status.add" label="上传">
            <el-upload
              ref="upload"
              :limit="1"
              :before-upload="beforeUpload"
              :auto-upload="false"
              :headers="headers"
              :on-success="handleSuccess"
              :on-error="handleError"
              :action="appUploadApi + '?versionCode='+form.versionCode+'&appName='+form.appName+'&versionName='+form.versionName+'&modifyContent='+form.modifyContent"
            >
              <div class="eladmin-upload"><i class="el-icon-upload" /> 添加文件</div>
              <div slot="tip" class="el-upload__tip">可上传任意格式文件，且不超过200M</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button v-if="crud.status.add" :loading="loading" type="primary" @click="upload">确认</el-button>
          <el-button v-else :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="appName" label="项目名称" />
        <el-table-column prop="updateStatus" label="更新状态" />
        <el-table-column prop="versionCode" label="版本代码" />
        <el-table-column prop="versionName" label="版本名称" />
        <el-table-column prop="modifyContent" label="修改内容" />
        <el-table-column prop="downloadUrl" label="下载网站">
          <template slot-scope="scope">
            <el-popover
              :content="scope.row.downloadUrl"
              placement="top-start"
              title="路径"
              width="200"
              trigger="hover"
            >
              <a
                slot="reference"
                :href="baseApi + scope.row.downloadUrl "
                class="el-link--primary"
                style="word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color: #1890ff;font-size: 13px;"
                target="_blank"
              >
                {{ scope.row.downloadUrl }}
              </a>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="apkSize" label="程序大小">
          <template slot-scope="scope">
            <span v-if="scope.row.apkSize < 1024">{{ Number(scope.row.apkSize).toFixed(2) }}K</span>
            <span v-if="scope.row.apkSize > 1024">{{ Number(scope.row.apkSize/1024).toFixed(2) }}M</span>
          </template>
        </el-table-column>
        <el-table-column prop="apkMd5" label="APKMD5" />
        <el-table-column v-if="checkPer(['admin','sysAppUpdate:edit','sysAppUpdate:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件2-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSysAppUpdate from '@/api/sysAppUpdate'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
const defaultForm = { id: null, name: '', versionCode: '', versionName: '', modifyContent: '' }
export default {
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'app_update', url: 'api/sysAppUpdate', idField: 'id', sort: 'id,desc', crudMethod: { ...crudSysAppUpdate }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      permission: {
        edit: ['admin', 'sysAppUpdate:edit'],
        del: ['admin', 'sysAppUpdate:del']
      },
      rules: {
        updateStatus: [
          { required: true, message: '更新状态.不能为空', trigger: 'blur' }
        ],
        versionCode: [
          { required: true, message: '版本代码.不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'appUploadApi'
    ])
  },
  created() {
    this.crud.optShow.add = false
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    upload() {
      this.$refs.upload.submit()
    },
    beforeUpload(file) {
      let isLt2M = true
      isLt2M = file.size / 1024 / 1024 < 200
      if (!isLt2M) {
        this.loading = false
        this.$message.error('上传文件大小不能超过 200MB!')
      }
      return isLt2M
    },
    handleSuccess(response, file, fileList) {
      this.crud.notify('上传成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.$refs.upload.clearFiles()
      this.crud.status.add = CRUD.STATUS.NORMAL
      this.crud.resetForm()
      this.crud.toQuery()
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
      this.loading = false
    }
  }
}
</script>

<style scoped>

</style>
