<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <!-- 编号 -->
        <el-form-item :label="$t('dictionary:sign:label:code')">
          <el-input v-model="searchData.Code" style="width:100px;" />
        </el-form-item>
        <!-- 模板 -->
        <el-form-item :label="$t('dictionary:sign:label:name')">
          <el-input v-model="searchData.Name" style="width:100px;" />
        </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>
      <span style="margin-left:auto;">
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
        >
          {{ $t('common:button:new') }}
        </el-button>
      </span>

    </div>

    <!-- 受试者列表 -->
    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:45}"
      :data="list"
      stripe
      height="100"
      style="width:100%"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="40" />
      <!-- 编号 -->
      <el-table-column
        prop="Code"
        :label="$t('dictionary:sign:label:code')"
        sortable="custom"
        show-overflow-tooltip
      />
      <!-- 模板 -->
      <el-table-column
        prop="Name"
        :label="$t('dictionary:sign:label:name')"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- 签名内容(EN) -->
      <el-table-column
        prop="Value"
        :label="$t('dictionary:sign:label:value')"
        show-overflow-tooltip
      />
      <!-- 签名内容(CN) -->
      <el-table-column
        prop="ValueCN"
        :label="$t('dictionary:sign:label:valueCN')"
        show-overflow-tooltip
      />
      <!-- 更新时间 -->
      <el-table-column
        prop="UpdateTime"
        :label="$t('dictionary:sign:label:updateTime')"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- 创建时间 -->
      <el-table-column
        prop="CreateTime"
        :label="$t('dictionary:sign:label:createTime')"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column :label="$t('common:action:action')" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>
          <!-- 场景配置 -->
          <el-button
            type="text"
            @click="handleConfig(scope.row)"
          >
            {{ $t('dictionary:sign:button:config') }}
          </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="editVisible"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      :title="title"
      width="700px"
      custom-class="base-dialog-wrapper"
    >
      <SignTemplateForm :data="rowData" @closeDialog="closeDialog" @getList="getList" />
    </el-dialog>

    <!-- 场景配置 -->
    <el-dialog
      v-if="configVisible"
      :visible.sync="configVisible"
      :title="$t('dictionary:sign:button:config')"
      :fullscreen="true"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <SceneList :parent-id="rowData.Id" @closeDialog="closeDialog" />
    </el-dialog>
  </box-content>
</template>
<script>
import { getSystemBasicDataList, deleteSystemBasicData } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import SignTemplateForm from './components/SignTemplateForm'
import SceneList from './components/SceneList'
import Pagination from '@/components/Pagination'
const searchDataDefault = () => {
  return {
    Code: '',
    Name: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'SignTemplate',
  components: { BoxContent, SignTemplateForm, Pagination, SceneList },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      title: '',
      editVisible: false,
      configVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取受试者列表
    getList() {
      this.loading = true
      getSystemBasicDataList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    // 新增
    handleAdd() {
      this.rowData = {}
      this.title = this.$t('common:button:new')
      this.editVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.title = this.$t('common:button:edit')
      this.editVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('trials:uploadedDicoms:message:deleteMes'), {
        type: 'warning',
        distinguishCancelAndClose: true,

      })
        .then(() => {
          deleteSystemBasicData(row.Id)
            .then(res => {
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            })
        })
    },
    // 场景配置
    handleConfig(row) {
      this.rowData = { ...row }
      this.configVisible = true
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(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()
    },
    // 关闭新增、编辑框
    closeDialog() {
      this.editVisible = false
    }
  }
}
</script>
