<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true" size="small">
        <el-form-item label="业务场景">
          <el-select
            v-model="searchData.BusinessScenarioEnum"
            style="width:150px;"
            disabled
          >
            <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="文件名称">
          <el-input :maxLength="$inputMax"
            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-form>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-left:auto;"
        size="small"
        @click="handleAdd"
      >
        {{ $t('common:button:new') }}
      </el-button>
    </template>
    <template slot="main-container">
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        height="600"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" width="40" />
        <el-table-column
          prop="Code"
          label="Code"
          show-overflow-tooltip
          sortable="custom"
        />

        <el-table-column
          prop="BusinessScenarioEnum"
          label="业务场景"
          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="文件名称"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          prop="CriterionTypeEnum"
          label="阅片标准"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd('CriterionType',scope.row.CriterionTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="Description"
          label="描述"
          show-overflow-tooltip
        />
        <el-table-column
          prop="IsDeleted"
          label="是否废除"
          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="更新时间"
          show-overflow-tooltip
          sortable="custom"
        />

        <el-table-column label="Action">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleDownload(scope.row)"
            >
              下载
            </el-button>
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
            >
              编辑
            </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"
        append-to-body
      >
        <AttachmentForm :data="currentRow" :is-distinguish-criteria="isDistinguishCriteria" @closeDialog="closeDialog" @getList="getList" />
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import { getCommonDocumentList, DownloadCommonDoc } from '@/api/dictionary'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import AttachmentForm from './AttachmentForm'
const FileTypeEnum = 3
const searchDataDefault = () => {
  return {
    FileTypeEnum: FileTypeEnum,
    BusinessScenarioEnum: null,
    Name: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'ExportTemplate',
  components: { BaseContainer, Pagination, AttachmentForm },
  props: {
    businessScenarioEnum: {
      type: Number,
      required: true
    },
    isDistinguishCriteria: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      currentRow: {},
      editDialog: { title: '', visible: false },
      loading: false
    }
  },
  mounted() {
    this.searchData.BusinessScenarioEnum = this.businessScenarioEnum
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.searchData.BusinessScenarioEnum = this.businessScenarioEnum
      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 = 'Add'
      this.currentRow = { FileTypeEnum: FileTypeEnum, BusinessScenarioEnum: this.businessScenarioEnum }
      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 = 'Edit'
      this.currentRow = { ...row }
      this.editDialog.visible = true
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.searchData.BusinessScenarioEnum = this.businessScenarioEnum
      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>
