<template>
  <div>
    <div class="search">
      <el-form ref="advancedForm" :model="advancedForm" label-width="160px">
        <el-row>
          <el-col :span="8">
            <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-col>
          <el-col :span="8">
            <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-col>
          <el-col :span="8">
            <el-form-item label="状态">
                  <el-select v-model="advancedForm.status">
                    <el-option v-for="item in statusList" :key="item.id" :label="item.label" :value="item.value" />
                   <!--  <el-option key="NotSubmit" label="未提交" value="NotSubmit" />
                    <el-option key="Submit" label="已提交" value="Submit" />
                    <el-option key="Confirm" label="已确认" value="Confirm" />
                    <el-option key="AuditReject" label="已退回" value="AuditReject" /> -->
                  </el-select>
                </el-form-item>
          </el-col>
          <el-col :span="8">
            <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-col>
          <el-col :span="8">
            <el-form-item label="调拨单号">
                <el-input v-model="advancedForm.sn" placeholder="请输入调拨单号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button @click="advancedSearchEvent">
        搜索
      </el-button>
    </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(['goodsUnderstock:add'])" type="primary"
            @click="handleAddGoodsUnderstock">新增</el-button>
         <!--  <el-button size="mini" v-if="checkPermission(['goodsTransfer:del'])" @click="handleDeleteGoodsTransfer()"
            type="danger" :disabled="multipleSelection.length === 0">删除</el-button> -->
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sn" label="调拨单编号">
          <template slot-scope="scope">
           <!--  <router-link
              v-if="checkPermission(['goodsTransfer'])"
              style="color: #409eff;"
              :to="{ name: 'goodsTransferDetail', params: { id: scope.row.id } }">
              {{ scope.row.sn }}
            </router-link>v-else -->
            <span >{{scope.row.sn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="调拨时间">
          <template slot-scope="scope">{{ scope.row.transfer_time }}</template>
        </el-table-column>
        <!-- <el-table-column prop="out_dept_name" label="调出部门" /> -->
        <el-table-column prop="out_warehouse_name" label="调出仓库">
          <template slot-scope="scope">
            {{warehouseName(scope.row.out_warehouse_id ) }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="in_dept_name" label="调入部门" /> -->
        <el-table-column prop="in_warehouse_name" label="调入仓库">
          <template slot-scope="scope">
            {{ warehouseName(scope.row.in_warehouse_id) }}
          </template>
        </el-table-column>

        <!-- <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_name" label="调出人" />
        <el-table-column prop="in_handled_by_name" label="会员" />-->
        <el-table-column prop="" label="配送方式">
          <template slot-scope="scope">
            {{ getTypename(scope.row.send_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">{{ get_typeName(scope.row.status) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
             <!-- <el-button size="mini" v-if="scope.row.status!=='ROLLBACK_HANDLE'"
              @click="handleSuccessGoodsTransfer(scope.row)" type="success">撤销</el-button> -->
             <el-button  size="mini"
              @click="handleSubmitGoodsTransfer(scope.row)" v-if="checkPermission(['goodsUnderstockDetail'])" >查看</el-button>
          <!--  <el-button type="danger" size="mini"v-if="scope.row.allowable.submit && checkPermission(['goodsTransfer:submit'])"
              v-else-if="scope.row.allowable.withdraw && checkPermission(['goodsTransfer:withdraw'])"
              @click="handleWithdrawGoodsTransfer(scope.row)">撤回</el-button>
            <el-button size="mini" v-if="scope.row.allowable.edit && checkPermission(['goodsTransfer:edit'])"
              @click="handleEditGoodsTransfer(scope.row)">编辑</el-button>

            <el-button size="mini" v-if="scope.row.allowable.confirm && checkPermission(['goodsTransfer:return'])"
              @click="handleRejectGoodsTransfer(scope.row)" type="danger">退回</el-button>-->
          </template>
        </el-table-column>
      </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_Statistics from '@/api/statistics'
import * as API_Setting from '@/api/setting'
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_Express from "@/api/express";
import { mapGetters } from 'vuex'
export default {
  name: 'GoodsTransfer',
  data() {
    return {
      warehouseList: [],
      goodstransferType: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      // 当前已选择的行
      multipleSelection: [],
      GoodsTransferForm: {},
      deliveryTypeList:[],//配送方式
      statusList:[]
    }
  },
  mounted() {
    this.GET_GoodsTransferList()
    this.GET_DictDataInfo()
    this.GET_WarehouseList()
    this.GET_ExpressList()
  },
  watch: {
    $route: function({ query }) {
      this.params.page_no = 1
      this.GET_GoodsTransferList()
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  methods: {
    get_typeName(val){
      let name=''
      this.statusList.map(el=>{
       if(el.value==val) {
        return name=el.label
       }
      })
      return name
    },
    getTypename(val){
      let name=''
      this.deliveryTypeList.map(el=>{
       if(el.id==val) {
        return name=el.name
       }
      })
      return name
    },
    /** 获取物流公司列表 */
    GET_ExpressList() {
      API_Express.getExpressList(this.params).then((response) => {
        this.deliveryTypeList = response.data;
      });
    },
    //获取仓库名称
    warehouseName(val){
      let name=''
      this.warehouseList.map(el=>{
       if(el.id==val) {
        return name=el.name
       }
      })
      return name
    },
    handleSuccessGoodsTransfer(row) {
      API_GoodsTransfer.get_withdraw(row.id).then(() => {
        this.$message.success('撤销成功！')
        this.GET_GoodsTransferList()
      })
    },
    handleRejectGoodsTransfer(row) {
      API_GoodsTransfer.rejectGoodsTransfer(row.id, { in_handled_by_id: this.user.uid, in_handled_by_name: this.user.username }).then(() => {
        this.$message.success('退回成功！')
        this.GET_GoodsTransferList()
      })
    },
    /** 获取仓库 */
    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
      })
      API_Setting.getPage({dictType:"transfer_status"}).then(res=>{
        this.statusList=res.data
      })
    },
    handleSubmitGoodsTransfer(row) {
      this.$router.push({
        name: 'goodsUnderstockDetail',
        params: { id: row.id }
      })
    },
    handleWithdrawGoodsTransfer(row) {
      API_GoodsTransfer.withdrawGoodsTransfer(row.id).then(() => {
        this.$message.success('撤回成功！')
        this.GET_GoodsTransferList()
      })
    },
    /** 调拨单状态 */
    statusFilter(val) {
      switch (val) {
        case 'NotSubmit':
          return '未提交'
        case 'Submit':
          return '已提交'
        case 'Confirm':
          return '已确认'
        case 'AuditReject ':
          return '已退回'
        default:
          return ''
      }
    },
    handleDeleteGoodsTransfer() {
      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.deleteGoodsTransfer(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()
    },

    /** 添加商品调拨 */
    handleAddGoodsUnderstock() {
      this.$router.push({
        name: 'goodsUnderstockAdd',
        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_Statistics.getstockTransferStatistics(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.search {
  display: flex;
  align-items: center;
  background: #fff;
  overflow: hidden;
  padding: 30px 10px 10px 10px;
  /deep/ .el-form-item {
    .el-form-item__content {
      .el-input {
        width: 200px;
      }
    }
  }
  .el-button{
    margin-top: -19px;
    margin-left: 20px;
  }
}
.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>
