<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="vechieType"
          label="车辆类型"
        >
          <el-select
            v-model="searchForm.vechieType"
            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>
        <div class="btns">
          <el-button
            type="primary"
            size="small"
            @click="search"
          >查询</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="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>
                <el-link
                  type="primary"
                  @click="delHandler(scope.row.id)"
                >删除</el-link>
                <el-link
                  type="primary"
                  @click="siteHandler(scope.row.id)"
                >绑定站点</el-link>
              </template>
            </el-table-column>
          </el-table>
        </table-mixin>
        <EmergencyCarDialog
          ref="emergencyCarDialog"
          @subsucc="search"
        />
        <BindSiteDialog
          ref="bindSiteDialog"
          @subsucc="search"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { tableDataMixin } from '../../common/js/mixin'
import EmergencyCarDialog from './components/emergencyCarDialog.vue'
import BindSiteDialog from './components/bindSiteDialog.vue'
import { queryEmergencyCarPageList, deleteEmergencyCar } from '@/api/emergencyCar'
import { getDic } from '../../util/dict'
export default {
  name: 'emergencyCar',
  mixins: [tableDataMixin],
  components: {
    EmergencyCarDialog,
    BindSiteDialog
  },
  data() {
    return {
      searchForm: {
        vechieUseStatus: '',
        vechieType: ''
      },
      vechieTypeList: [],
      areaList: [],
      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: '200',
            fixed: false
          },
          {
            name: '当前车辆状态',
            key: 'vechieUseStatus',
            sortable: false,
            width: '80',
            fixed: false,
            formatter: this.vechieUseStatusFormatter
          },
          {
            name: '是否院前急救',
            key: 'isPreHospitalVechie',
            sortable: false,
            width: '80',
            fixed: false,
            formatter: this.isPreHospitalVechieFormatter
          },
          {
            name: '车辆类型',
            key: 'vechieType',
            sortable: false,
            width: '120',
            fixed: false,
            formatter: this.vechieTypeFormatter
          }
        ],
        body: [],
        option: [],
        condition: []
      }
    }
  },
  mounted() {
    // 获取字典
    getDic(['vechie_type', 'area']).then(res => {
      this.vechieTypeList = res.vechie_type
      this.areaList = res.area
    })
    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.success(res.content)
          }
          this.search()
        })
      })

    }
  }
}
</script>
<style lang="scss" scoped>
.mainContent {
  padding: 20px;
  .bt {
    text-align: right;
    margin-bottom: 16px;
  }
}
</style>
