<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <search-bar
      :formConfigs="formConfig"
      ref="searchBarForm"
      @headBtnClick="buttonClick"
    />
    <!-- 列表 -->
    <base-table
      :page="queryParams.pageNo"
      :limit="queryParams.pageSize"
      :table-props="tableProps"
      :table-data="list"
      :max-height="tableH"
    >
      <method-btn
        v-if="tableBtn.length"
        slot="handleBtn"
        :width="160"
        label="操作"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <!-- 新增 -->
    <base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="centervisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="50%"
    >
			<add-or-update ref="addOrUpdate" @successSubmit="successSubmit" />
		</base-dialog>
    <!-- 装车 -->
    <base-dialog
			dialogTitle="装车"
			:dialogVisible="centervisible2"
			@cancel="handleCancel2"
			@confirm="handleConfirm2"
			:before-close="handleCancel2"
			width="50%"
    >
			<loaded-page ref="loadedPage" @successSubmit="successSubmit2" />
		</base-dialog>
    <!-- 发货详情 -->
    <delivery-log-detail ref='deliveryLogDetail' v-if='showDetail' />
  </div>
</template>
<script>
import { parseTime } from '@/utils/ruoyi'
import { deliveryLogPage, deliveryLogDelete } from '@/api/base/delivery'
import AddOrUpdate from './components/addOrUpdate'
import LoadedPage from './components/loadedPage'
import DeliveryLogDetail from './components/deliveryLogDetail.vue'
const tableProps = [
  {
    prop: 'orderName',
    label: '订单名',
    minWidth: 100,
    showOverflowtooltip: true
  },
  {
    prop: 'name',
    label: '发货单名称',
    minWidth: 100,
    showOverflowtooltip: true
  },
  {
    prop: 'deliveryTime',
    label: '发货时间',
    filter: parseTime,
    minWidth: 160
  },
  {
    prop: 'code',
    label: '发货单号',
    showOverflowtooltip: true
  },
  {
    prop: 'deliverPerName',
    label: '发货负责人',
    minWidth: 100
  },
  {
    prop: 'principal',
    label: '运输负责人',
    minWidth: 100
  },
  {
    prop: 'principalCall',
    label: '运输联系方式',
    minWidth: 110,
    showOverflowtooltip: true
  },
  {
    prop: 'principalCost',
    label: '运输费用'
  },
  {
    prop: 'remark',
    label: '备注',
    showOverflowtooltip: true
  }
]
export default {
  name: "DeliveryLog",
  data() {
    return {
      formConfig: [
        {
          type: 'input',
          label: '订单名',
          param: 'orderName',
          defaultSelect: ''
        },
        {
          type: 'input',
          label: '发货单名称',
          param: 'name'
        },
        {
          type: 'input',
          label: '发货单号',
          param: 'code'
        },
        {
          type: 'button',
          btnName: '查询',
          name: 'search',
          color: 'primary'
        },
        {
          type: this.$auth.hasPermi('extend:delivery-log:create') ? 'separate' : '',
        },
        {
          type: this.$auth.hasPermi('extend:delivery-log:create') ? 'button' : '',
          btnName: '新增',
          name: 'add',
          color: 'success',
          plain: true
        }
      ],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        orderName: '',
        code: '',
        name: ''
      },
      tableProps,
      list: [],
      tableH: this.tableHeight(260),
      total: 0,
      tableBtn: [
      this.$auth.hasPermi('extend:delivery-log:loaded')
          ? {
              type: 'loaded',
              btnName: '装车'
            }
          : undefined,
      this.$auth.hasPermi('extend:delivery-log:detail')
          ? {
              type: 'detail',
              btnName: '详情'
            }
          : undefined,
        this.$auth.hasPermi('extend:delivery-log:update')
          ? {
              type: 'edit',
              btnName: '编辑'
            }
          : undefined,
        this.$auth.hasPermi('extend:delivery-log:delete')
          ? {
              type: 'delete',
              btnName: '删除'
            }
          : undefined
      ].filter((v) => v),
      addOrEditTitle: '',
      centervisible: false,
      centervisible2: false,
      showDetail: false
    }
  },
  created() {
    window.addEventListener('resize', () => {
      this.tableH = this.tableHeight(260)
    })
    if (location.href.indexOf('?') > 0) {
      let arr = location.href.split('?')[1]
      this.formConfig[0].defaultSelect = decodeURI(arr.split('=')[1])
    } else {
      this.formConfig[0].defaultSelect = ''
    }
    this.queryParams.orderName = this.formConfig[0].defaultSelect
    this.getList();
  },
  components: { AddOrUpdate, LoadedPage, DeliveryLogDetail },
  watch: {
    $route: 'initData'
  },
  methods: {
    initData(to) {
      if (to.name === 'DeliveryLog') {
        if (location.href.indexOf('?') > 0) {
          let arr = location.href.split('?')[1]
          this.formConfig[0].defaultSelect = decodeURI(arr.split('=')[1])
        } else {
          this.formConfig[0].defaultSelect = ''
        }
        this.queryParams.orderName = this.formConfig[0].defaultSelect
        this.getList()
      }
    },
    getList() {
      deliveryLogPage({...this.queryParams}).then(res => {
        this.list = res.data.list || []
        this.total = res.data.total || 0
      })
    },
    buttonClick(val) {
      if (val.btnName === 'search') {
        this.queryParams.name = val.name
        this.queryParams.orderName = val.orderName
        this.queryParams.code = val.code
        this.getList()
      } else {
        this.addOrEditTitle = '新增'
        this.centervisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      }
    },
    handleClick(val) {
      console.log(val)
      switch (val.type) {
        case 'edit':
          this.addOrEditTitle = '编辑'
          this.centervisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(val.data.id)
          })
          break
        case 'delete':
          this.handleDelete(val.data)
          break
        case 'loaded':
          this.centervisible2 = true
          this.$nextTick(() => {
            this.$refs.loadedPage.init(val.data.id, val.data.code, val.data.orderId)
          })
          break
        default:
          this.showDetail = true
          this.$nextTick(() => {
            this.$refs.deliveryLogDetail.init(val.data)
          })
      }
    },
    // 新增
    handleCancel() {
      this.$refs.addOrUpdate.formClear()
      this.centervisible = false
      this.addOrEditTitle = ''
    },
    handleConfirm() {
      this.$refs.addOrUpdate.submitForm()
    },
    successSubmit() {
      this.handleCancel()
      this.getList()
    },
    // 删除
    handleDelete(val) {
      this.$modal.confirm('是否确认删除发货单名为"' + val.name + '"的数据项?').then(function() {
          return deliveryLogDelete({ id: val.id })
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
    },
    // 装车
    handleCancel2() {
      this.$refs.loadedPage.formClear()
      this.centervisible2 = false
    },
    handleConfirm2() {
      this.$refs.loadedPage.submitForm()
    },
    successSubmit2() {
      this.handleCancel2()
      this.getList()
    }
  }
}
</script>