<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item label="编号:">
          <el-input v-model="searchData.Code" style="width:100px;" />
        </el-form-item>
        <el-form-item label="模板:">
          <el-input v-model="searchData.Name" style="width:100px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">Reset</el-button>
          <el-button type="primary" @click="handleSearch">Search</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto;">
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
        >
          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="Code"
        sortable="custom"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Name"
        label="模板"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="Value"
        label="签名内容(EN)"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ValueCN"
        label="签名内容(CN)"
        show-overflow-tooltip
      />
      <el-table-column
        prop="UpdateTime"
        label="更新时间"
        show-overflow-tooltip
        sortable="custom"
      />

      <el-table-column
        prop="CreateTime"
        label="创建时间"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column label="Action" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            Edit
          </el-button>
          <el-button
            type="text"
            @click="handleConfig(scope.row)"
          >
            场景配置
          </el-button>

          <el-button type="text" @click="handleDelete(scope.row)">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="场景配置"
      :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 = 'Add'
      this.editVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.title = 'Edit'
      this.editVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('Sure to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          deleteSystemBasicData(row.Id)
            .then(res => {
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$message.success('Deleted successfully!')
              }
            })
        })
    },
    // 场景配置
    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>
