<template>
  <box-content>
    <div class="search">
      <el-form :inline="true" size="small" class="base-search-form">
        <el-form-item label="Site Name:">
          <el-input v-model="searchData.SiteName" style="width:120px;" />
        </el-form-item>
        <el-form-item label="Alias Name:">
          <el-input v-model="searchData.AliasName" style="width:120px;" />
        </el-form-item>
        <el-form-item label="Country:">
          <el-input v-model="searchData.Country" style="width:120px;" />
        </el-form-item>
        <el-form-item label="City:">
          <el-input v-model="searchData.City" style="width:120px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">Search</el-button>
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">Reset</el-button>
        </el-form-item>
      </el-form>
      <el-button
        type="primary"
        size="small"
        style="margin-left:auto;"
        icon="el-icon-plus"
        @click="handleAddSite"
      >New</el-button>
    </div>
    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:60}"
      :data="list"
      stripe
      height="100"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="40" />
      <el-table-column
        prop="SiteCode"
        label="Site Code"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="SiteName"
        label="Site Name"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="AliasName"
        label="Alias Name"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="UniqueCode"
        label="组织机构代码"
        min-width="130"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="Country"
        label="Country"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="City"
        label="City"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="Address"
        label="Address"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="HospitalName"
        label="Affiliated Hospital"
        min-width="180"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="DirectorName"
        label="Director Name"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="DirectorPhone"
        label="Director Phone"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="ContactName"
        label="Contact Name"
        min-width="150"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        prop="ContactPhone"
        label="Contact Phone"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column label="Action" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            circle
            icon="el-icon-edit-outline"
            title="编辑"
            @click="handleEdit(scope.row)"
          />
          <el-button
            circle
            icon="el-icon-delete"
            title="删除"
            @click="handleDelete(scope.row)"
          />
        </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="600px"
      custom-class="base-dialog-wrapper"
    >
      <site-form v-if="editVisible" :data="rowData" @close="close" @getList="getList" />
    </el-dialog>

  </box-content>
</template>
<script>
import { getSiteList, deleteSite } from '@/api/dictionary'

import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import SiteForm from './SiteForm'
const searchDataDefault = () => {
  return {
    SiteName: '',
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: '',
    AliasName: '',
    Country: null,
    City: null
  }
}
export default {
  name: 'Sites',
  components: { BoxContent, Pagination, SiteForm },
  data() {
    return {
      editVisible: false,
      title: 'Add',
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {}
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取Site列表信息
    getList() {
      this.loading = true
      getSiteList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => {
        this.loading = false
      })
    },
    // 新增Site信息
    handleAddSite() {
      this.rowData = {}
      this.title = 'Add'
      this.editVisible = true
    },
    // 编辑Site信息
    handleEdit(row) {
      this.rowData = row
      this.title = 'Edit'
      this.editVisible = true
    },
    // 删除Site
    handleDelete(row) {
      this.$confirm('Sure to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          this.loading = true
          deleteSite(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$store.dispatch('global/setSite', {})
                this.$message.success('Deleted successfully!')
              }
            }).catch(() => {
              this.loading = false
            })
        })
    },
    // 重置列表
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      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()
    },
    // 关闭模态框
    close() {
      this.editVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
.Sites{
  height: 100%;
}
</style>
