<template>
  <div class="view-AerospaceCompileLineList">
    <div class="view-AerospaceCompileLineList-left">
      <MenuTree :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-AerospaceCompileLineList-right">
      <div class="view-AerospaceCompileLineList-right-btn">
        <div class="view-AerospaceCompileLineList-right-btn-left">
          <el-button type="primary" @click="openDialogVisible()">自动生成</el-button>
          <el-button type="primary" @click="addRow()">添加</el-button>
          <el-button type="primary">导入</el-button>
          <el-button type="primary">导出</el-button>
        </div>
        <div class="view-AerospaceCompileLineList-right-btn-right">
          <div class="view-AerospaceCompileLineList-right-btn-right-select">
            <span>机型</span>
            <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>
          <el-input placeholder="请输入内容" v-model="searchValue" class="input-with-select">
            <el-button 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%">
          <el-table-column prop="sparePartsCoding" label="航材编码">
            <template slot-scope="scope">
              <i class="el-icon-edit cell-edit" @click="sparePartsCodingClick(scope.$index, scope.row)"></i>
              <span>{{ scope.row.sparePartsCoding }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="sparePartsName" label="航材名称" align="center"> </el-table-column>
          <el-table-column prop="meteringUnit" label="计量单位" align="center"> </el-table-column>
          <el-table-column prop="aircraft" label="所属飞机" align="center"> </el-table-column>
          <el-table-column prop="carryQuantity" label="携带数量" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.carryQuantity.value || scope.row.carryQuantity.value == ''">
                <el-input @blur="carryQuantityBlur(scope.$index, scope.row)" type="text" v-model="scope.row.carryQuantity.value" />
              </template>
              <template v-else>
                <i @click="editCarryQuantity(scope.$index, scope.row)" class="el-icon-edit cell-edit"></i>
                <span>{{ scope.row.carryQuantity }}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="unitPrice" label="单价/元" align="center"> </el-table-column>
          <el-table-column prop="amount" label="金额/元" align="center"> </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" align="center"> </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="handleRemoveClick(scope.$index, scope.row)" type="text" size="small">删除</el-button>
            </template>
          </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>

      <!-- 选择航材 -->
      <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="dialogCurrentNodeKey" :treedata="dialogMenuTreeData" @TreeNodeclick="dialogTreeNodeClick" v-bind="dialogTreeObj"> </MenuTree>
          </div>
          <div class="fittings-modal-box-right">
            <LTable ref="table" :defaultFetch="false" :columns="dialogColumns" :dataSource="dialogTableData" :options="dialogOptions" :fetch="dialogFetchTableData" :pagination="dialogTableRequset"></LTable>
          </div>
        </div>
      </el-dialog>

      <!-- 自动生成 -->
      <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>
    </div>
  </div>
</template>

<script>
import { mockTableData, tableData, dialogTreeObj, treeObj, dialogColumns, dialogOptions, dialogMenuTreeData, dialogTableData, addModeOptions } from './index'
import vue from 'vue'
import { useMessage } from '@/utils/element-ui'
export default {
  name: 'AerospaceCompileLineList',
  components: {},
  data() {
    // 这里存放数据
    return {
      currentNodeKey: '',
      menuTreeData: mockTableData,
      treeObj: treeObj,
      searchValue: '',
      modelValue: '战斗机',
      modelList: [
        { label: '战斗机', value: '战斗机' },
        { label: '运输机', value: '运输机' },
        { label: '轰炸机', value: '轰炸机' }
      ],
      tableData: tableData,
      pagination: {
        total: 100,
        pageSize: 10,
        pageIndex: 1
      },

      activeIndex: null,
      fittingsVisible: false,
      dialogCurrentNodeKey: '',
      dialogTreeObj: dialogTreeObj,
      dialogMenuTreeData: dialogMenuTreeData,
      dialogTableData: dialogTableData,
      dialogColumns: dialogColumns(this),
      dialogOptions: dialogOptions,
      dialogTableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      dialogVisible: false,
      formulaValue: '时长模型',
      formulaOptions: [
        { label: '时长模型', value: '时长模型' },
        { label: '次数模型', value: '次数模型' }
      ],
      formulaDisplay: '',
      addModeValue: '1',
      addModeOptions: addModeOptions
    }
  },
  mounted() {},
  methods: {
    handleIndexChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleSizeChange(val) {
      console.log(`当前页: ${val}`)
    },
    treeNodeClick() {},
    handleRemoveClick(index, row) {
      this.$confirm('是否删除该航材', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.tableData.splice(index, 1)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    carryQuantityBlur(index, row) {
      console.log(row)
      row.carryQuantity = row.carryQuantity.value
      row.amount = Number(row.carryQuantity) * Number(row.unitPrice)
      this.tableData[index] = row
    },
    addRow() {
      let obj = {
        sparePartsCoding: '',
        sparePartsName: '',
        meteringUnit: '',
        aircraft: '',
        carryQuantity: {
          value: ''
        },
        unitPrice: '',
        amount: '',
        updateTime: ''
      }
      this.tableData.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.fittingsVisible = true
      console.log(index, row)
    },
    closeStructureEquipping() {},
    dialogTreeNodeClick() {},
    dialogFetchTableData() {},
    closefittingsVisible() {
      this.fittingsVisible = false
      this.activeIndex = null
    },
    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
      this.activeIndex = null
    },
    openDialogVisible() {
      this.dialogVisible = true
    },
    generationHandleClose() {
      this.dialogVisible = false
    },
    generationHandelClick() {
      useMessage('success', '生成成功', 1000)
      this.dialogVisible = false
    }
  }
}
</script>

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