<template>
  <template-page
    ref="pageRef"
    :get-list="getList"
    :operation="operation()"
    :options-evens-group="optionsEvensGroup"
    :export-list="exportList"
    :column-parsing="columnParsing"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
  >
    <Popu v-if="isShowForm">
      <PickupForm :list-item="queryItem" :flag="flag" @backListFormDetail="backList" />
    </Popu>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import Popu from '@/components/template/popu.vue'

import { getPickupList, cancelData,cancelDeleteDa, getPickupManList, pickListV2, pickListV2Export } from '@/api/supply/pickup'
import { getWarehouseList } from '@/api/supply/engin'
import PickupForm from '@/views/supply/pickup/components/pickup_form'

export default {
  components: {
    PickupForm,
    TemplatePage,
    Popu
  },
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: () => {
                this.toForm()
              },
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: {
        // 筛选表单数据
        orderNum: '',
        jxsName: '',
        jxsNum: '',
        correspondId: '',
        date: [],
        status: '',
        takerPhone: '',
        takerId: '',
        printTime: [],
        salesOrderId: '',
        createBy: ''
      },
      pickupManList: '',
      flag: false,
      isCollapse: true,
      queryItem: {},
      isShowForm: false,
      warehouseList: []
    }
  },

  computed: {
    isDealer() {
      return JSON.parse(localStorage.getItem('supply_user')).isCustomer
    },
    exParams() {
      return {
        invoiceId: this.screenForm.orderNum,
        customerName: this.screenForm.jxsName,
        customerNumber: this.screenForm.jxsNum,
        correspondId: this.screenForm.correspondId,
        startTime: this.screenForm.date ? this.screenForm.date[0] : '',
        endTime: this.screenForm.date ? this.screenForm.date[1] : '',
        status: this.screenForm.status,
        takerPhone: this.screenForm.takerPhone,
        createBy: this.screenForm.createBy,
        takerId: this.screenForm.takerId,
        printStartTime: this.screenForm.printTime ? this.screenForm.printTime[0] : '',
        printEndTime: this.screenForm.printTime ? this.screenForm.printTime[1] : '',
        salesOrderId: this.screenForm.salesOrderId
      }
    }
  },

  created() {
    // this.getList()
    // this.getWarehouseList()
    // this.getPickupManList()
  },
  activated() {
    if (this.$route.query && this.$route.query.id) {
      this.toForm({ id: this.$route.query.id }, true)
    }
  },
  methods: {
    // 列表请求函数
    getList: pickListV2,
    // 列表导出函数
    exportList: pickListV2Export,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class='operation-btns'>
            <el-button
              size='mini'
              type='text'
              onClick={async() => {
                this.toForm(row, true)
              }}
            >
              详情
            </el-button>

            {this.$checkBtnRole('edit', this.$route.meta.roles) && !row.printNum ? (
              <el-button
                size='mini'
                type='text'
                onClick={async() => {
                  this.toForm(row)
                }}
              >
                编辑
              </el-button>
            ) : (
              ''
            )}

            {this.$checkBtnRole('cancelDelete', this.$route.meta.roles) &&
            ((this.isDealer && !row.printNum) || !this.isDealer) ? (
                <el-popconfirm
                  onConfirm={async() => {
                    this.handleCancel(row.id, cancelDeleteDa)
                  }}
                  title={`是否确定需要作废该项内容?`}
                >
                  <el-button slot='reference' size='mini' type='text'>
                    作废
                  </el-button>
                </el-popconfirm>
              ) : (
                ''
              )}
              {this.$checkBtnRole('cancel', this.$route.meta.roles) &&
            ((this.isDealer && !row.printNum) || !this.isDealer) ? (
                <el-popconfirm
                  onConfirm={async() => {
                    this.handleCancel(row.id, cancelData)
                  }}
                  title={`是否确定需要取消该项内容?`}
                >
                  <el-button slot='reference' size='mini' type='text'>
                    取消
                  </el-button>
                </el-popconfirm>
              ) : (
                ''
              )}
          </div>
        )
      }
    },

    // 获取仓库列表
    getWarehouseList() {
      getWarehouseList({
        pageNum: 1,
        pageSize: -1
      }).then(res => {
        this.warehouseList = res.data.records
      })
    },

    // 查询按钮权限
    checkBtnRole(value) {
      // let btnRole = this.$route.meta.roles;
      // if(!btnRole) {return true}
      // let index = btnRole.indexOf(value);
      // return index >= 0;
      return true
    },
    getPickupManList() {
      getPickupManList({ pageNum: 1, pageSize: -1 }).then(res => {
        this.pickupManList = res.data.records
      })
    },
    // 查询列表
    // getList() {
    //   this.listLoading = true

    //   const params = {
    //     pageNum: this.currentPage,
    //     pageSize: this.pageSize,
    //     invoiceId: this.screenForm.orderNum,
    //     customerName: this.screenForm.jxsName,
    //     customerNumber: this.screenForm.jxsNum,
    //     correspondId: this.screenForm.correspondId,
    //     startTime: this.screenForm.date ? this.screenForm.date[0] : '',
    //     endTime: this.screenForm.date ? this.screenForm.date[1] : '',
    //     status: this.screenForm.status,
    //     takerPhone: this.screenForm.takerPhone,
    //     takerId: this.screenForm.takerId,
    //     createBy: this.screenForm.createBy,

    //     printStartTime: this.screenForm.printTime[0],
    //     printEndTime: this.screenForm.printTime[1],
    //     salesOrderId: this.screenForm.salesOrderId
    //   }
    //   getPickupList(params).then(res => {
    //     this.dataList = res.data.records
    //     this.listTotal = res.data.total
    //     this.listLoading = false
    //   })
    // },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 进入表单
    toForm(item, flag = false) {
      this.flag = flag // 详情 flag 为 details
      this.queryItem = item
      this.isShowForm = true
    },

    backList() {
      this.isShowForm = false
      if (this.$route.query && this.$route.query.id) {
        this.$router.push({name: 'exwarehouseSum_list'})
      } else {
        this.queryItem = {}
        this.$refs.pageRef.refreshList()
      }
    },

    // 取消/作废
    handleCancel(id, fn) {
      fn({ id }).then(res => {
        this.$successMsg()
        // this.getList()
        this.$refs.pageRef.refreshList()
      })
    }
  }
}
</script>

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