<template>
  <div class="view-priceList">
    <div class="view-priceList-left">
      <MenuTree nodeKey="avmatCategoriesCode" :currentNodeKey="dialogCurrentNodeKey" :treedata="dialogMenuTreeData" @TreeNodeclick="dialogTreeNodeClick" v-bind="dialogTreeObj"> </MenuTree>
    </div>
    <div class="view-priceList-right">
      <div style="margin-bottom: 10px">
        <el-button type="warning" @click="handleCreate">生成</el-button>
        <el-button type="primary" @click="handleExport('导出')">导出</el-button>
      </div>

      <LTable ref="table" :defaultFetch="false" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="pagination"></LTable>
    </div>
  </div>
</template>

<script>
import { columns, data, options, dialogTreeObj } from './priceList'
import { roleAvmatCategoriesTree, getAvmatCatalog } from '@/api/carrierStandard/carrierListMachineryPreparation'
import { socialSecurityDetailsPage, generatePriceList } from '@/api/task/carrierFlightMission'

export default {
  name: 'PriceList',
  components: {},
  data() {
    // 这里存放数据
    return {
      dialogCurrentNodeKey: '',
      dialogMenuTreeData: [],
      dialogTreeObj,

      options,
      columns: columns(this),
      tableData: [],
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 20
      }
    }
  },
  mounted() {
    this.getroleAvmatCategoriesTree()
  },
  methods: {
    async getroleAvmatCategoriesTree() {
      const { code, data } = await roleAvmatCategoriesTree()
      if (code == 200) {
        this.dialogMenuTreeData = [
          {
            avmatCategoriesName: '全部',
            avmatCategoriesCode: '',
            children: data
          }
        ]
        this.fetchTableData()
      }
    },
    dialogTreeNodeClick(node) {
      this.dialogCurrentNodeKey = node.avmatCategoriesCode
      this.fetchTableData()
    },
    async fetchTableData() {
      let postData = {
        pageSize: this.pagination.pageSize,
        pageIndex: this.pagination.pageIndex,
        socialSecurityId: this.$route.params.id,
        avmatCategoriesId: this.dialogCurrentNodeKey
      }
      const { code, data } = await socialSecurityDetailsPage(postData)
      if (code == 200) {
        this.tableData = data.list
        this.pagination.total = data.totalCount
      }
    },
    handleCreate() {
      this.$confirm('是否确认生成数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = {
            socialSecurityId: this.$route.params.id
          }
          await generatePriceList(params)
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    handleExport(type) {
      let msg = ''
      type == '下载模板' ? (msg = '是否确认下载该模板') : type == '生成' ? (msg = '将根据历年平均飞行时长的消耗品数量与维修品数量，预测今年计划飞行时长所需要的价格清单') : (msg = `是否${type}该数据`)
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss">
.view-priceList-left {
  width: 300px;
}
.view-priceList-right {
  width: calc(100% - 330px);
  margin-left: 30px;
}
.view-priceList {
  display: flex;
}
</style>
