<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
  >
    <center-parts-shop-in-stock-from
      v-if="showFromBool"
      @goBack="showFromBool = false"
      @success="$refs.pageRef.refreshList"
    />
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import CenterPartsShopInStockFrom from '../components/center-parts-shop-in-stock-from.vue'
import {
  partsNewInList,
  partsNewInExport,
  partsNewInImport,
  partsNewInUpdate,
  partsNewInDel
} from '@/api/material-system/center/center-parts-shop-in-stock'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage, CenterPartsShopInStockFrom },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        selectColumn: true,
        selectable: this.selectable
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      showFromBool: false
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('partsStorage', {
              click: this.update
            })
          ]
        ],
        [
          [
            this.optionsEvensAuth('add', {
              click: this.add
            })
          ]
        ],
        [
          [
            this.optionsEvensAuth('dels', {
              click: this.del
            })
          ]
        ],
        [
          [
            this.optionsEvensAuth('import', ({ moduleName }) => {
              return {
                name: moduleName,
                render: () => {
                  return this.importButton(partsNewInImport, moduleName)
                }
              }
            })
          ]
        ]
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return partsNewInList(...p)
    },
    // 列表导出函数
    exportList: partsNewInExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 获取勾选框数据
    selectionChange(data) {
      this.recordSelected = data
    },
    selectable(row, index) {
      var status = Object.entries(row?.selectMapData?.status).find(([key, val]) => val == row.status)?.[0]
      return !['CHECKED'].includes(status)
    },

    add() {
      this.showFromBool = true
    },
    update() {
      if (this.recordSelected.length) {
        this.$confirm('请确认配件是否已收货?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            partsNewInUpdate({
              ids: this.recordSelected.map(item => item.id).join(',')
            })
              .then(res => {
                this.$refs.pageRef.refreshList()
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
              })
              .catch(() => {
                this.$message({
                  type: 'error',
                  message: '操作失败!'
                })
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
      } else {
        this.$message({
          type: 'info',
          message: '请先勾选需要操作的数据!'
        })
      }
    },
    del() {
      if (this.recordSelected.length) {
        this.$confirm('此操作将删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            partsNewInDel({
              ids: this.recordSelected.map(item => item.id).join(',')
            })
              .then(res => {
                this.$refs.pageRef.refreshList()
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
              })
              .catch(() => {
                this.$message({
                  type: 'error',
                  message: '删除失败'
                })
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.$message({
          type: 'info',
          message: '请先勾选需要删除的数据!'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
