<template>
  <div style="height: 100%">
    <div v-show="!purchaseAdjustArea" style="height: 100%">
      <template-page
        ref="pageRef"
        :loading="listLoading"
        :getList="getList"
        :exportList="exportList"
        :columnParsing="columnParsing"
        :operation="operation()"
        :optionsEvensGroup="optionsEvensGroup"
        :tableAttributes="tableAttributes"
        :tableEvents="tableEvents"
      >
      </template-page>
    </div>
    <template v-if="purchaseAdjustArea">
      <div style="box-sizing: border-box; padding: 20px">
        <el-page-header
          class="pdt"
          :content="
            purchaseAdjustAreaParam.openType === 'add'
              ? '新增采购调整单'
              : purchaseAdjustAreaParam.openType === 'view'
              ? '查看采购调整单'
              : '编辑采购调整单'
          "
          @back="backUpdPage"
        />
        <el-divider />
        <!-- 单据 -->
        <purchase-adjustment-area
          v-if="purchaseAdjustArea"
          :is-open.sync="purchaseAdjustArea"
          :input-param="purchaseAdjustAreaParam"
          @getList="$refs.pageRef.refreshList()"
        />
      </div>
    </template>
  </div>
</template>

<script>
import {
  purchaseAdjustmentSheetRevoke,
  purchaseAdjustmentSheetDel,
  purchaseAdjustmentSheetSubmit,
  purchaseAdjustmentSheetConfirm,
  getNewPurchaseAdjustmentSheetList,
  exportNewPurchaseAdjustmentSheetList
} from '@/api/material-system/shop/purchase-adjustment-sheet'
import PurchaseAdjustmentArea from '../components/PurchaseAdjustmentArea'
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  name: 'PurchaseAdjustmentSheet',
  components: { PurchaseAdjustmentArea, TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      purchaseAdjustArea: false,
      purchaseAdjustAreaParam: {
        openType: 'add',
        purchaseAdjustmentId: ''
      },
      listLoading: false, // 列表加载loading
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.addOrEdit('add')
              }
            })
          ]
        ]
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList: getNewPurchaseAdjustmentSheetList,
    // 列表导出函数
    exportList: exportNewPurchaseAdjustmentSheetList,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            this.addOrEdit('view', row.purchaseAdjustmentId)
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return row.flag === 'SAVE'
          },
          click: ({ row, index, column }) => {
            this.addOrEdit('edit', row.purchaseAdjustmentId)
          }
        },
        submit: {
          conditions: ({ row, index, column }) => {
            return row.flag === 'SAVE'
          },
          click: ({ row, index, column }) => {
            this.submitSheet(row.purchaseAdjustmentId)
          }
        },
        del: {
          prompt: '确定删除吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SAVE'
          },
          click: ({ row, index, column }) => {
            this.delSheet(row.purchaseAdjustmentId)
          }
        },
        revoke: {
          prompt: '确定撤消吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT'
          },
          click: ({ row, index, column }) => {
            this.revokeSheet(row.purchaseAdjustmentId)
          }
        },
        confirm: {
          prompt: '确定审核吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT'
          },
          click: ({ row, index, column }) => {
            this.confirmSheet(row.purchaseAdjustmentId)
          }
        }
      })
    },
    // 新增编辑 采购调整单
    addOrEdit(type, id) {
      this.purchaseAdjustArea = true
      this.purchaseAdjustAreaParam.openType = type
      this.purchaseAdjustAreaParam.purchaseAdjustmentId = id
    },
    // 提交采购调整单
    submitSheet(purchaseAdjustmentId) {
      this.$confirm(`此操作将提交 ${purchaseAdjustmentId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          purchaseAdjustmentSheetSubmit({ purchaseAdjustmentId: purchaseAdjustmentId }).then(
            () => {
              setTimeout(() => {
                this.$refs.pageRef.refreshList()
                this.$successMsg('提交成功')
                this.listLoading = false
              }, 2000)
            },
            () => {
              this.listLoading = false
            }
          )
        })
        .catch(() => console.log('取消'))
    },
    // 撤消采购调整单
    revokeSheet(purchaseAdjustmentId) {
      this.$confirm(`此操作将撤消 ${purchaseAdjustmentId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          purchaseAdjustmentSheetRevoke({ purchaseAdjustmentId: purchaseAdjustmentId }).then(
            () => {
              setTimeout(() => {
                this.$refs.pageRef.refreshList()
                this.$successMsg('撤消成功')
                this.listLoading = false
              }, 2000)
            },
            () => {
              this.listLoading = false
            }
          )
        })
        .catch(() => console.log('取消'))
    },
    // 删除采购调整单
    delSheet(purchaseAdjustmentId) {
      this.$confirm(`此操作将删除 ${purchaseAdjustmentId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          purchaseAdjustmentSheetDel({ purchaseAdjustmentId: purchaseAdjustmentId }).then(
            () => {
              setTimeout(() => {
                this.$refs.pageRef.refreshList()
                this.$successMsg('删除成功')
                this.listLoading = false
              }, 2000)
            },
            () => {
              this.listLoading = false
            }
          )
        })
        .catch(() => console.log('取消'))
    },
    // 审核采购调整单
    confirmSheet(purchaseAdjustmentId) {
      this.$confirm(`此操作将审核 ${purchaseAdjustmentId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          purchaseAdjustmentSheetConfirm({ purchaseAdjustmentId: purchaseAdjustmentId }).then(
            () => {
              setTimeout(() => {
                this.$refs.pageRef.refreshList()
                this.$successMsg('审核成功')
                this.listLoading = false
              }, 2000)
            },
            () => {
              this.listLoading = false
            }
          )
        })
        .catch(() => console.log('取消'))
    },
    backUpdPage() {
      this.$refs.pageRef.refreshList()
      this.purchaseAdjustArea = false
    }
  }
}
</script>

<style scoped></style>
