/*
 *@description:司机管理
 *@author: hudw
 *@date: 2022-03-14 10:40:55
*/
<template>
  <div class="pages">
    <driver-details :options="options" :st="detailsState" :formExtendedField="formExtendedField" :record="currentMainData" @back="onBack" />

    <div v-show="detailsState === 0">
      <!-- <p-card style="margin-bottom: 10px;">
        <template slot="title">
          <searchBar ref="searchBar" @submit="submit"></searchBar>
        </template>
      </p-card> -->
      <p-card :bordered="false">
        <searchBar slot="title" ref="searchBar" @submit="submit"></searchBar>
        <a-space slot="extra">
          <p-button @click="add_item" type="primary" icon="plus">新增</p-button>
          <import-Button :importUrl="importUrl" :tmpUrl="mainDownUrl" fileName="司机模板" />
          <p-button @click="is_enable_item(1)" :disabled="selectedRowKeys.length === 0">启用</p-button>
          <p-button @click="is_enable_item(0)" :disabled="selectedRowKeys.length === 0">停用</p-button>
          <p-button @click="delete_items(selectedRowKeys)" :disabled="selectedRowKeys.length === 0">删除</p-button>
        </a-space>
        <mainTable
          ref="mainTable"
          :tableExtendedField="tableExtendedField"
          @selectChange="selectChange"
          :activeID="activeID"
          @update="handleUpdate"
          @view="handleView"
          @clickRow="handleRowClick"
          @deleteMainItem="handelClearAll"
        />
        <a-divider />
        <children-table ref="childrenTable" :clearAll="clearAll" :currentMainRow="currentRowData" />
      </p-card>
    </div>
  </div>
</template>
<script>
import mainTable from './model/mainTable.vue' /**司机主表 */
import driverDetails from './model/driverDetails/index.driverDetails.vue' /**司机详情 */
import searchBar from './model/searchBar.vue'
import childrenTable from './model/childrenTable/index.childrenTable.vue'
import { mainIsEnable, deleteMainItem, importUrl, mainDownUrl } from './js/api'
import { resultExtendedField, initDictionary } from '@/blocks/utils'
import importButton from '@/blocks/components/importButton/index.importButton.vue'
import { getOrganization } from '@/blocks/components/formConfig/departmenTree.js'

export default {
  components: { importButton, searchBar, mainTable, childrenTable, driverDetails },
  data() {
    return {
      clearAll: false,
      options: {},
      activeKey: '1',
      selectedRowKeys: [],
      confirmLoading: false,
      activeID: '',
      detailsState: 0,
      currentMainData: null,
      currentRowData: null,
      tableExtendedField: [],
      formExtendedField: [],
      importUrl,
      mainDownUrl,
    }
  },
  mounted() {
    let _this = this

    getOrganization(this)
    initDictionary([{ objectcode: 'DRIVER_TYPE' }]).then((res) => {
      for (let key in res) {
        res[key].forEach((item) => {
          if (!isNaN(item.value)) {
            item.value = +item.value
          }
        })
      }
      _this.options = { ..._this.options, ...res }
    })
    resultExtendedField({ extendedObjectCode: '11' }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table

        _this.formExtendedField = data.form
      }
    })
  },

  methods: {

    selectChange(selectKey, selectData) {

      this.selectedRowKeys=selectKey
    },
    handelClearAll() {
      //清空所有细明table
      this.clearAll = true
      this.$nextTick(() => {
        this.clearAll = false
      })
    },

    is_enable_item(number) {
      let arr = this.selectedRowKeys.map((id) => {
        return { id }
      })
      return mainIsEnable(this, arr, number)
    },
    submit() {
      this.clearAll = true
      return this.$refs.mainTable.reload().then(() => {
        this.clearAll = false
      })
    },
    add_item(data) {
      this.detailsState = 1
      this.currentMainData = null
    },
    delete_items(ids) {
      let p = ids.map((id) => {
        return { id }
      })

      deleteMainItem(this, p).then(() => {
        this.handelClearAll()
      })
    },
    handleView(data) {
      this.detailsState = 3
      this.currentMainData = data
    },
    handleUpdate(data) {
      this.detailsState = 2
      this.currentMainData = data
    },
    handleRowClick(event, key, data) {
      this.currentRowData = data
    },
    onBack(data, isSave) {
      this.detailsState = 0
      if (!data) return
      if (isSave) {
        this.clearAll = true
        this.$refs.mainTable.reload().then(() => {
          if (data.id) {
            this.activeID = data.id
            this.currentRowData = { ...data }
            this.clearAll = false
          }
        })
      }
    },
    handle_ok(value) {
      console.log(value)
    },
  },
}
</script>
