<template>
  <div class="view-carrierSailingMaterialConsumeRecord">
    <div class="common-box">
      <div class="common-box-button">
        <el-button type="success" @click="handleAdd">新增</el-button>
        <el-button type="warning" @click="handleDelete(removeArr)" :disabled="removeArr.length == 0">删除</el-button>
        <el-button type="primary" @click="handleExport('导出')">导出</el-button>
      </div>
      <div class="common-box-search">
        <el-input placeholder="请输入分类名称、编号" v-model="queryParam" class="input-with-select" @keyup.enter.native="fetchTableData">
          <el-button slot="append" icon="el-icon-search" @click="fetchTableData"></el-button>
        </el-input>
      </div>
    </div>
    <LTable ref="table" @selection-change="selection" :defaultFetch="true" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="pagination" />
    <CrudDialog v-bind="modalConfig" ref="NewDialogRef" @crudDialogSubmit="crudDialogSubmit"></CrudDialog>
  </div>
</template>

<script>
import { columns, options, modalConfig } from './index'
import { removeOveryearAvmatConsume, updateOveryearAvmatConsume, addOveryearAvmatConsume, getOveryearAvmatConsume } from '@/api/carrierStandard/carrierSailingMaterialConsumeRecord'
export default {
  name: 'carrierSailingMaterialConsumeRecord',
  data() {
    // 这里存放数据
    return {
      searchValue: '',
      removeArr: [],
      columns: columns(this),
      modalConfig: modalConfig(this),
      options,
      // 分页
      queryParam: '',
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 20
      },
      tableData: []
    }
  },

  methods: {
    handle() {
      this.$router.push('/carrierStandard/carrierSailingMaterialConsumeRecord/outboundRecord')
    },

    handOutboundRecord(row) {
      //出库记录
      this.$router.push(`/carrierStandard/carrierSailingMaterialConsumeRecord/outboundRecord/${row.consumeDataId}`)
    },

    handWarehousingRecord(row) {
      //入库记录
      this.$router.push(`/carrierStandard/carrierSailingMaterialConsumeRecord/warehousingRecord/${row.consumeDataId}`)
    },

    handSummarizedCalculation(row) {
      //汇总计算
      this.$router.push(`/carrierStandard/carrierSailingMaterialConsumeRecord/summarizedCalculation/${row.consumeDataId}`)
    },

    async fetchTableData() {
      if (this.$refs.table) {
        this.$refs.table.clearSelection()
      }
      const { pageIndex, pageSize } = this.pagination
      const {
        data: { list, totalCount }
      } = await getOveryearAvmatConsume({
        queryParam: this.queryParam,
        pageIndex,
        pageSize
      })
      this.tableData = list
      this.pagination.total = totalCount
    },

    handleAdd() {
      let config = {
        type: 'add',
        dataShow: {}, // 回填的值（字段对应上）
        title: '新增'
      }
      this.$refs.NewDialogRef.open(config)
    },

    handUpdate(row) {
      let config = {
        type: 'update',
        dataShow: row, // 回填的值（字段对应上）
        title: '编辑'
      }
      this.$refs.NewDialogRef.open(config)
    },

    handleDelete(params) {
      this.$confirm('是否确认删除选中数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { success } = await removeOveryearAvmatConsume(params.map((e) => e.consumeDataId))
          if (success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          }
          this.fetchTableData()
        })
        .catch(() => {})
    },

    // 单选
    selection(data) {
      this.removeArr = data
    },

    handleExport(type) {
      // let msg = ''
      // type == '下载模板' ? (msg = '是否确认下载该模板') : (msg = `是否${type}该航材`)
      // this.$confirm(msg, '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     getImportModel(0)
      //   })
      //   .catch(() => {})
      // this.fetchTableData()
    },

    async addTableRow(params) {
      try {
        const { success } = await addOveryearAvmatConsume(params)
        if (success) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        }
      } catch (error) {}
    },

    async updateTableRow(params) {
      const { consumeDataId, year, aircraftNum, flyDuration, affiliatedTroop } = params
      try {
        const { success } = await updateOveryearAvmatConsume({ consumeDataId, year, aircraftNum, flyDuration, affiliatedTroop })
        if (success) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        }
      } catch (error) {}
    },

    async crudDialogSubmit(data) {
      const add_Type = 'add'
      const add_Update = 'update'
      const { type, dialogData } = data
      switch (type) {
        case add_Type:
          await this.addTableRow(dialogData)
          break
        case add_Update:
          await this.updateTableRow(dialogData)
          break
      }
      this.fetchTableData()
    }
  }
}
</script>

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