<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
          label="站点名称"
          prop="siteName"
        >
          <!-- <el-input
            placeholder="请输入站点名称"
            v-model="searchForm.siteName"
          /> -->
          <el-select
            v-model="searchForm.siteName"
            placeholder="请选择站点名称"
            style="width: 100%"
            filterable
            allow-create
            remote
            :loading="selectLoading2"
            :remote-method="remoteMethod2"
            :clearable="true"
          >
            <el-option
              v-for="item in siteList"
              :label="item.siteName"
              :key="item.id"
              :value="item.siteName"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="area"
          label="辖区"
        >
          <el-select
            v-model="searchForm.area"
            placeholder="请选择辖区"
            style="width: 100%"
            :disabled="isRoleDisabled"
            :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
          label="挂靠单位"
          prop="hospitalCode"
          v-if="checkPermission()"
        >
          <el-select
            v-model="searchForm.hospitalCode"
            placeholder="请选择挂靠单位"
            style="width: 100%"
            filterable
            remote
            :loading="selectLoading"
            :remote-method="remoteMethod"
            :clearable="true"
          >
            <el-option
              v-for="item in hospitalList"
              :label="item.hospitalName"
              :key="item.id"
              :value="item.hospitalCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="siteStatus"
          label="启用状态"
        >
          <el-radio-group v-model="searchForm.siteStatus">
            <el-radio label="">全部</el-radio>
            <el-radio label="1">启用</el-radio>
            <el-radio label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          prop="auditStatus"
          label="审核状态"
        >
          <el-select
            v-model="searchForm.auditStatus"
            placeholder="请选择站点"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              v-for="item in auditStatusList"
              :label="item.name"
              :key="item.id"
              :value="item.val"
            />
          </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"
            v-if="checkPermission()"
            @click="exportFile()"
          >导出</el-button>
          <el-button
            type="primary"
            size="small"
            v-if="!checkPermission()"
            @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="200px"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="showSaveDialog('detail', scope.row.id)"
                >查看详情</el-link>
                <el-link
                  type="primary"
                  @click="showSaveDialog('update', scope.row.id)"
                >修改</el-link>
                <el-link
                  type="primary"
                  @click="delHandler(scope.row.id)"
                  v-if="!checkPermission() && scope.row.auditStatus != 4"
                >删除</el-link>
                <el-link
                  type="primary"
                  v-if="(scope.row.auditStatus === 1 || scope.row.auditStatus === 5) && checkPermission()"
                  @click="auditHandler(scope.row.id)"
                >审核</el-link>
              </template>
            </el-table-column>
          </el-table>
        </table-mixin>
        <SiteDialog
          ref="siteDialog"
          @subsucc="search"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { tableDataMixin } from '../../common/js/mixin'
import { roleMixin } from '../../common/js/roleMixin'
import { queryEmergencySitePageList, deleteEmergencySite, auditEmergencySite, querySiteListByName } from '@/api/site'
import { getDic } from '../../util/dict'
import { queryHospitalList, exportEmergencyResources } from '@/api/common'
import SiteDialog from './components/siteDialog.vue'
export default {
  name: 'sitePage',
  mixins: [tableDataMixin, roleMixin],
  components: {
    SiteDialog
  },
  data() {
    return {
      searchForm: {
        area: '',
        siteStatus: ''
      },
      areaList: [],
      hospitalList: [],
      auditStatusList: [],
      hospitalList: [],
      siteList: [],
      selectLoading2: false,
      selectLoading: false,
      tableData: {
        loading: false,
        head: [
          {
            name: '站点名称',
            key: 'siteName',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '所在辖区',
            key: 'areaName',
            sortable: false,
            width: '120',
            fixed: false
          },
          {
            name: '站点位置',
            key: 'siteLocation',
            sortable: false,
            width: '120',
            fixed: false
          },
          {
            name: '挂靠单位',
            key: 'hospitalName',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '启用状态',
            key: 'siteStatus',
            sortable: false,
            width: '80',
            fixed: false,
            formatter: this.siteStatusFormatter
          },
          {
            name: '审核状态',
            key: 'auditStatusName',
            sortable: false,
            width: '80',
            fixed: false
          }
        ],
        body: [],
        option: [],
        condition: []
      }
    }
  },
  mounted() {
    // 获取字典
    getDic(['area', 'audit_status']).then(res => {
      this.areaList = res.area
      this.auditStatusList = res.audit_status
    })
    //获取可选择的医院
    queryHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    this.remoteMethod2()
    this.search()
  },
  methods: {
    getTableData() {
      this.tableData.loading = true
      const params = {
        ...this.searchForm,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      }
      queryEmergencySitePageList(params).then((res) => {
        if (res.msg == 201) {
          this.dealTableResponse(res)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryHospitalList(query).then(res => {
        this.hospitalList = res.respData
        this.selectLoading = false;
      })
    },
    remoteMethod2(query) {
      this.selectLoading2 = true;
      querySiteListByName(query).then(res => {
        this.siteList = res.respData
        this.selectLoading2 = false;
      })
    },
    showSaveDialog(type = 'add', id = '') {
      this.$refs.siteDialog.init(type, id)
    },
    auditHandler(id) {
      this.$confirm('是否审批通过？', '提示', {
        confirmButtonText: '通过',
        cancelButtonText: '拒绝',
        distinguishCancelAndClose: true,
        type: 'warning'
      }).then(() => {
        this.auditData(id, 2)
      }).catch(action => {
        if (action === 'cancel') {
          this.auditData(id, 3)
        }
      })
    },
    auditData(id, auditStatus) {
      auditEmergencySite({ id: id, auditStatus: auditStatus }).then(res => {
        if (res.msg === 201) {
          this.$message.success('操作成功！')
        } else {
          this.$message.error(res.content)
        }
        this.search()
      })
    },
    siteStatusFormatter(row, column, cellValue, index) {
      const siteStatusList = ['', '启用', '停用']
      return siteStatusList[cellValue] || ''
    },
    delHandler(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEmergencySite({ id: id }).then(res => {
          if (res.msg === 201) {
            this.$message.success('进入删除审核中！')
          } else {
            this.$message.error(res.content)
          }
          this.search()
        })
      })
    },
    exportFile() {
      const params = {
        ...this.searchForm,
        exportType: 2
      }
      exportEmergencyResources(params).then(res => {
        if (res.msg === 201) {
          window.open(res.respData)
        } else {
          this.$message.error(res.content)
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.mainContent {
  padding: 20px;
  .bt {
    text-align: right;
    margin-bottom: 16px;
  }
}
</style>
