<template>
  <div>
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item :label="soByType ? '采购时间' : '领料时间'" prop="searchTimeArray">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="queryParams.searchTimeArray"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="关键字" prop="query">
        <el-input v-model="queryParams.query" style="width: 18rem" placeholder="请输入单号、商品名称、编码或规格"/>
      </el-form-item>
      <el-form-item :label="soByType ? '采购门店' : '领料门店'" prop="corpId">
        <CorpChoose v-model="queryParams.corp" />
      </el-form-item>
      <el-form-item v-if="soByType" label="所属库位" prop="wareId">
        <WarehouseChoose v-model="queryParams.ware" @selected="getWarehouse"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleResetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-download" size="mini"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"></right-toolbar>
    </el-row>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单号" align="center" prop="so.soNo" width="200" />
      <el-table-column label="商品名称" align="center" prop="repairWares.name" width="180" />
      <el-table-column :label="soByType ? '采购员' : '领料人'" align="center" prop="so.userName" width="180" />
      <el-table-column label="商品编码" align="center" prop="repairWares.code" width="180" />
      <el-table-column v-if="soByType" label="条形码" align="center" prop="repairWares.barCode" width="180" />
      <el-table-column label="规格" align="center" prop="repairWares.model" width="180" />
      <el-table-column label="数量" align="center" prop="goodsCount" width="150" />
      <el-table-column :label="soByType ? '进价' : '成本'" align="center" prop="goodsPrice" width="150" />
      <el-table-column label="合计" align="center" prop="itemCount" width="150">
        <template slot-scope="scope">
          {{scope.row.goodsCount * scope.row.goodsPrice}}
        </template>
      </el-table-column>
      <el-table-column v-if="soByType" label="供应商" align="center" prop="so.supplierName" width="180" />
      <el-table-column :label="soByType ? '采购仓库' : '仓库'" align="center" prop="wareId" width="150">
        <template slot-scope="scope">
          {{getWareHoseName(scope.row.wareId)}}
        </template>
      </el-table-column>
      <el-table-column label="所属门店" align="center" prop="so.corpId" width="180" />
      <el-table-column :label="soByType ? '入库时间' : '领料时间'" align="center" prop="so.createTime" width="150">
        <template slot-scope="scope">
          {{parseTime(scope.row.so.createTime, '{y}-{m}-{d}')}}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="180" />
    </el-table>
    <!-- 分页 -->
    <pagination @pagination="pageSoi" style="margin-bottom: 3rem" v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
    />
  </div>
</template>

<script>
import CorpChoose from "@/views/repair/Components/CorpChoose.vue";
import WarehouseChoose from "@/views/repair/Components/WarehouseChoose.vue";
import {getSoiPage, getRepairSoiByIds} from "@/api/repair/stockOperate/stockOperateItem";
import {getBaseWarehouseList} from "@/api/base/warehouse";

export default {
  name: "SoiTable",
  components: {WarehouseChoose, CorpChoose},
  props: {
    soByType: {
      type: Boolean,
      defaultValue: true,
      required: true
    },
  },
  watch:{
    'queryParams.corp'(val){
      this.queryParams.corpId = val ? val.id : null
    }
  },
  data() {
    return {
      queryParams:{
        pageNo: 1,
        pageSize: 10,
        searchTimeArray:[],
        soNo: null,
        corpId: null,
        soiType: this.soByType ? "01" : "02",
        ware: null,
        wareId: null,
        corp: null,
        query: null
      },
      warehouseList: null,
      showSearch: true,
      loading: false,
      list: [],
      total: 0,
    }
  },
  mounted() {
    this.pageSoi()
  },
  methods: {
    // 库位选择
    getWarehouse(data){
    },
    // 分页
    async pageSoi(){
      try {
        this.loading = true
        const res = await getSoiPage(this.queryParams)
        const data = res.data.records
        if (data && data.length > 0){
          const ids = data.map(item => item.id)
          const response = await getRepairSoiByIds(ids)
          this.list = response.data
          const wareResponse = await getBaseWarehouseList()
          this.warehouseList = wareResponse.data
        }else {
          this.list = data
        }
        this.total = res.data.total
      }finally {
        this.loading = false
      }
    },
    // 搜索
    handleQuery(){
      this.queryParams.pageNo = 1
      if (this.queryParams.ware){
        this.queryParams.wareId = this.queryParams.ware.id
      }
      this.pageSoi()
    },
    // 重置
    handleResetQuery(){
      this.resetForm('queryForm')
      this.queryParams.corp = null
      this.queryParams.ware = null
      this.handleQuery()
    },
    getWareHoseName(value){
      if (!(this.warehouseList && this.warehouseList.length > 0)) return ''
      return this.warehouseList.find(item => item.id === value)?.name
    }
  }
}
</script>

<style scoped lang="scss">

</style>
