<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" v-if="checkPermission(['goodsExchange:add'])" type="primary"
            @click="handleExportGoodsExchange">导出</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-cascader v-model="advancedForm.dept_id" :options="deptList" :props="{
                    label: 'name',
                    value: 'id',
                    emitPath: false,
                    checkStrictly: true
                  }" clearable></el-cascader>
                </el-form-item>
                <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-form-item label="商品类别" prop="category_id">
                  <el-cascader v-model="advancedForm.category_id" :options="categorys" :props="{
                    label: 'name',
                    value: 'category_id',
                    emitPath: false,
                    checkStrictly: true
                  }" clearable></el-cascader>
                </el-form-item>
                <el-form-item label="商品名称" prop="product_name">
                  <el-input v-model="advancedForm.product_name" :minlength="2" :maxlength="20" clearable
                    placeholder="请输入商品名称"></el-input>
                </el-form-item>
                <el-form-item label="报损类型">
                  <el-select v-model="advancedForm.damage_type_value">
                    <el-option v-for="item in roleSplit" :key="item.label" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="时间">
                  <el-date-picker v-model="advancedForm.goodsExchange_time_range" type="daterange" align="center"
                    :editable="false" unlink-panels :clearable="false" :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 prop="sn" label="调整单编号">
          <template slot-scope="scope">
            <router-link
            v-if="checkPermission(['goodsInventoryLoss'])"
            style="color: #409eff;"
            :to="{ name: 'goodsInventoryLossDetail', params: { id: scope.row.report_id } }">
            {{ scope.row.sn }}</router-link>
            <span v-else>{{scope.row.sn}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="report_time" label="调整时间">
          <template slot-scope="scope">{{ scope.row.report_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column prop="stock_sn" label="入库单编号" />
        <el-table-column prop="product_sn" label="商品编号" />
        <el-table-column prop="product_name" label="商品名称" />
        <el-table-column prop="category_name" label="商品类别" />
        <el-table-column prop="specification" label="规格型号" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="damage_type_key" label="报损类型" />
        <el-table-column prop="report_num" 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_Statistics from '@/api/statistics'
import * as API_Setting from '@/api/setting'
import { Foundation } from '~/ui-utils'
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_Category from '@/api/category'
const FileDownload = require('js-file-download')
export default {
  name: 'stockUpdateSratistics',
  data() {
    return {
      categorys: [],
      deptList: [],
      roleSplit: [],
      warehouseList: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      dialogVisible: false
    }
  },
  mounted() {
    this.GET_GoodsExchangeList()
    this.GET_DeptList()
    this.GET_WarehouseList()
    this.GET_Category()
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        this.params.page_no = 1
        if (newVal.params.product_sn) {
          this.params.product_sn = newVal.params.product_sn
          this.advancedForm.product_sn = newVal.params.product_sn
        }
        this.GET_GoodsExchangeList()
        this.GET_DictDataInfo()
      }
    }
  },
  methods: {
    /** 入库单状态 */
    statusFilter(val) {
      switch (val) {
        case 'DRAFT':
          return '未提交'
        case 'Submit':
          return '已提交'
        case 'AuditPass':
          return '审核通过'
        case 'AuditReject ':
          return '审核驳回'
        default:
          return ''
      }
    },
    GET_DictDataInfo() {
      API_Setting.getDictDataInfo('es_stock_damage_report').then(response => {
        this.roleSplit = response
      })
    },
    /** 获取类别 */
    GET_Category() {
      API_Category.getCategoryAllChildren(0).then(response => {
        this.categorys = response
      })
    },
    /** 获取仓库 */
    GET_WarehouseList() {
      API_BasicSetting.getWarehouseListAll().then(response => {
        this.warehouseList = response
      })
    },
    /** 获取部门 */
    GET_DeptList() {
      API_Setting.getDeptList().then(response => {
        this.deptList = Foundation.buildTree(response, '0')
      })
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_GoodsExchangeList()
    },

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

    /** 导出 */
    handleExportGoodsExchange() {
      API_Statistics.getStockDamageReportExport(this.params).then(response => {
        FileDownload(response, '库存调整统计.xlsx')
      })
    },
    /** 搜索事件触发 */
    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_GoodsExchangeList()
    },
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      delete this.params.start_time
      delete this.params.end_time
      if (this.advancedForm.goodsExchange_time_range) {
        this.params.start_time = parseInt(Number(this.advancedForm.goodsExchange_time_range[0]) / 1000)
        this.params.end_time = parseInt(Number(this.advancedForm.goodsExchange_time_range[1]) / 1000)
      }
      delete this.params.goodsExchange_time_range
      this.params.page_no = 1
      this.GET_GoodsExchangeList()
    },
    /** 获取列表 */
    GET_GoodsExchangeList() {
      this.loading = true
      API_Statistics.getStockDamageReportStatistics(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.print {
  margin-left: 790px;
}

.tips-t {
  display: flex;
  padding: 10px 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>
