<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="调拨时间" 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="调出门店" prop="outCorpId">
        <CorpChoose v-model="queryParams.outCorp"/>
      </el-form-item>
      <el-form-item label="调入门店" prop="inCorpId">
        <CorpChoose v-model="queryParams.inCorp"/>
      </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="handleReset">重置</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="st.stNo" width="200"/>
      <el-table-column label="商品名称" align="center" prop="wares.name" width="180"/>
      <el-table-column label="规格" align="center" prop="wares.model" width="180"/>
      <el-table-column label="商品编码" align="center" prop="wares.code" width="180"/>
      <el-table-column v-if="!stByType" label="条形码" align="wares.barCode" prop="soNo" width="180"/>
      <el-table-column label="数量" align="center" prop="goodsCount" width="150"/>
      <el-table-column label="成本" align="center" prop="wares.price" width="150"/>
      <el-table-column label="合计成本" align="center" prop="totalPrice" width="150">
        <template slot-scope="scope">
          {{scope.row.wares.price * scope.row.goodsCount}}
        </template>
      </el-table-column>
      <el-table-column v-if="stByType" label="出库金额" align="center" prop="goodsPrice" width="150"/>
      <el-table-column label="合计金额" align="center" prop="itemCount" width="150">
        <template slot-scope="scope">
          {{scope.row.goodsPrice * scope.row.goodsCount}}
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="st.userName" width="150"/>
      <el-table-column label="调出门店" align="center" prop="st.outCorpId" width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.st.outCorpId)}}
        </template>
      </el-table-column>
<!--      <el-table-column label="调出仓库" align="center" prop="itemCount" width="180"/>-->
      <el-table-column label="调入门店" align="center" prop="st.inCorpId" width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.st.inCorpId)}}
        </template>
      </el-table-column>
<!--      <el-table-column label="调入仓库" align="center" prop="itemCount" width="180"/>-->
      <el-table-column label="调拨时间" align="center" prop="st.stTime" width="180"/>
      <el-table-column label="备注" align="center" prop="remark" width="180"/>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
    />
  </div>
</template>

<script>
import {getStiPage, getStIsWares} from "@/api/repair/stockTransfer/stockTransferItem";
import CorpChoose from "@/views/repair/Components/CorpChoose.vue";
import {getCompanyList} from "@/api/base/company";

export default {
  name: "StiTable",
  components: {CorpChoose},
  props: {
    stByType: {
      type: Boolean,
      defaultValue: true,
      required: true
    }
  },
  watch: {
    'queryParams.outCorp'(val) {
      this.queryParams.outCorpId = val ? val.id : null
    },
    'queryParams.inCorp'(val) {
      this.queryParams.inCorpId = val ? val.id : null
    }
  },
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        searchTimeArray: [],
        outCorp: null,
        outCorpId: null,
        inCorp: null,
        inCorpId: null,
        query: null
      },
      showSearch: true,
      loading: false,
      list: [],
      total: 0,
      corpList: []
    }
  },
  mounted() {
    this.getList()
    this.listCompany()
  },
  methods: {
    async listCompany(){
      const res = await getCompanyList();
      this.corpList = res.data
    },
    getCompanyName(id){
      if (!(this.corpList && this.corpList.length > 0)) return ''
      return this.corpList.find(item => item.id === id).corpName
    },
    async getList() {
      const res = await getStiPage(this.queryParams)
      const ids = res.data.records.map(item => item.id)
      if (ids && ids.length > 0){
        const response = await getStIsWares(ids)
        if (response.data && response.data.length > 0) {
          this.list = response.data
        } else {
          this.list = res.data.records
        }
      }else {
        this.list = res.data.records
      }
      this.total = res.data.total
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      this.queryParams.inCorpId = this.queryParams?.inCorp?.id
      this.queryParams.outCorpId = this.queryParams?.outCorp?.id
      this.getList()
    },
    handleReset() {
      this.resetForm("queryForm")
      this.queryParams.outCorp = null
      this.queryParams.inCorp = null
      this.handleQuery()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
