<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operationColumnWidth="200"
    :operation="operation()"
  >
    <website-parts-shop-sales-information
      v-if="showFromBool"
      v-bind="fromPm"
      @success="$refs.pageRef.refreshList"
      @goBack="showFromBool = false"
    />
    <pay-popup-window
      v-if="visible"
      :item="rowData"
      :partsIsCash="partsIsCash"
      :visible="visible"
      @success="
        () => {
          $refs.pageRef.refreshList()
        }
      "
      @setVisible="
        bool => {
          visible = bool
          rowData = null
          partsIsCash = true
        }
      "
    />
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import WebsitePartsShopSalesInformation from './components/website-parts-shop-sales-information.vue'
import PayPopupWindow from './components/pay-popup-window.vue'
import {
  partsPartssalesorderSalesOrderList,
  partsPartssalesorderSalesOrderExport,
  partsPartssalesorderDeleteById,
  partsPartssalesorderSalesOrderShopList,
  partsPartssalesorderSalesOrderShopExport
} from '@/api/material-system/website/website-parts-shop-sales'
import operation_mixin from '@/components/template/operation_mixin.js'
import { getDepartmentDetail } from '@/api/setting'
export default {
  components: {
    TemplatePage,
    WebsitePartsShopSalesInformation,
    PayPopupWindow
  },
  mixins: [operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {},
      // 表格事件
      tableEvents: {},
      showFromBool: false,
      fromPm: {},
      visible: false,
      rowData: null,
      partsIsCash: true
    }
  },
  computed: {
    greemall_user() {
      return JSON.parse(localStorage.getItem('greemall_user'))
    },
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', role => {
              return {
                isRole: role?.moduleName && this?.greemall_user?.type == 0,
                name: role.moduleName,
                click: this.add
              }
            })
          ]
        ]
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList(p) {
      return this?.greemall_user?.type == 0
        ? partsPartssalesorderSalesOrderShopList(p)
        : partsPartssalesorderSalesOrderList(p)
    },
    // 列表导出函数
    exportList(p) {
      return this?.greemall_user?.type == 0
        ? partsPartssalesorderSalesOrderShopExport(p)
        : partsPartssalesorderSalesOrderExport(p)
    },
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    add() {
      this.fromPm = {
        type: 0
      }
      this.showFromBool = true
    },
    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            this.fromPm = {
              type: 2,
              item: { ...row }
            }
            this.showFromBool = true
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return this?.greemall_user?.type == 0 && ~['SAVE', 'SUBMIT'].indexOf(row.status)
          },
          click: ({ row, index, column }) => {
            this.fromPm = {
              type: 1,
              item: { ...row }
            }
            this.showFromBool = true
          }
        },
        del: {
          prompt: '确定删除吗？',
          conditions: ({ row, index, column }) => {
            return this?.greemall_user?.type == 0 && ~['SAVE', 'CANCEL'].indexOf(row.status)
          },
          click: ({ row, index, column }) => {
            partsPartssalesorderDeleteById({ id: row.id })
              .then(res => {
                this.$refs.pageRef.refreshList()
                this.$message({
                  type: 'success',
                  message: `删除成功!`
                })
              })
              .catch(err => {
                console.log(err)
              })
          }
        },
        examine: {
          name: ({ row, index, column }) => {
            return row.status == 'SUBMIT'
              ? '支付'
              : row.status == 'EXCEPTION'
              ? '编辑'
              : row.status == 'PAYED' && ~[0].indexOf(row.deliveryConfirmFlag)
              ? '提货'
              : ''
          },
          conditions: ({ row, index, column }) => {
            return (
              this?.greemall_user?.type == 0 &&
              (row.status == 'SUBMIT' ||
                row.status == 'EXCEPTION' ||
                (row.status == 'PAYED' && ~[0].indexOf(row.deliveryConfirmFlag)))
            )
          },
          click: ({ row, index, column }) => {
            if (row.status == 'SUBMIT') {
              getDepartmentDetail({ id: row.websitId }).then(res => {
                console.log(res.data)
                this.partsIsCash = res.data.partsIsCash
                this.rowData = row
                this.visible = true
              })
            } else if (row.status == 'EXCEPTION') {
              this.fromPm = {
                type: 3,
                item: { ...row }
              }
              this.showFromBool = true
            } else if (row.status == 'PAYED' && ~[0].indexOf(row.deliveryConfirmFlag)) {
              this.fromPm = {
                type: 3,
                item: { ...row }
              }
              this.showFromBool = true
            }
          }
        }
      })
    }
  }
}
</script>

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