<template>
  <div class="view-missionPlane">
    <div class="view-missionPlane-btn">
      <div class="view-missionPlane-btn-left">
        <el-button type="primary" @click="addRow()">添加</el-button>
        <span class="container">
          <span>任务要求数量：12/50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;任务要求总飞行时长：120/500小时</span>
        </span>
      </div>
      <div class="view-missionPlane-btn-right">
        机型&nbsp;&nbsp;
        <el-select v-model="modelValue" placeholder="请选择">
          <el-option v-for="item in modelList" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled"> </el-option>
        </el-select>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="aircraftNumber" label="飞机编号" align="center">
        <template slot-scope="scope">
          <i class="el-icon-edit cell-edit" @click="aircraftNumberClick(scope.$index, scope.row)"></i>
          <span>{{ scope.row.aircraftNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="model" label="所属机型" align="center"> </el-table-column>
      <el-table-column prop="taskLength" label="任务飞行时长/小时" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.taskLength.value || scope.row.taskLength.value == ''">
            <el-input @blur="taskLengthbBlur(scope.$index, scope.row)" type="text" v-model="scope.row.taskLength.value" />
          </template>
          <template v-else>
            <i @click="editTaskLength(scope.$index, scope.row)" class="el-icon-edit cell-edit"></i>
            <span>{{ scope.row.taskLength }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center"> </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleRemoveClick(scope.$index, scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :total="pagination.total"
      :page-size="pagination.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]"
      layout="sizes,total, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleIndexChange"
      style="padding: 20px; text-align: center"
    >
    </el-pagination>

    <el-dialog class="fittings-modal" title="选择任务飞机" :visible="fittingsVisible" width="1200px" :before-close="closefittingsVisible">
      <div class="fittings-modal-box">
        <div class="fittings-modal-box-left">
          <MenuTree :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
        </div>
        <div class="fittings-modal-box-right">
          <LTable ref="table" :defaultFetch="false" :columns="columns" :dataSource="dialogTableData" :options="options" :fetch="fetchTableData" :pagination="tableRequset"></LTable>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { columns, options, tableData, dialogTableData, defaultProps, mockMenuTreeData } from './index.js'
import vue from 'vue'
export default {
  name: 'MmissionPlane',
  components: {},
  data() {
    // 这里存放数据
    return {
      modelValue: '战斗机',
      modelList: [
        { label: '战斗机', value: '战斗机' },
        { label: '运输机', value: '运输机' },
        { label: '轰炸机', value: '轰炸机' }
      ],
      pagination: {
        total: 100,
        pageSize: 10,
        pageIndex: 1
      },
      tableData: tableData,
      fittingsVisible: false,
      currentNodeKey: '',
      menuTreeData: mockMenuTreeData,
      treeObj: {
        title: '所属分类',
        activityheight: '500px',
        searchIcon: false,
        configure: defaultProps
      },
      columns: columns(this),
      options,
      dialogTableData: dialogTableData,
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      activeIndex: null
    }
  },
  mounted() {},
  methods: {
    handleIndexChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleSizeChange(val) {
      console.log(`当前页: ${val}`)
    },
    addRow() {
      let obj = {
        aircraftNumber: '',
        model: '',
        taskLength: {
          value: ''
        },
        updateTime: ''
      }
      this.tableData.unshift(obj)
    },
    handleRemoveClick(index, row) {
      this.$confirm('是否删除该任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.tableData.splice(index, 1)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    taskLengthbBlur(index, row) {
      row.taskLength = row.taskLength.value
      this.tableData[index] = row
    },
    editTaskLength(index, row) {
      let value = row.taskLength
      row.taskLength = {}
      vue.set(row.taskLength, 'value', value)
      this.tableData[index] = row
    },
    aircraftNumberClick(index, row) {
      this.activeIndex = index
      this.fittingsVisible = true
    },
    treeNodeClick() {},
    fetchTableData() {},

    select(row) {
      for (const key in this.tableData[this.activeIndex]) {
        if (key && row[key]) {
          vue.set(this.tableData[this.activeIndex], key, row[key])
        }
      }

      this.fittingsVisible = false
    },

    closefittingsVisible() {
      this.fittingsVisible = false
      this.activeIndex = null
      this.activeRow = {
        aircraftNumber: '',
        model: '',
        taskLength: '',
        updateTime: ''
      }
    }
  }
}
</script>

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