<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true" size="small">
        <!-- 业务场景 -->
        <el-form-item :label="$t('dictionary:attachment:label:businessScenario')">
          <el-select
            v-model="searchData.BusinessScenarioEnum"
            style="width: 150px"
          >
            <el-option
              v-for="item of $d.Common_File_BusinessScenario"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- 文件名称 -->
        <el-form-item :label="$t('dictionary:attachment:label:name')">
          <el-input v-model="searchData.Name" style="width: 130px" clearable />
        </el-form-item>

        <el-form-item>
          <!-- 查询 -->
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button
            type="primary"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
        <el-button
          type="primary"
          style="float: right"
          size="small"
          @click="handleAdd"
        >
          {{ $t('common:button:new') }}
        </el-button>
      </el-form>
    </template>
    <template slot="main-container">
      <el-table
        v-adaptive="{ bottomOffset: 60 }"
        v-loading="loading"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" width="40" />
        <el-table-column
          prop="Code"
          :label="$t('dictionary:attachment:label:code')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 业务场景 -->
        <el-table-column
          prop="BusinessScenarioEnum"
          :label="$t('dictionary:attachment:label:businessScenario')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{
              $fd(
                'Common_File_BusinessScenario',
                scope.row.BusinessScenarioEnum
              )
            }}
          </template>
        </el-table-column>
        <!-- 文件名称 -->
        <el-table-column
          prop="Name"
          :label="$t('dictionary:attachment:label:name')"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          prop="NameCN"
          :label="$t('dictionary:attachment:upload:table:nameCN')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 描述 -->
        <el-table-column
          prop="Description"
          :label="$t('dictionary:attachment:label:description')"
          show-overflow-tooltip
        />
        <!-- 是否废除 -->
        <el-table-column
          prop="IsDeleted"
          :label="$t('dictionary:attachment:label:isDeleted')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsDeleted" type="danger">{{
              $fd('YesOrNo', scope.row.IsDeleted)
            }}</el-tag>
            <el-tag v-else type="primary">{{
              $fd('YesOrNo', scope.row.IsDeleted)
            }}</el-tag>
          </template>
        </el-table-column>
        <!-- 上传时间 -->
        <el-table-column
          prop="UpdateTime"
          :label="$t('dictionary:attachment:label:updateTime')"
          show-overflow-tooltip
          sortable="custom"
        />

        <el-table-column :label="$t('common:action:action')" width="300">
          <template slot-scope="scope">
            <el-button type="text" @click="PreviewFile(scope.row)">
              {{ $t('common:button:preview') }}
            </el-button>
            <el-button type="text" @click="handleDownload(scope.row)">
              {{ $t('common:button:download') }}
            </el-button>
            <el-button type="text" @click="handleEdit(scope.row)">
              {{ $t('common:button:edit') }}
            </el-button>
            <el-button type="text" @click="handleDelete(scope.row)">
              {{ $t('common:button:delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination
        class="page"
        :total="total"
        :page.sync="searchData.PageIndex"
        :limit.sync="searchData.PageSize"
        @pagination="getList"
      />

      <!-- 新增/编辑 -->
      <el-dialog
        v-if="editDialog.visible"
        :visible.sync="editDialog.visible"
        :close-on-click-modal="false"
        :title="editDialog.title"
        width="600px"
        custom-class="base-dialog-wrapper"
      >
        <TemplateForm
          :data="currentRow"
          @closeDialog="closeDialog"
          @getList="getList"
        />
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import {
  getCommonDocumentList,
  DownloadCommonDoc,
  deleteCommonDocument,
} from '@/api/dictionary'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import TemplateForm from './TemplateForm'
const FileTypeEnum = 1
const searchDataDefault = () => {
  return {
    FileTypeEnum: FileTypeEnum,
    BusinessScenarioEnum: null,
    Name: '',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  name: 'UploadTemplate',
  components: { BaseContainer, Pagination, TemplateForm },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      currentRow: {},
      editDialog: { title: '', visible: false },
      loading: false,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleDelete(row) {
      // 确定删除该模版？
      this.$confirm(this.$t('dictionary:attachment:message:msg1')).then(() => {
        deleteCommonDocument(row.Id).then(() => {
          this.$message.success(this.$t('common:message:deletedSuccessfully'))
          this.getList()
        })
      }).catch(()=>{})
    },
    PreviewFile(row) {
      let basePath = window.location.origin
      if (window.location.protocol !== 'https:') {
        basePath = 'https://irc.test.extimaging.com'
      }
      let data = {
        name: row.NameCN,
        path: basePath + row.Path,
      }
      this.$emit('PreviewFile', data)
    },
    getList() {
      this.loading = true
      getCommonDocumentList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 新增
    handleAdd() {
      this.editDialog.title = this.$t('common:button:new')
      this.currentRow = { FileTypeEnum: FileTypeEnum }
      this.editDialog.visible = true
    },
    // 下载
    handleDownload(row) {
      this.loading = true
      DownloadCommonDoc(row.Code)
        .then((data) => {
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 编辑
    handleEdit(row) {
      this.editDialog.title = this.$t('common:button:edit')
      this.currentRow = { ...row }
      this.editDialog.visible = true
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    closeDialog() {
      this.editDialog.visible = false
    },
    // 排序
    handleSortChange(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep .search {
  display: block;
}
</style>
