<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-date-picker v-model="advancedForm.goodsInventory_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-select v-model="advancedForm.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-input v-model="advancedForm.creator" placeholder="请输入内容" width="200px"></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(['goodsInventoryLoss:add'])" type="primary"
            @click="handleAddGoodsInventoryLoss">新增</el-button>
          <!-- <el-button v-if="checkPermission(['goodsInventoryLoss:del'])" size="mini" @click="handleDeleteGoodsInventoryLoss()"
            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(['goodsInventoryLoss'])"
              style="color: #409eff;"
              :to="{ name: 'goodsInventoryLossDetail', params: { id: scope.row.id } }">
              {{ scope.row.sn }}
            </router-link> -->
            <span>{{ scope.row.sn }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="report_time" label="报损日期">
          <template slot-scope="scope">{{ scope.row.report_time }}</template>
        </el-table-column> -->
        <el-table-column label="仓库">
          <template slot-scope="scope">{{ warehouseName(scope.row.warehouse_id) }}</template>
        </el-table-column>
        <!--<el-table-column prop="creator" label="报损人" />
        <el-table-column prop="damage_type_key" label="审核人" /> -->
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">{{ statusFilter(scope.row.status) }}</template>
        </el-table-column>
        <!-- <el-table-column prop="dept_name" label="报损数量" /> -->
        <el-table-column prop="report_desc" label="报损说明" />
        <el-table-column label="操作" width="450">
          <template slot-scope="scope">
            <el-button type="mini" size="mini" v-if="checkPermission(['goodsInventoryLossDetail'])" @click="handleDetailGoodsInventoryLoss(scope.row)">查看</el-button>
            <el-button type="mini" size="mini" v-if="scope.row.status == 'NOT_HANDLE' && checkPermission(['goodsInventoryLossEdit'])"
              @click="handleSubmitGoodsInventoryLoss(scope.row)">审核</el-button>
            <!--<el-button v-if="scope.row.allowable.allow_audit && checkPermission(['goodsInventoryLoss:audit'])" type="mini"
              size="mini" @click="handleAuditGoodsInventoryLoss(scope.row)">审核</el-button>

            <el-button v-if="scope.row.allowable.allow_cancel && checkPermission(['goodsInventoryLoss:cancel'])" type="mini"
              size="mini" @click="handleCancelGoodsInventoryLoss(scope.row)">撤回</el-button>
            <el-button v-if="scope.row.allowable.allow_edit && checkPermission(['goodsInventoryLoss:edit'])" size="mini"
              @click="handleEditGoodsInventoryLoss(scope.row)">编辑</el-button> -->
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-inventory="handlePageSizeChange"
        @current-inventory="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_GoodsInventoryLoss from '@/api/goodsInventoryLoss'
import Print from 'print-js'
import * as API_Setting from '@/api/setting'
import * as API_BasicSetting from '@/api/basicSetting'
import { Foundation } from '~/ui-utils'
export default {
  name: 'GoodsInventoryLoss',
  data() {
    return {
      warehouseList: [],
      deptList: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      // 当前已选择的行
      multipleSelection: [],
      GoodsInventoryLossForm: {},
      inventoryType:[]
    }
  },
  mounted() {
    this.GET_GoodsInventoryLossList()
    this.GET_DeptList()
    this.GET_WarehouseList()
  },
  watch: {
    $route: function ({ query }) {
      this.params.page_no = 1
      this.GET_GoodsInventoryLossList()
    }
  },
  methods: {
    // 查看
    handleDetailGoodsInventoryLoss(row) {
      this.$router.push({
        name: 'goodsInventoryLossDetail',
        params: { id: row.id }
      })
    },
    //获取仓库名称
    warehouseName(val) {
      let name = ''
      this.warehouseList.map(el => {
        if (el.id == val) {
          return name = el.name
        }
      })
      return name
    },
    /** 获取仓库 */
    GET_WarehouseList() {
      API_BasicSetting.getWarehouseListAll().then(response => {
        this.warehouseList = response
      })
      API_Setting.getPage({ dictType: "inventory_status" }).then((res) => {
        this.inventoryType = res.data;
      });
    },
    handleAuditGoodsInventoryLoss(row) {
      this.$router.push({
        name: 'goodsInventoryLossAudit',
        params: {
          id: row.id
        }
      })
    },
    handleEditGoodsInventoryLoss(row) {
      this.$router.push({
        name: 'goodsInventoryLossEdit',
        params: { id: row.id }
      })
    },
    /** 处理 */
    handleSubmitGoodsInventoryLoss(row) {
      /* API_GoodsInventoryLoss.getSyncStock(row.id).then(() => {
        this.$message.success('处理成功！')
        this.GET_GoodsInventoryLossList()
      }) */
      this.$router.push({
        name: 'goodsInventoryLossEdit',
        params: { id: row.id }
      })
    },
    /** 撤回 */
    handleCancelGoodsInventoryLoss(row) {
      API_GoodsInventoryLoss.cancelGoodsInventoryLoss(row.id).then(() => {
        this.$message.success('撤回成功！')
        this.GET_GoodsInventoryLossList()
      })
    },
    /** 确认 */
    handleConfirmGoodsInventoryLoss(row) {
      // if (!this.multipleSelection || !this.multipleSelection.length) return this.$message.error('请先选择！')
      this.$confirm('确定吗？', '提示', { type: 'warning' }).then(() => {
        // const ids = this.multipleSelection.map(item => item.id)
        API_GoodsInventoryLoss.InventoryLossFormConfirm(row.id).then(() => {
          this.$message.success('确认成功！')
          this.GET_GoodsInventoryLossList()
        })
      }).catch(() => { })
    },
    /** 添加调整单 */
    handleAddGoodsInventoryLoss() {
      this.$router.push({
        name: 'goodsInventoryLossAdd',
        params: { callback: this.GET_GoodsInventoryLossList }
      })
    },
    /** 打印调整单 */
    handlePrintGoodsInventoryLossDialog(row) {
      this.GET_GoodsInventoryLossDetail(row.id)
    },
    /** 删除 */
    handleDeleteGoodsInventoryLoss() {
      console.log('this.multipleSelection=', this.multipleSelection)
      if (!this.multipleSelection || !this.multipleSelection.length) return this.$message.error('请先选择！')
      this.$confirm('确定要删除当前调整单吗？', '提示', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(item => item.id)
        API_GoodsInventoryLoss.deleteGoodsInventoryLoss(ids).then(() => {
          this.$message.success('删除成功！')
          this.GET_GoodsInventoryLossList()
        })
      }).catch(() => { })
    },
    /** 状态 */
    statusFilter(val) {
      let name = ''
      this.inventoryType.map(el => {
        if (el.value == val) {
          return name = el.label
        }
      })
      return name
    },
    /** 获取部门 */
    GET_DeptList() {
      API_Setting.getDeptList().then(response => {
        this.deptList = Foundation.buildTree(response, '0')
      })
    },
    /** 打印对账详情 */
    handlePrintGoodsInventoryLoss() {
      Print({
        printable: 'deliverySheet',
        type: 'html',
        targetStyles: ['*'],
        ignoreElements: ['no-logs', 'goods-image', 'no-btn']
      })
    },
    /** 多选改变 */
    handleSelectionChange(val) {
      console.log('val=-', val)
      this.multipleSelection = val
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_GoodsInventoryLossList()
    },
    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_GoodsInventoryLossList()
    },
    /** 搜索事件触发 */
    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_GoodsInventoryLossList()
    },
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      delete this.params.start_time
      delete this.params.end_time
      if (this.advancedForm.GoodsInventoryLoss_time_range) {
        this.params.start_time = parseInt(Number(this.advancedForm.GoodsInventoryLoss_time_range[0]) / 1000)
        this.params.end_time = parseInt(Number(this.advancedForm.GoodsInventoryLoss_time_range[1]) / 1000)
      }
      delete this.params.GoodsInventoryLoss_time_range
      this.params.page_no = 1
      this.GET_GoodsInventoryLossList()
    },
    /** 获取调整单列表 */
    GET_GoodsInventoryLossList() {
      this.loading = true
      API_GoodsInventoryLoss.getGoodsInventoryLossList(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;
  }
}

.print {
  margin-left: 790px;
}

.deliverySheet-header {
  display: flex;
  justify-content: center;
  align-items: center;
}

.tips-t {
  display: flex;
  padding: 30px 0;

  div {
    flex: 1;
    text-align: left;
    padding-left: 70px;
  }
}

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

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

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