<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="stNo">
        <el-input v-model="queryParams.stNo" 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">
      <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="stNo" width="200" />
      <el-table-column label="调出门店" :show-overflow-tooltip="true" align="center" prop="outCorpId" width="200">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.outCorpId)}}
        </template>
      </el-table-column>
      <el-table-column label="调入门店" :show-overflow-tooltip="true" align="center" prop="inCorpId" width="200">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.inCorpId)}}
        </template>
      </el-table-column>
      <el-table-column label="总数量" align="center" prop="itemCount" width="180" />
      <el-table-column label="总金额" align="center" prop="totalPrice" width="180" />
      <el-table-column label="调拨日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          {{parseTime(scope.row.stTime, '{y}-{m}-{d}')}}
        </template>
      </el-table-column>
      <el-table-column label="调拨人" align="center" prop="userName" width="180" />
      <el-table-column label="状态" align="center" prop="stStatus" width="180">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.REPAIR_ST_STATUS" v-model="scope.row.stStatus" />
        </template>
      </el-table-column>
      <el-table-column label="作废备注" align="center" prop="remark" width="180" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
    />
    <StShow ref="stShow" />
  </div>
</template>

<script>
import CorpChoose from "@/views/repair/Components/CorpChoose.vue";
import {getStPage} from "@/api/repair/stockTransfer/stockTransfer";
import {getCompanyList} from "@/api/base/company";
import StShow from "@/views/repair/stockTransfer/Components/StShow.vue";

export default {
  name: "StVoid",
  components: {StShow, CorpChoose},
  data() {
    return {
      queryParams:{
        pageNo: 1,
        pageSize: 10,
        searchTimeArray: [],
        stStatus: "04",
        outCorp: null,
        outCorpId: null,
        inCorp: null,
        inCorpId: null,
        stNo: null
      },
      showSearch:true,
      loading: false,
      list: [],
      total: 0,
      corpList: []
    }
  },
  mounted() {
    this.getPage()
    this.listCompany()
  },
  methods: {
    async getPage(){
      try {
        this.loading = true
        const res = await getStPage(this.queryParams)
        this.list = res.data.records
        this.total = res.data.total
      }finally {
        this.loading = false
      }
    },
    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
    },
    handleShow(row){
      row = {
        ...row,
        outCorpName: this.getCompanyName(row.outCorpId),
        inCorpName: this.getCompanyName(row.inCorpId),
      }
      this.$refs.stShow.open(row)
    },
    handleReset(){
      this.resetForm('queryForm')
      this.queryParams.outCorp = null
      this.queryParams.inCorp= null
      this.handleQuery()
    },
    handleQuery(){
      this.queryParams.pageNo = 1
      this.queryParams.inCorpId = this.queryParams?.inCorp?.id
      this.queryParams.outCorpId = this.queryParams?.outCorp?.id
      this.getPage()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
