<template>
  <div class="view-dataSailingMaterialPrice">
    <div class="view-dataSailingMaterialPrice-left">
      <MenuTree node-key="avmatCategoriesCode" :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-dataSailingMaterialPrice-right">
      <div class="common-box">
        <div class="common-box-button">
          <!-- <el-button type="primary" @click="handleSubmit('导入')">导入</el-button>
          <el-button type="primary" @click="handleExport('导出')" :disabled="tableData.length == 0">导出</el-button> -->
          <el-button type="primary" @click="handleSubmit('保存')">保存</el-button>

          <el-dropdown split-button type="primary" style="margin-left: 10px" trigger="click" @command="handleExport">
            更多
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="importOperation">导入</el-dropdown-item>
              <el-dropdown-item command="allExport" :disabled="tableData.length == 0">导出</el-dropdown-item>
              <el-dropdown-item command="downloadTemplate">下载数据模板</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="common-box-select">
          <label>维修价格 : </label>
          <el-select v-model="repairPriceSearch" placeholder="请选择" @change="fetchTableData">
            <el-option label="全部" :value="null"></el-option>
            <el-option v-for="item in priceOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
          <label>采购价格 : </label>
          <el-select v-model="purchasePriceSearch" placeholder="请选择" @change="fetchTableData">
            <el-option label="全部" :value="null"></el-option>
            <el-option v-for="item in priceOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </div>
        <div class="common-box-search">
          <el-input placeholder="航材编码/航材名称" v-model="keyword" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="fetchTableData"></el-button>
          </el-input>
        </div>
      </div>
      <LTable ref="table" @selection-change="selection" :defaultFetch="false" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="tableRequset"></LTable>
      <CrudDialog v-bind="modalConfig" ref="NewDialogRef" @crudDialogSubmit="crudDialogSubmit"></CrudDialog>
      <!-- 导入 -->
      <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
    </div>
  </div>
</template>

<script>
import { page, edit, exportExcel, downLoadFile } from '@/api/basicData/dataSailingMaterialPrice'
import { getAvmatCategoriesTree } from '@/api/basicData/dataSpecies'
import { defaultProps, columns, options, modalConfig } from './index'
import { isNums } from '@/utils/verifyType'
import ImportDialog from './importDialog/index.vue'
export default {
  name: 'dataSailingMaterialPrice',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      currentNodeKey: '',
      menuTreeData: [],
      treeObj: {
        title: '所属分类',
        activityheight: '275px',
        searchIcon: false,
        configure: defaultProps
      },
      options,
      columns: columns(this),
      tableData: [],
      modalConfig: modalConfig(this),
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 20
      },
      removeArr: [],
      keyword: '',
      repairPriceSearch: null,
      purchasePriceSearch: null,
      priceOptions: [
        {
          label: '已维护',
          value: 0
        },
        {
          label: '未维护',
          value: 1
        }
      ]
    }
  },
  mounted() {
    this.getTreeData()
  },
  methods: {
    async getTreeData() {
      const { data } = await getAvmatCategoriesTree()
      this.menuTreeData = [
        {
          avmatCategoriesId: '',
          avmatCategoriesCode: '',
          avmatCategoriesName: '全部',
          children: data
        }
      ]
      this.currentNodeKey = ''
      this.fetchTableData()
    },

    priceChange() {},

    async fetchTableData() {
      const { pageSize, pageIndex } = this.tableRequset
      const { keyword, repairPriceSearch, purchasePriceSearch } = this
      const {
        data: { list, totalCount }
      } = await page({ avmatCategoriesId: this.currentNodeKey, pageSize, pageIndex, keyword, repairPriceSearch, purchasePriceSearch })

      this.tableData = list
      this.tableRequset.total = totalCount
    },

    treeNodeClick(node) {
      this.currentNodeKey = node.avmatCategoriesId
      this.fetchTableData()
    },

    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item.id)
      })
    },

    async updateRow(row) {
      const { repairPrice, purchasePrice, avmatPriceId, avmatCatalogCode, avmatCatalogId } = row
      const res = isNums([repairPrice, purchasePrice])
      if (!res.every((e) => e)) {
        this.fetchTableData()
        this.$message({
          type: 'warning',
          message: '只能输入数字'
        })
        return
      }
      try {
        const { success } = await edit({
          avmatCatalogId,
          repairPrice,
          purchasePrice,
          avmatPriceId,
          avmatCatalogCode
        })
        if (success) {
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
        }
      } catch (error) {}
      this.fetchTableData()
    },

    handleSubmit(type) {
      this.$confirm(`是否确认${type}数据`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '保存成功!'
          })
        })
        .catch(() => {})
    },
    //更多按钮
    handleExport(type) {
      this[type]()
    },
    //导入
    importOperation() {
      this.$refs.ImportDialog.triggerDialog()
    },
    //导入回写
    ImportDialogCallBack() {
      this.fetchTableData()
    },

    //导出全部
    async allExport() {
      const params = {
        avmatCategoriesId: this.currentNodeKey
      }
      await exportExcel(params)
    },
    //下载模板
    async downloadTemplate() {
      await downLoadFile()
    },

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

    crudDialogSubmit(data) {
      const { dialogData, type } = data
      if (type == 'update') {
        this.tableData.forEach((item) => {
          if (item.id === dialogData.id) {
            for (let key in item) {
              if (dialogData.hasOwnProperty(key)) {
                item[key] = dialogData[key]
              }
            }
          }
        })
      } else {
        this.tableData.push(dialogData)
      }
      this.$message({
        type: 'success',
        message: '操作成功!'
      })
    }
  }
}
</script>

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