<template>
  <div class="view-carrierFlightMission">
    <div class="view-carrierFlightMission-FlightMission-btn">
      <div class="view-carrierFlightMission-FlightMission-btn-box">
        <el-button type="success" @click="addDialog()">新增任务</el-button>
        <el-button type="success" @click="addChildDialog()">新增子任务</el-button>
        <el-button :disabled="!removeArr.length" type="warning" @click="selectRemove">删除</el-button>
        <el-dropdown split-button type="primary" style="margin-left: 10px" trigger="click" @command="handleExport">
          更多
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="importOperation">导入</el-dropdown-item>
            <el-dropdown-item command="allExport" :disabled="tableData.length == 0">导出</el-dropdown-item>
            <el-dropdown-item command="downloadTemplate">下载数据模板</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="view-carrierFlightMission-FlightMission-btn-right">
        <div class="input-with-select">
          <span>状态</span>
          <el-select v-model="statusValue" placeholder="请选择" @change="fetchTableData('search')">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </div>

        <el-input @keyup.enter.native="fetchTableData('search')" placeholder="请输入任务名称/代号/带队领导" v-model="searchValue" class="input-select">
          <el-button @click="fetchTableData('search')" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
    </div>
    <div class="view-carrierFlightMission-FlightMission-table">
      <LTable ref="table" @select-all="selectAll" @selection-change="selection" :defaultFetch="false" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData"></LTable>
    </div>
    <CrudDialog v-bind="modalConfig" ref="NewDialogRef" @crudDialogSubmit="crudDialogSubmit"></CrudDialog>
    <!-- 导入 -->
    <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
  </div>
</template>

<script>
import { columns, options, modalConfig } from './index'
import { queryFlyTaskTree, queryFlyTaskMain, getTEnvFactorDict, addFlyTask, updateFlyTask, removeFlyTask, downLoadFile, exportExcel } from '@/api/task/carrierFlightMission'
import { deepClone } from '@/utils/index'
import ImportDialog from './components/importDialog/index.vue'

import { statusList } from '@/utils/status'

export default {
  name: 'carrierFlightMission',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      statusList: statusList,
      statusValue: null,
      searchValue: '',
      activeName: 'flightMission',
      columns: columns(this),
      options,
      tableData: [],
      tableRequset: {
        searchValue: ''
      },
      modalConfig: modalConfig(this),
      removeArr: [],
      currentRow: []
    }
  },
  mounted() {
    this.fetchTableData()
    //  this.getTaskTree()
  },
  methods: {
    addDialog() {
      this.queryFlyTaskMain()
      this.getTEnvFactorDict()
      let config = {
        type: 'add',
        dataShow: {}, // 回填的值（字段对应上）
        title: '新增任务'
      }
      this.modalConfig.formItems[0].isHidden = true
      this.$refs.NewDialogRef.open(config)
    },

    addChildDialog() {
      if (this.removeArr.length != 1) {
        this.$message({
          type: 'warning',
          message: '请选择一个待提交的主任务!'
        })
        return
      }
      this.currentRow = this.tableData.find((row) => row.taskId == this.removeArr)
      if (this.currentRow == undefined || this.currentRow.status != '1') {
        this.$message({
          type: 'warning',
          message: '请选择一个待提交的主任务!'
        })
        return
      }
      this.queryFlyTaskMain()
      this.getTEnvFactorDict()
      let config = {
        type: 'addChild',
        dataShow: { parentId: this.currentRow.taskId }, // 回填的值（字段对应上）
        title: '新增子任务'
      }
      this.modalConfig.formItems[0].isHidden = false
      this.$refs.NewDialogRef.open(config)
    },

    async crudDialogSubmit(data) {
      const { dialogData, type } = data
      dialogData.taskTime = dialogData.taskTime.join('/')
      if (type == 'add' || type == 'addChild') {
        const { code } = await addFlyTask(dialogData)
        if (code == 200) {
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
        }
      }
      if (type == 'upDate') {
        const { code } = await updateFlyTask(dialogData)
        if (code == 200) {
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        }
      }
    },

    edit(row) {
      this.queryFlyTaskMain()
      this.getTEnvFactorDict()
      const newRow = deepClone(row)
      const tiemArr = newRow.taskTime.split('/')
      newRow.taskTime = tiemArr
      let config = {
        type: 'upDate',
        dataShow: newRow,
        title: '编辑任务'
      }
      if (newRow.parentId == null || newRow.parentId == '') {
        this.modalConfig.formItems[0].isHidden = true
      } else {
        this.modalConfig.formItems[0].isHidden = false
      }
      this.$refs.NewDialogRef.open(config)
    },

    toCompile(row) {
      this.$router.push('/task/carrierFlightMission/compile')
      // this.dialogTitle = '编辑'
      // this.form = row
      // this.dialogVisible = true
    },
    toAircraft(row) {
      this.$router.push(`/task/carrierFlightMission/aircraft/${row.taskId}/${row.status}`)
    },
    remove(row) {
      if (row.children && row.children.length > 0) {
        this.$message({
          type: 'warning',
          message: '请先删除子级!'
        })
        return
      }

      this.$confirm('是否删除该任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await removeFlyTask([row.taskId])
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchTableData()
          }
        })
        .catch(() => {})
    },
    selectRemove() {
      this.$confirm('是否删除该任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await removeFlyTask(this.removeArr)
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchTableData()
            this.$refs.table.clearSelection()
          }
        })
        .catch(() => {})
    },

    audit(row) {
      this.$confirm('是否提交该任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          var tName = ''
          const childIdList = []
          if (row.children != null) {
            row.children.forEach((item) => {
              childIdList.push(item.taskId)
              if (item.flyTaskAircraftNum <= 0) {
                tName = item.taskName
              }
            })
          } else {
            if (row.flyTaskAircraftNum <= 0) {
              tName = row.taskName
            }
          }
          if (tName != '') {
            return this.$message({
              type: 'warning',
              message: tName + '未添加任务飞机!'
            })
          }

          let postData = {
            taskId: row.taskId,
            childIdList: childIdList,
            status: '2'
          }
          const { code, data } = await updateFlyTask(postData)
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '提交该任务成功!'
            })
            this.fetchTableData()
          }
        })
        .catch(() => {})
    },

    // 跳转航材
    toAerospaceSpareParts(row) {
      this.$router.push('/task/carrierFlightMission/aerospaceSpareParts')
    },
    // 跳转任务飞机
    toMissionPlane(row) {
      this.$router.push('/task/carrierFlightMission/missionPlane')
    },
    // 全选
    selectAll(data) {
      this.removeArr = []
      if (data.length > 0) {
        data.forEach((item) => {
          this.removeArr.push(item.taskId)
        })
      } else {
        this.removeArr = []
      }
    },
    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item.taskId)
      })
    },
    async fetchTableData(search) {
      let postData = {
        status: this.statusValue,
        queryParam: this.searchValue
      }
      const { code, data } = await queryFlyTaskTree(postData)
      if (code == 200) {
        this.tableData = data
      }
    },
    //获取主任务集合
    async queryFlyTaskMain() {
      const { code, data } = await queryFlyTaskMain()
      if (code == 200) {
        this.modalConfig.formItems[0].valueOptions = data
      }
    },
    async getTEnvFactorDict() {
      const { code, data } = await getTEnvFactorDict()
      if (code == 200) {
        this.modalConfig.formItems[6].valueOptions = data.find((item) => item.dictType == '环境因子字典').dictDataList
        this.modalConfig.formItems[11].valueOptions = data.find((item) => item.dictType == '转场形式字典').dictDataList
        this.modalConfig.formItems[13].valueOptions = data.find((item) => item.dictType == '任务强度字典').dictDataList
      }
    },
    //更多按钮
    handleExport(type) {
      this[type]()
    },
    //导入
    importOperation() {
      this.$refs.ImportDialog.triggerDialog()
    },
    //导入回写
    ImportDialogCallBack() {
      this.fetchTableData()
    },

    //导出全部
    async allExport() {
      var status = this.statusValue
      if (status == null) {
        status = ''
      }
      let params = {
        queryParam: this.searchValue,
        status: status
      }
      await exportExcel(params)
    },
    //下载模板
    async downloadTemplate() {
      await downLoadFile()
    }
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
