<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="vehicleUsingUnit"
        >
          <!-- <el-input
            placeholder="请输入车辆使用单位"
            v-model="searchForm.vehicleUsingUnit"
          /> -->
          <el-select
            v-model="searchForm.vehicleUsingUnit"
            placeholder="请选择车辆使用单位"
            style="width: 100%"
            filterable
            allow-create
            remote
            :loading="selectLoading"
            :remote-method="remoteMethod"
            :clearable="true"
          >
            <el-option
              v-for="item in hospitalList"
              :label="item.hospitalName"
              :key="item.id"
              :value="item.hospitalName"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="辖区："
          prop="area"
        >
          <el-select
            v-model="searchForm.area"
            placeholder="请选择辖区"
            style="width: 100%"
            :clearable="true"
            :disabled="isRoleDisabled"
          >
            <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="unitType"
          >
            <el-radio-group
              class="radio-group-type4"
              v-model="searchForm.unitType"
            >
              <el-radio :label="1">公立医疗</el-radio>
              <el-radio :label="2">民营医院</el-radio>
              <el-radio :label="3">非医疗机构</el-radio>
            </el-radio-group>
          </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>
        </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="230px"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="showDetailDialog(scope.row.vehicleUsingUnit)"
                >查看详情</el-link>
              </template>
            </el-table-column>
          </el-table>
        </table-mixin>
        <detailDialog
          ref="detailDialog"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { tableDataMixin } from '../../common/js/mixin'
import { roleMixin } from '../../common/js/roleMixin'
import detailDialog from './components/detailDialog.vue'
import { queryTotalCarByHospital } from '@/api/emergencyCar'
import { getDic } from '../../util/dict'
import { exportEmergencyResources, queryHospitalList } from '@/api/common'
export default {
  name: 'usage',
  mixins: [tableDataMixin, roleMixin],
  components: {
    detailDialog
  },
  data() {
    return {
      searchForm: {
        vehicleUsingUnit: '',
        area: ''
      },
      areaList: [],
      hospitalList: [],
      selectLoading: false,
      tableData: {
        loading: false,
        head: [
          {
            name: '车辆使用单位',
            key: 'vehicleUsingUnit',
            sortable: false,
            width: '250',
            fixed: false
          },
          {
            name: '辖区',
            key: 'area',
            sortable: false,
            width: '150',
            fixed: false,
            formatter: this.areaFormatter
          },
          {
            name: '单位性质',
            key: 'unitTypeStr',
            sortable: false,
            width: '100',
            fixed: false
          },
          {
            name: '救护车总数',
            key: 'carCountStr',
            sortable: false,
            width: '100',
            fixed: false
          },
          {
            name: '在用总数',
            key: 'useCountStr',
            sortable: false,
            width: '100',
            fixed: false,
          },
          {
            name: '闲置总数',
            key: 'freeCountStr',
            sortable: false,
            width: '100',
            fixed: false,
          },
          {
            name: '其他总数',
            key: 'otherCountStr',
            sortable: false,
            width: '100',
            fixed: false,
          }
        ],
        body: [],
        option: [],
        condition: []
      }
    }
  },
  mounted() {
    // 获取字典
    getDic(['area']).then(res => {
      this.areaList = res.area
    })
    //获取可选择的医院
    queryHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    this.search()
  },
  methods: {
    getTableData() {
      this.tableData.loading = true
      const params = {
        ...this.searchForm,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      }
      queryTotalCarByHospital(params).then((res) => {
        if (res.msg == 201) {
          this.dealTableResponse(res)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    areaFormatter(row, column, cellValue, index) {
      const area = this.areaList.find(i => i.val === cellValue)
      return area ? area.name : ''
    },
    showDetailDialog(vehicleUsingUnit = '') {
      console.log(vehicleUsingUnit)
      this.$refs.detailDialog.init(vehicleUsingUnit)
    },
    exportFile() {
      const params = {
        ...this.searchForm,
        exportType: 3
      }
      exportEmergencyResources(params).then(res => {
        if (res.msg === 201) {
          window.open(res.respData)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryHospitalList(query).then(res => {
        this.hospitalList = res.respData
        this.selectLoading = false;
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.mainContent {
  padding: 20px;
  .bt {
    text-align: right;
    margin-bottom: 16px;
  }
}
</style>
