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

<script>
import {
  confirmPurchaseSheet,
  revokePurchaseSheet,
  getNewPurchaseSheetList,
  exportNewPurchaseSheetList
} from '@/api/material-system/shop/purchase-sheet'
import PurchaseArea from '../components/PurchaseArea'
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: 'PurchaseSheet',
  components: { PurchaseArea, TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      purchaseArea: false,
      purchaseAreaParam: {
        openType: 'add',
        purchaseId: ''
      },
      listLoading: false, // 列表加载loading
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  methods: {
    // 新增编辑 采购入库单
    addOrEdit(type, id) {
      this.purchaseArea = true
      this.purchaseAreaParam.openType = type
      this.purchaseAreaParam.purchaseId = id
    },
    // 删除单据
    revokeSheet(purchaseId) {
      this.$confirm(`此操作将作废 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          revokePurchaseSheet({ purchaseId: purchaseId }).then(() => {
            this.$refs.pageRef.refreshList()
            this.$successMsg('作废成功')
          })
        })
        .catch(() => console.log('取消'))
    },
    // 审核 采购入库单
    confirmSheet(purchaseId) {
      this.$confirm(`此操作将审核 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          confirmPurchaseSheet({ purchaseId: purchaseId }).then(
            () => {
              setTimeout(() => {
                this.$refs.pageRef.refreshList()
                this.$successMsg('审核成功')
                this.listLoading = false
              }, 2000)
            },
            () => {
              this.listLoading = false
            }
          )
        })
        .catch(() => console.log('取消'))
    },
    backUpdPage() {
      this.$refs.pageRef.refreshList()
      this.purchaseArea = false
    },
    // 列表请求函数
    getList: getNewPurchaseSheetList,
    // 列表导出函数
    exportList: exportNewPurchaseSheetList,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },

    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            this.addOrEdit('view', row.purchaseId)
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'YES'
          },
          click: ({ row, index, column }) => {
            this.addOrEdit('edit', row.purchaseId)
          }
        },
        confirm: {
          prompt: '确定审核吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'YES'
          },
          click: ({ row, index, column }) => {
            this.confirmSheet(row.purchaseId)
          }
        },
        revoke: {
          prompt: '确定撤消吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'YES'
          },
          click: ({ row, index, column }) => {
            this.revokeSheet(row.purchaseId)
          }
        }
      })
    }
  }
}
</script>

<style scoped></style>
