<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%"
            :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="licensePlateNumber"
        >
          <el-input
            placeholder="请输入车牌号"
            v-model="searchForm.licensePlateNumber"
          />
        </el-form-item>
        <el-form-item
          prop="vechieTypeNew"
          label="车辆类型"
        >
          <el-select
            v-model="searchForm.vechieTypeNew"
            placeholder="请选择车辆类型"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              v-for="item in vechieTypeList"
              :label="item.name"
              :key="item.id"
              :value="item.val"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="vechieUseStatus"
          label="车辆状态"
        >
          <el-radio-group v-model="searchForm.vechieUseStatus">
            <el-radio label="">全部</el-radio>
            <el-radio label="1">在用</el-radio>
            <el-radio label="2">闲置</el-radio>
            <el-radio label="3">其他</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          prop="siteId"
          label="站点"
        >
          <el-select
            v-model="searchForm.siteId"
            placeholder="请选择站点"
            style="width: 100%"
            :clearable="true"
          >
            <el-option
              v-for="item in siteList"
              :label="item.siteName"
              :key="item.id"
              :value="item.id"
            />
          </el-select>
        </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>
        <el-form-item
          prop="isPreHospitalVechie"
          label="是否为院前救护车："
        >
          <el-radio-group v-model="searchForm.isPreHospitalVechie">
              <el-radio label="">全部</el-radio>
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</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>
          <el-button
            type="primary"
            size="small"
            v-if="!checkPermission()"
            @click="showEmergencyCarDialog()"
          >新增</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="showEmergencyCarDialog('detail',scope.row.id)"
                >查看详情</el-link>
                <el-link
                  type="primary"
                  @click="showEmergencyCarDialog('update', scope.row.id)"
                >修改</el-link>
                <!--4786放开限制 v-if="!checkPermission() && scope.row.auditStatus != 4" -->
                <el-link
                  type="primary"
                  @click="delHandler(scope.row.id)"
                >删除</el-link>
                <el-link
                  type="primary"
                  v-if="!scope.row.siteId && !checkPermission()"
                  @click="siteHandler(scope.row.id)"
                >绑定站点</el-link>
                <el-link
                  type="primary"
                  v-if="(scope.row.auditStatus === 1 || scope.row.auditStatus === 5) && checkPermission()"
                  @click="auditHandler(scope.row.id)"
                >审核</el-link>
                <el-link
                  type="primary"
                  v-if="scope.row.siteId && checkPermission() && !isRoleDisabled"
                  @click="unboundHandler(scope.row.id)"
                >解绑</el-link>
                <el-link
                  type="primary"
                  v-if="checkPermission()"
                  @click="transferHandler(scope.row)"
                >划拨</el-link>
              </template>
            </el-table-column>
          </el-table>
        </table-mixin>
        <EmergencyCarDialog
          ref="emergencyCarDialog"
          @subsucc="search"
        />
        <BindSiteDialog
          ref="bindSiteDialog"
          @subsucc="search"
        />
        <TransferDialog
          ref="transferDialog"
          @subsucc="search"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { tableDataMixin } from '../../common/js/mixin'
import { roleMixin } from '../../common/js/roleMixin'
import EmergencyCarDialog from './components/emergencyCarDialog.vue'
import BindSiteDialog from './components/bindSiteDialog.vue'
import TransferDialog from './components/transferDialog'
import { queryEmergencyCarPageList, deleteEmergencyCar, toExamineEmergencyCar, unBindSiteToEmergencyCar } from '@/api/emergencyCar'
import { exportEmergencyResources, queryTransferHospitalList } from '@/api/common'
import { queryEmergencySiteList } from '@/api/site'
import { getDic } from '../../util/dict'
export default {
  name: 'emergencyCar',
  mixins: [tableDataMixin, roleMixin],
  components: {
    EmergencyCarDialog,
    BindSiteDialog,
    TransferDialog
  },
  
  data() {
    return {
      searchForm: {
        vehicleUsingUnit: '',
        area: '',
        licensePlateNumber: '',
        vechieUseStatus: '',
        vechieTypeNew: '',
        siteId: '',
        isPreHospitalVechie: ''
      },
      hospitalList: [],
      selectLoading: false,
      vechieTypeList: [],
      auditStatusList: [],
      areaList: [],
      siteList: [],
      tableData: {
        loading: false,
        head: [
          {
            name: '车辆使用单位',
            key: 'vehicleUsingUnit',
            sortable: false,
            width: '200',
            fixed: false
          },
          {
            name: '辖区',
            key: 'area',
            sortable: false,
            width: '120',
            fixed: false,
            formatter: this.areaFormatter
          },
          {
            name: '救护车牌号',
            key: 'licensePlateNumber',
            sortable: false,
            width: '120',
            fixed: false
          },
          {
            name: '车辆负责人',
            key: 'vehicleManager',
            sortable: false,
            width: '100',
            fixed: false
          },
          {
            name: '当前车辆状态',
            key: 'vechieUseStatus',
            sortable: false,
            width: '80',
            fixed: false,
            formatter: this.vechieUseStatusFormatter
          },
          {
            name: '是否为院前救护车',
            key: 'isPreHospitalVechie',
            sortable: false,
            width: '120',
            fixed: false,
            formatter: this.isPreHospitalVechieFormatter
          },
          {
            name: '车辆类型',
            key: 'vechieTypeNew',
            sortable: false,
            width: '120',
            fixed: false,
            formatter: this.vechieTypeFormatter
          },
          {
            name: '审核状态',
            key: 'auditStatusStr',
            sortable: false,
            width: '100',
            fixed: false
          },
          {
            name: '所属站点',
            key: 'siteName',
            sortable: false,
            width: '100',
            fixed: false
          }
        ],
        body: [],
        option: [],
        condition: []
      }
    }
  },
  mounted() {
    // 获取字典
    getDic(['vechie_type', 'area', 'audit_status']).then(res => {
      this.vechieTypeList = res.vechie_type
      this.areaList = res.area
      this.auditStatusList = res.audit_status
    })
    queryEmergencySiteList().then(res => {
      if (res.msg === 201) {
        this.siteList = res.respData
      } else {
        this.$message.error(res.content)
      }
    })
    //获取可选择的医院
    queryTransferHospitalList().then(res => {
      this.hospitalList = res.respData
    })
    this.search()
  },
  methods: {
    getTableData() {
      this.tableData.loading = true
      const params = {
        ...this.searchForm,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      }
      queryEmergencyCarPageList(params).then((res) => {
        if (res.msg == 201) {
          this.dealTableResponse(res)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    showEmergencyCarDialog(type = 'add', id = '') {
      this.$refs.emergencyCarDialog.init(type, id)
    },
    siteHandler(id = "") {
      this.$refs.bindSiteDialog.init(id)
    },
    areaFormatter(row, column, cellValue, index) {
      const area = this.areaList.find(i => i.val === cellValue)
      return area ? area.name : ''
    },
    vechieUseStatusFormatter(row, column, cellValue, index) {
      const vechieUseStatusList = ['', '在用', '闲置', '其他']
      return vechieUseStatusList[cellValue] || ''
    },
    isPreHospitalVechieFormatter(row, column, cellValue, index) {
      const isPreHospitalVechieList = ['', '是', '否']
      return isPreHospitalVechieList[cellValue] || ''
    },
    vechieTypeFormatter(row, column, cellValue, index) {
      const vechieType = this.vechieTypeList.find(i => i.val === cellValue)
      return vechieType ? vechieType.name : ''
    },
    delHandler(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        deleteEmergencyCar({ 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: 1
      }
      exportEmergencyResources(params).then(res => {
        if (res.msg === 201) {
          window.open(res.respData)
        } else {
          this.$message.error(res.content)
        }
      })
    },
    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) {
      toExamineEmergencyCar({ id: id, auditStatus: auditStatus }).then(res => {
        if (res.msg === 201) {
          this.$message.success('操作成功！')
        } else {
          this.$message.error(res.content)
        }
        this.search()
      })
    },
    unboundHandler(id) {
      this.$confirm('是否确定解绑？', '提示', {
        confirmButtonText: '解绑',
        cancelButtonText: '取消',
        distinguishCancelAndClose: true,
        type: 'warning'
      }).then(() => {
        unBindSiteToEmergencyCar({ id: id }).then(res => {
          if (res.msg === 201) {
            this.$message.success('操作成功！')
          } else {
            this.$message.error(res.content)
          }
          this.search()
        })
      })
      
    },
    transferHandler(row) {
      this.$refs.transferDialog.init(row)
      console.log(row)
    },
    remoteMethod(query) {
      this.selectLoading = true;
      queryTransferHospitalList(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>
