<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading" @selection-change="handleSelectionChange">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" v-if="checkPermission(['goodsTransferConfirm:add'])" type="primary"
            @click="handleAddGoodsTransfer">新增</el-button>
          <el-button size="mini" v-if="checkPermission(['goodsTransferConfirm:confirm'])" @click="handleSuccessGoodsTransfer()"
            type="success" :disabled="multipleSelection.length === 0">确认</el-button>
          <el-button size="mini" v-if="checkPermission(['goodsTransferConfirm:del'])" @click="handleRejectGoodsTransfer()"
            type="danger" :disabled="multipleSelection.length === 0">退回</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" @advancedSearch="advancedSearchEvent" placeholder="请输调拨单号" advanced
            advancedWidth="465">
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="90px">
                <el-form-item label="调出仓库">
                  <el-select v-model="advancedForm.out_warehouse_id">
                    <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="调入仓库">
                  <el-select v-model="advancedForm.in_warehouse_id">
                    <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="调拨类型" prop="type">
                  <el-select v-model="advancedForm.type">
                    <el-option v-for="item in goodstransferType" :key="item.value" :label="item.label"
                      :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="状态">
                  <el-select v-model="advancedForm.status">
                    <el-option key="NotSubmit" label="未提交" value="NotSubmit" />
                    <el-option key="Submit" label="已提交" value="Submit" />
                    <el-option key="AuditPass" label="审核通过" value="AuditPass" />
                    <el-option key="AuditReject" label="审核驳回" value="AuditReject" />
                  </el-select>
                </el-form-item>
                <el-form-item label="时间">
                  <el-date-picker v-model="advancedForm.purchasePlan_time_range" type="daterange" align="center"
                    :editable="false" unlink-panels :clearable="true" :default-time="['00:00:00', '23:59:59']"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="timestamp"
                    :picker-options="{ disabledDate(time) { return time.getTime() - 1 >= new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime() + 86400000 - 1 }, shortcuts: MixinPickerShortcuts }">
                  </el-date-picker>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sn" label="调拨单编号" />
        <el-table-column label="调拨日期">
          <template slot-scope="scope">{{ scope.row.transfer_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column prop="out_dept_name" label="调出部门" />
        <el-table-column prop="out_warehouse_name" label="调出仓库" />
        <el-table-column prop="in_dept_name" label="调入部门" />
        <el-table-column prop="in_warehouse_name" label="调入仓库" />
        <el-table-column prop="type" label="调拨类型">
          <template slot-scope="scope">{{ typeFilter(scope.row.type) }}</template>
        </el-table-column>
        <el-table-column prop="out_handled_by" label="调出人" />
        <el-table-column prop="in_handled_by" label="调入人" />
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
  </div>
</template>

<script>
import * as API_GoodsTransfer from '@/api/goodsTransfer'
import * as API_Setting from '@/api/setting'
import * as API_BasicSetting from '@/api/basicSetting'
export default {
  name: 'goodsTransferConfirm',
  data() {
    return {
      warehouseList: [],
      goodstransferType: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      // 当前已选择的行
      multipleSelection: [],
      GoodsTransferForm: {}
    }
  },
  mounted() {
    this.GET_GoodsTransferList()
    this.GET_DictDataInfo()
    this.GET_WarehouseList()
  },
  watch: {
    $route: function({ query }) {
      this.params.page_no = 1
      this.GET_GoodsTransferList()
    }
  },
  methods: {
    /** 获取仓库 */
    GET_WarehouseList() {
      API_BasicSetting.getWarehouseListAll().then(response => {
        this.warehouseList = response
      })
    },
    typeFilter(row) {
      return this.goodstransferType.find(type => type.value === row).label
    },
    GET_DictDataInfo() {
      API_Setting.getDictDataInfo('erp_goodstransfer_type').then(res => {
        this.goodstransferType = res
      })
    },
    /** 调拨单状态 */
    statusFilter(val) {
      switch (val) {
        case 'NotSubmit':
          return '未提交'
        case 'Submit':
          return '已提交'
        case 'AuditPass':
          return '审核通过'
        case 'AuditReject ':
          return '审核驳回'
        default:
          return ''
      }
    },
    handleRejectGoodsTransfer() {
      if (!this.multipleSelection || !this.multipleSelection.length) return this.$message.error('请先选择！')
      this.$confirm('确定要退回这些商品调拨吗？', '提示', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(item => item.id)
        API_GoodsTransfer.rejectGoodsTransfer(ids).then(() => {
          this.$message.success('删除成功！')
          this.GET_GoodsTransferList()
        })
      }).catch(() => { })
    },
    handleSuccessGoodsTransfer() {
      if (!this.multipleSelection || !this.multipleSelection.length) return this.$message.error('请先选择！')
      this.$confirm('确定要确认这些商品调拨吗？', '提示', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(item => item.id)
        API_GoodsTransfer.successGoodsTransfer(ids).then(() => {
          this.$message.success('删除成功！')
          this.GET_GoodsTransferList()
        })
      }).catch(() => { })
    },
    /** 多选改变 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_GoodsTransferList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_GoodsTransferList()
    },

    /** 添加商品调拨 */
    handleAddGoodsTransfer() {
      this.$router.push({
        name: 'goodsTransferAdd',
        params: { callback: this.GET_GoodsTransferList }
      })
    },
    /** 编辑商品调拨 */
    handleEditGoodsTransfer(row) {
      this.$router.push({
        name: 'goodsTransferEdit',
        params: { id: row.id }
      })
    },
    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        sn: data
      }
      Object.keys(this.advancedForm).forEach((key) => delete this.params[key])
      this.params.page_no = 1
      this.GET_GoodsTransferList()
    },
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      delete this.params.start_time
      delete this.params.end_time
      if (this.advancedForm.purchasePlan_time_range) {
        this.params.start_time = parseInt(Number(this.advancedForm.purchasePlan_time_range[0]) / 1000)
        this.params.end_time = parseInt(Number(this.advancedForm.purchasePlan_time_range[1]) / 1000)
      }
      delete this.params.purchasePlan_time_range
      this.params.page_no = 1
      this.GET_GoodsTransferList()
    },
    /** 获取商品调拨列表 */
    GET_GoodsTransferList() {
      this.loading = true
      API_GoodsTransfer.getGoodsTransferConfirmList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.tips-t {
  display: flex;
  padding: 10px 0;

  div {
    flex: 1;
    text-align: center;
  }
}

.tips-f {
  padding: 20px 0;
}

.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
