<template>
  <div class="view-CarrierListMachineryPreparation">
    <div class="view-AerospaceCompileLineList-left">
      <MenuTree ref="tree" nodeKey="flyTaskAircaftId" :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-AerospaceCompileLineList-right">
      <MissionDetail :detail="detail" />
      <div class="view-AerospaceCompileLineList-right-btn">
        <div class="view-AerospaceCompileLineList-right-btn-left"></div>
        <div class="view-AerospaceCompileLineList-right-btn-right">
          <el-input placeholder="请输入内容" @keyup.enter.native="getqueryCarryingMainteOrgan('searchValue')" v-model="searchValue" class="input-with-select">
            <el-button @click="getqueryCarryingMainteOrgan('searchValue')" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </div>

      <div class="view-AerospaceCompileLineList-right-table">
        <el-table :data="tableData" border style="width: 100%" height="calc(100vh - 420px)">
          <el-table-column prop="avmatCatalogCode" label="航材编码"> </el-table-column>
          <el-table-column prop="avmatCatalogName" label="航材名称" align="center"> </el-table-column>
          <el-table-column prop="specification" label="航材型号" align="center"> </el-table-column>
          <el-table-column prop="avmatCategoriesName" label="航材分类" align="center"> </el-table-column>
          <el-table-column prop="unit" label="计量单位" align="center"> </el-table-column>
          <el-table-column prop="aircaftCatalogCode" label="指向飞机" align="center"> </el-table-column>
          <el-table-column prop="carryQuantity" label="携带数量" align="center"> </el-table-column>
          <el-table-column prop="purchasePrice" label="单价/万元" align="center"> </el-table-column>
          <el-table-column prop="createBy" label="创建人" align="center"> </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" align="center"> </el-table-column>
        </el-table>
      </div>
      <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>

      <!-- 选择航材 -->
      <AviationMaterialsModal ref="AviationMaterialsModal" avmatType="0" @select="selectAviationMaterialsModal" />

      <!-- 自动生成 -->
      <el-dialog title="数量自动计算" :visible.sync="dialogVisible" width="500px" :before-close="generationHandleClose">
        <div class="auto-generation">
          <span>计算模型</span>
          <el-select v-model="formulaValue" placeholder="请选择">
            <el-option v-for="item in formulaOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </div>
        <div class="auto-generation">
          <span>公式展示</span>
          <el-input v-model="formulaDisplay" placeholder="公式展示"></el-input>
        </div>
        <!-- <div class="auto-generation">
          <span>添加方式</span>
          <el-radio-group v-model="addModeValue">
            <el-radio v-for="item in addModeOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </div> -->

        <span slot="footer" class="dialog-footer">
          <el-button @click="generationHandleClose">取 消</el-button>
          <el-button type="primary" @click="generationHandelClick">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 同步生成 -->
      <el-dialog title="同步生成" :visible.sync="fjdialogVisible" width="500px" :before-close="fjHandleClose">
        <div class="auto-generation">
          <span>目标飞机</span>
          <el-select ref="onSelect" v-model="fjValue" placeholder="请选择">
            <el-option hidden :value="fjValue" :label="fjName"></el-option>
            <el-tree default-expand-all :data="menuTreeData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
          </el-select>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="fjHandleClose">取 消</el-button>
          <el-button type="primary" @click="fjHandelClick">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import vue from 'vue'
import { treeObj, addModeOptions } from './index'
import { useMessage } from '@/utils/element-ui'
import { queryFlyTaskAircraft, queryCarryingMainteOrgan, batchAddCarryingMainteOrgan, removeCarryingMainteOrgan, syncCarryingMainteOrgan } from '@/api/carrierStandard/carrierListMachineryPreparation'

export default {
  name: 'CarrierListMachineryPreparationLook',
  components: {},
  data() {
    return {
      detail: null,
      taskId: this.$route.params.id,
      fjdialogVisible: false,
      fjValue: '', //
      fjName: '',
      currentNodeKey: 'null',
      flyTaskActive: '',
      menuTreeData: [],
      treeObj: treeObj,
      defaultProps: {
        children: 'flyTaskAircraftVos',
        label: 'aircaftCatalogId'
      },
      searchValue: '',
      modelValue: '战斗机',
      modelList: [
        { label: '战斗机', value: '战斗机' },
        { label: '运输机', value: '运输机' },
        { label: '轰炸机', value: '轰炸机' }
      ],

      tableData: [],
      pagination: {
        total: 100,
        pageSize: 10,
        pageIndex: 1
      },
      cache: 'fdz',
      activeIndex: null,

      dialogVisible: false,
      formulaValue: '时长模型',
      formulaOptions: [
        { label: '时长模型', value: '时长模型' },
        { label: '次数模型', value: '次数模型' }
      ],
      formulaDisplay: '',
      addModeValue: '1',
      addModeOptions: addModeOptions,
      cacheList: []
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    async fjHandelClick() {
      let postData = {
        aircaftCatalogId: this.fjValue,
        aimAircaftCatalogId: this.currentNodeKey
      }
      const { code, data } = await syncCarryingMainteOrgan(postData)
      if (code == 200) {
        this.fjdialogVisible = false
        this.fjValue = ''
        this.fjName = ''
        useMessage('success', '同步成功', 1000)
        this.getqueryCarryingMainteOrgan()
      }
    },
    fjHandleClose() {
      this.fjValue = ''
      this.fjName = ''
      this.fjdialogVisible = false
    },
    openfj() {
      this.fjdialogVisible = true
    },
    handleNodeClick(data) {
      const { aircaftCatalogId, flyTaskAircaftId } = data
      this.fjValue = flyTaskAircaftId
      this.fjName = aircaftCatalogId
      this.$refs.onSelect.blur()
    },
    async initData() {
      let postData = {
        taskId: this.taskId
      }
      const { code, data } = await queryFlyTaskAircraft(postData)
      if (code == 200) {
        this.menuTreeData = data.flys
        this.detail = data
        this.currentNodeKey = this.menuTreeData[0].flyTaskAircaftId
        this.currentNode = this.menuTreeData[0]
        this.getqueryCarryingMainteOrgan()
        this.$store.commit('app/updataTagsItemMetaTitle', { id: this.$route.params.id, otherTitle: '携行清单机务编制-查看详情', newTitle: `携行清单机务编制-查看详情-${this.detail.taskName}` })
      }
    },

    async getqueryCarryingMainteOrgan(searchValue) {
      searchValue ? (this.pagination.pageIndex = 1) : (this.pagination.pageIndex = this.pagination.pageIndex)
      let postData = {
        pageIndex: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
        flyTaskAircaftId: this.currentNodeKey,
        taskId: this.taskId,
        queryParam: this.searchValue,
        avmatType: '0'
      }
      const { code, data } = await queryCarryingMainteOrgan(postData)
      if (code == 200) {
        this.tableData = data.list
        this.pagination.total = data.totalCount
      }
    },

    handleIndexChange(val) {
      this.pagination.pageIndex = val
      this.getqueryCarryingMainteOrgan()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getqueryCarryingMainteOrgan()
    },
    treeNodeClick(node) {
      this.pagination.pageIndex = 1
      this.currentNodeKey = node.flyTaskAircaftId
      this.flyTaskActive = node.aircaftCatalogCode
      this.currentNode = node
      this.getqueryCarryingMainteOrgan()
    },
    handleRemoveClick(index, row) {
      if (row.mainteOrganId) {
        this.$confirm('是否删除该航材', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            const { code, data } = await removeCarryingMainteOrgan([row.mainteOrganId])
            if (code == 200) {
              this.getqueryCarryingMainteOrgan()
              useMessage('success', '删除成功', 1000)
            }
          })
          .catch(() => {})
      } else {
        this.tableData.splice(index, 1)
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      }
    },
    carryQuantityBlur(index, row) {
      row.carryQuantity = row.carryQuantity.value
      this.tableData[index] = row
    },
    addRow() {
      let obj = {
        avmatCatalogCode: '', //编码
        avmatCatalogName: '', // 航材名称
        specification: '', // 航材型号
        avmatCategoriesName: '', // 航材分类
        unit: '',
        aircaftCatalogCode: this.flyTaskActive, // 指向飞机
        flyTaskAircaftId: this.currentNodeKey, // 指向飞机的ID
        taskId: this.taskId,
        carryQuantity: {
          // 携带数量
          value: ''
        },
        purchasePrice: '', // 价格
        updateTime: '' // 更新时间
      }
      this.tableData.unshift(obj)
      this.cacheList.unshift(obj)
    },
    editCarryQuantity(index, row) {
      let value = row.carryQuantity
      row.carryQuantity = {}
      vue.set(row.carryQuantity, 'value', value)
      this.tableData[index] = row
    },
    sparePartsCodingClick(index, row) {
      this.activeIndex = index
      this.$refs.AviationMaterialsModal.openFittings()
    },
    closeStructureEquipping() {},
    dialogFetchTableData() {},

    selectAviationMaterialsModal(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
      this.activeIndex = null
    },
    openDialogVisible() {
      this.dialogVisible = true
    },
    generationHandleClose() {
      this.dialogVisible = false
    },
    generationHandelClick() {
      useMessage('success', '生成成功', 1000)
      this.dialogVisible = false
    },
    async save() {
      for (let index = 0; index < this.tableData.length; index++) {
        const element = this.tableData[index]
        if (element.avmatCatalogCode == '' || element.carryQuantity == '') {
          useMessage('error', `第${index + 1}行数据没有填写完整`, 1000)
          return
        }
      }
      const { code, data } = await batchAddCarryingMainteOrgan(this.tableData)
      if (code == 200) {
        useMessage('success', '保存成功', 1000)
        this.cacheList = []
        this.getqueryCarryingMainteOrgan()
      }
    }
  }
}
</script>

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