<template>
  <div class="main-page">
    <div class="main-page-header">
      <p class="title">机构管理</p>
      <el-form
        :inline="true"
        :model="searchForm"
        ref="searchForm"
        size="small"
      >
        <el-form-item
          prop="hospitalName"
          label="机构名称"
        >
          <el-input
            placeholder="请输入机构名称"
            v-model="searchForm.hospitalName"
          />
        </el-form-item>
        <el-form-item
          label="所属辖区："
          prop="area"
        >
          <el-select
            v-model="searchForm.area"
            placeholder="请选择辖区"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              v-for="item in areaList"
              :label="item.name"
              :key="item.id"
              :value="item.val"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="unitType"
          label="单位性质"
        >
          <el-select
            v-model="searchForm.unitType"
            placeholder="请选择单位性质"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              label="公立医疗"
              :value="1"
            />
            <el-option
              label="民营医院"
              :value="2"
            />
            <el-option
              label="非医疗机构"
              :value="3"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="specitalType"
          label="特殊单位类型"
        >
          <el-select
            v-model="searchForm.specitalType"
            placeholder="请选择特殊单位类型"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              label="无特殊"
              :value="0"
            />
            <el-option
              label="急救中心"
              :value="1"
            />
            <el-option
              label="非急救转运中心"
              :value="2"
            />
          </el-select>
        </el-form-item>
        <div class="btns">
          <el-button
            type="primary"
            size="small"
            @click="searchFormSub"
          >查询</el-button>
          <el-button
            size="small"
            @click="resetForm('searchForm')"
          >重置</el-button>
        </div>
      </el-form>
    </div>
    <div class="main-page-body">
      <div class="mainContent">
        <div class="bt">
          <el-button
            type="primary"
            size="small"
            @click="showSaveDialog()"
          >新增</el-button>
        </div>
        <table-mixin
          :pageSize="pageSize"
          :pageNum="pageNum"
          :total="total"
          :pagination="pagination"
          :handleSizeChange="handleSizeChange"
          :handleCurrentChange="handleCurrentChange"
        >
          <el-table
            v-loading="tableData.loading"
            :data="tableData.body"
            @sort-change="handleSortChange"
          >
            <div
              slot="empty"
              class="noData"
            ></div>
            <el-table-column
              label="序号"
              type="index"
              min-width="50"
            >
            </el-table-column>
            <el-table-column
              v-for="(item,index) in tableData.head"
              :prop="item.key"
              :label="item.name"
              align="left"
              :formatter="item.formatter"
              :sortable="item.sortable"
              :fixed="item.fixed"
              :key="index"
              :min-width="item.width"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="left"
              width="auto"
              min-width="250px"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="showSaveDialog('detail',scope.row)"
                >查看详情</el-link>
                <el-link
                  type="primary"
                  @click="showSaveDialog('update', scope.row)"
                >修改</el-link>
                <el-link
                  v-if="userInfo.roleId!=3"
                  type="primary"
                  @click="delHandler(scope.row.id)"
                >删除</el-link>
              </template>
            </el-table-column>
          </el-table>
        </table-mixin>
      </div>
    </div>
    <addDialog
      ref="addDialog"
      @subsucc="search"
    />
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { tableDataMixin } from '../../common/js/mixin'
import { getDic } from '../../util/dict'
import { queryPageList, deleteById } from '@/api/mechanism'
import addDialog from './components/addDialog.vue'
export default {
  name: 'expertPage',
  mixins: [tableDataMixin],
  components: {
    addDialog
  },
  data() {
    return {
      searchForm: {
        hospitalName: '',
        area: '',
        unitType: '',
        specitalType: ''
      },
      areaList: [],
      selectLoading: false,
      tableData: {
        loading: false,
        head: [
          {
            name: '单位名称',
            key: 'hospitalName',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '所属辖区',
            key: 'areaStr',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '单位性质',
            key: 'unitTypeStr',
            sortable: false,
            width: '100',
            fixed: false
          },
          {
            name: '机构等级',
            key: 'hospitalGradeStr',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '医院等次',
            key: 'hospitalStepStr',
            sortable: false,
            width: '150',
            fixed: false
          },
          {
            name: '特殊单位类型',
            key: 'specitalTypeStr',
            sortable: false,
            width: '300',
            fixed: false
          }
        ],
        body: [],
        option: [],
        condition: []
      }
    }
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  mounted() {
    // 获取字典
    getDic(['area']).then(res => {
      this.areaList = res.area
    })
    this.search()
  },
  methods: {
    getTableData() {
      this.tableData.loading = true
      const params = {
        ...this.searchForm,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      }
      queryPageList(params).then((res) => {
        if (res.msg == 201) {
          this.dealTableResponse(res)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    delHandler(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: id }).then(res => {
          if (res.msg === 201) {
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.content)
          }
          this.search()
        })
      })
    },
    showSaveDialog(type = 'add', data = '') {
      this.$refs.addDialog.init(type, data)
    }
  }
}
</script>
<style lang="scss" scoped>
.mainContent {
  padding: 20px;
  .bt {
    text-align: right;
    margin-bottom: 16px;
  }
}
</style>
