<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="supplier_id">
                  <el-select v-model="advancedForm.supplier_id">
                    <el-option v-for="item in supplierList" :key="item.id" :label="item.custom_name" :value="item.id" />
                  </el-select>
                </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-date-picker v-model="advancedForm.goodsExchange_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 prop="warehouse_name" label="仓库名称" />
        <el-table-column prop="dept_name" label="所属部门" />
        <el-table-column prop="supplier_name" label="供应商" />
        <el-table-column prop="entry_sn" label="入库单编号">
          <template slot-scope="scope">
            <router-link
              v-if="checkPermission(['statisticsOfGoods'])"
              style="color: #409eff;"
              :to="{ name: 'statisticsOfGoods', params: { warehouse_entry_sn: scope.row.entry_sn,  product_id: scope.row.product_id} }">
              {{ scope.row.entry_sn }}
            </router-link>
            <span v-else>{{scope.row.entry_sn}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sn" label="商品编号" />
        <el-table-column prop="name" label="商品名称">
          <template slot-scope="scope">
            <router-link
              v-if="checkPermission(['stockUpdateStatistics'])"
              style="color: #409eff;"
              :to="{ name: 'stockUpdateStatistics', params: { product_name: scope.row.name } }">
              {{ scope.row.name }}
            </router-link>
            <span v-else>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="category_name" label="商品类别" />
        <el-table-column prop="specification" label="规格型号" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="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: 'purchasePlanStatistics',
  data() {
    return {
      supplierList: [],
      categorys: [],
      deptList: [],
      warehouseList: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      dialogVisible: false
    }
  },
  mounted() {
    this.GET_DeptList()
    this.GET_WarehouseList()
    this.GET_Category()
    this.GET_SupplierList()
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        if (newVal.name === 'purchasePlanStatistics' && newVal.params.product_name) {
          this.params.page_no = 1
          this.params.product_name = newVal.params.product_name
          this.advancedForm.product_name = newVal.params.product_name
        }
        this.GET_GoodsExchangeList()
      }
    }
  },
  methods: {
    /** 获取供应商 */
    GET_SupplierList() {
      API_BasicSetting.getSupplierListAll().then(response => {
        this.supplierList = 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.getwarehouseEntryExport(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.getwarehouseEntryStatistics(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>
