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

<script>
import {
  verifyPurchaseApplySheet,
  revokePurchaseApplySheet,
  getPurchaseApplySheet,
  getNewPurchaseApplySheetList,
  exportNewPurchaseApplySheetList
} from '@/api/material-system/vender/shop-purchase-apply'
import ShopPurchaseArea from '../components/ShopPurchaseArea'
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: 'ShopPurchase',
  components: { ShopPurchaseArea, TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      shopPurchaseArea: false,
      shopPurchaseAreaParam: {
        openType: 'add',
        purchaseId: ''
      },
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      screenForm: {
        // 筛选表单数据
        submitTime: '', // 创建时间
        flag: '', // 状态
        keyword: '', // 关键词
        purchaseId: '', // 单据编号
        shopId: ''
      },
      purchaseForm: {},
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
        // columnExpand: (h, { row, index, columm }) => {
        //   return (
        //     <div style="padding-left:95px">
        //       <el-form label-position="left" inline class="demo-table-expand">
        //         <el-form-item label="备注">
        //           <span>{row.notes2}</span>
        //         </el-form-item>
        //       </el-form>
        //     </div>
        //   )
        // }
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        // [
        //   [
        //     this.optionsEvensAuth('add', {
        //       click: () => {
        //         this.addOrEdit('add')
        //       }
        //     })
        //   ]
        // ]
      ]
    }
  },
  methods: {
    // 新增编辑 采购入库单
    addOrEdit(type, id) {
      this.shopPurchaseArea = true
      this.shopPurchaseAreaParam.openType = type
      this.shopPurchaseAreaParam.purchaseId = id
    },

    backUpdPage() {
      this.$refs.pageRef.refreshList()
      this.shopPurchaseArea = false
    },
    // 列表请求函数
    getList: getNewPurchaseApplySheetList,
    // 列表导出函数
    exportList: exportNewPurchaseApplySheetList,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // 操作事件
    handleCommand(param) {
      if (param[1] === 'view' || param[1] === 'edit') {
        this.addOrEdit(param[1], param[0])
      } else {
        this[param[1] + 'Sheet'](param[0])
      }
    },
    // 撤消单据
    revokeSheet(purchaseId) {
      this.$confirm(`此操作将撤消 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          revokePurchaseApplySheet({ purchaseId: purchaseId }).then(() => {
            this.$refs.pageRef.refreshList()

            this.$successMsg('撤消成功')
          })
        })
        .catch(() => console.log('取消'))
    },
    // 审核单据
    verifySheet(purchaseId) {
      this.$confirm(`此操作将审核 ${purchaseId} 单据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          verifyPurchaseApplySheet({ purchaseId: purchaseId }).then(() => {
            this.$refs.pageRef.refreshList()

            this.$successMsg('审核成功')
          })
        })
        .catch(() => console.log('取消'))
    },
    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            this.handleCommand([row.purchaseId, 'view'])
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.handleCommand([row.purchaseId, 'edit'])
          }
        },
        verify: {
          prompt: '确定核实吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.handleCommand([row.purchaseId, 'verify'])
          }
        },
        revoke: {
          prompt: '确定撤消吗？',
          conditions: ({ row, index, column }) => {
            return row.flag === 'SUBMIT' && row.isRecheck === 'NO'
          },
          click: ({ row, index, column }) => {
            this.handleCommand([row.purchaseId, 'revoke'])
          }
        }
      })
    }
  }
}
</script>

<style scoped></style>
