<template>
  <div style="height: 100%">
    <div v-show="!purchaseApplyArea" 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="purchaseApplyArea">
      <div style="box-sizing: border-box; padding: 20px">
        <el-page-header
          :content="
            purchaseApplyAreaParam.openType === 'add'
              ? '新增采购申请单'
              : purchaseApplyAreaParam.openType === 'view'
              ? '查看采购申请单'
              : '编辑采购申请单'
          "
          @back="backUpdPage"
        />
        <el-divider />
        <!-- 新增编辑单据 -->
        <purchase-apply-area
          :is-open.sync="purchaseApplyArea"
          :input-param="purchaseApplyAreaParam"
          @getList="$refs.pageRef.refreshList()"
        />
      </div>
    </template>
  </div>
</template>

<script>
import {
  submitPurchaseApplySheet,
  revokePurchaseApplySheet,
  delPurchaseApplySheet,
  getNewPurchaseApplySheetList,
  exportNewPurchaseApplySheetList
} from '@/api/material-system/shop/purchase-apply-sheet'
import PurchaseApplyArea from '../components/PurchaseApplyArea'
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: 'PurchaseApplySheet',
  components: { PurchaseApplyArea, TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      purchaseApplyArea: false,
      purchaseApplyAreaParam: {
        openType: 'add',
        purchaseId: ''
      },
      listLoading: false, // 列表加载loading
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.addOrEdit('add')
              }
            })
          ]
        ]
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList: getNewPurchaseApplySheetList,

    // 列表导出函数
    exportList: exportNewPurchaseApplySheetList,

    // 表格列解析渲染数据更改
    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 === 'SAVE' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.addOrEdit('edit', row.purchaseId)
          }
        },
        submit: {
          conditions: ({ row, index, column }) => {
            return row.flag === 'SAVE' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.submitSheet(row.purchaseId)
          }
        },
        del: {
          prompt: '确定删除吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SAVE' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.delSheet(row.purchaseId)
          }
        },
        revoke: {
          prompt: '确定撤消吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.revokeSheet(row.purchaseId)
          }
        }
      })
    },
    // 新增编辑 采购入库单
    addOrEdit(type, id) {
      this.purchaseApplyArea = true
      this.purchaseApplyAreaParam.openType = type
      this.purchaseApplyAreaParam.purchaseId = id
    },
    // 撤消单据
    revokeSheet(purchaseId) {
      this.$confirm(`此操作将撤消 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          revokePurchaseApplySheet({ purchaseId: purchaseId }).then(() => {
            this.$refs.pageRef.refreshList()
            this.$successMsg('撤消成功')
          })
        })
        .catch(() => console.log('取消'))
    },
    // 删除单据
    delSheet(purchaseId) {
      this.$confirm(`此操作将删除 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delPurchaseApplySheet([purchaseId]).then(() => {
            this.$refs.pageRef.refreshList()
            this.$successMsg('删除成功')
          })
        })
        .catch(() => console.log('取消'))
    },
    // 提交 采购入库单
    submitSheet(purchaseId) {
      this.$confirm(`此操作将提交 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          submitPurchaseApplySheet({ 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.purchaseApplyArea = false
    }
  }
}
</script>

<style scoped></style>
