<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :optionsEvensGroup="optionsEvensGroup"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
  >
  </template-page>
</template>

<script>
import import_mixin from '@/components/template/import_mixin.js'
import TemplatePage from '@/components/template/template-page-1.vue'
import {
  getBusPriceList,
  v2BusPriceList,
  v2BusPriceListExport,
  v2BusPriceListExcel,
  v2BusPriceListImport
} from '@/api/supply/commercial_list'
// import { downloadFiles, handleImport } from '@/utils/util'
export default {
  mixins: [import_mixin],
  components: {
    TemplatePage
  },
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '',
              render: this.importButton(v2BusPriceListImport)
            }
          ]
        ],
        [
          [
            {
              name: '导入模版',
              click: () => {
                v2BusPriceListExcel({}, `${this.$route.meta.title}`)
                  .then(res => {
                    this.$message({
                      message: '下载成功',
                      type: 'success'
                    })
                  })
                  .catch(err => {
                    this.$message.error('下载失败')
                  })
              }
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      listLoading: false, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      importFileList: [],
      isCollapse: true,
      searchForm: {
        categoryId: '',
        materialName: '',
        materialNumber: '',
        saleTypeId: '',
        specification: ''
      }
    }
  },
  computed: {
    exParams() {
      return {}
    }
  },
  created() {
    // this.getDataList()
  },
  methods: {
    // 列表请求函数
    getList: v2BusPriceList,
    // 列表导出函数
    exportList: v2BusPriceListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // operation() {
    //   return (h, { row, index, column }) => {
    //     return (
    //       <div class="operation-btns">
    //         <el-button
    //           size="mini"
    //           type="text"
    //           onClick={async () => {
    //             this.seeFn(row)
    //           }}
    //         >
    //           查看
    //         </el-button>

    //         {this.$checkBtnRole('del', this.$route.meta.roles) ? (
    //           <el-popconfirm
    //             onConfirm={async () => {
    //               this.deleFn(row.id)
    //             }}
    //             title="是否确定需要删除该项内容?"
    //           >
    //             <el-button slot="reference" size="mini" type="text">
    //               删除
    //             </el-button>
    //           </el-popconfirm>
    //         ) : (
    //           ''
    //         )}

    //         {row.flag === 1 ? (
    //           <el-button
    //             size="mini"
    //             type="text"
    //             onClick={async () => {
    //               this.stopFn(row.id)
    //             }}
    //           >
    //             停用
    //           </el-button>
    //         ) : (
    //           ''
    //         )}
    //         {row.flag === 2 ? (
    //           <el-button
    //             size="mini"
    //             type="text"
    //             onClick={async () => {
    //               this.rebuild(row)
    //             }}
    //           >
    //             重建
    //           </el-button>
    //         ) : (
    //           ''
    //         )}
    //       </div>
    //     )
    //   }
    // },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.getDataList()
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    //清除
    clearFn() {
      this.$refs.searchForm.resetFields()
    },
    //搜索
    searchFormFn() {
      this.getDataList()
    },
    //导入
    async handleImport(param) {
      const file = param.file
      const formData = new FormData()
      formData.append('file', file)
      let result = await handleImport('bus/price/import', formData)
      //   console.log(result);
      this.importFileList = []
      if (result.code == 200) {
        this.$message.success('导入成功')
      } else {
        this.$message.error(result.message)
      }
    },
    //下载模板
    downLoadFn() {
      downloadFiles('bus/price/download')
    },
    //获取列表数据
    async getDataList() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        categoryId: this.searchForm.categoryId,
        materialName: this.searchForm.materialName,
        materialNumber: this.searchForm.materialNumber,
        saleTypeId: this.searchForm.saleTypeId,
        specification: this.searchForm.specification
      }
      let res = await getBusPriceList(params)
      this.dataList = res.data.records
      this.listTotal = res.data.total
    }
  }
}
</script>

<style lang="scss" scoped>
.import-btn {
  display: inline-block;
  margin-left: 10px;
}
</style>
