<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 label="审核状态" prop="stStatus">-->
<!--        <el-select v-model="queryParams.stStatus">-->
<!--          <el-option v-for="item in this.getDictDatas(DICT_TYPE.REPAIR_ST_STATUS)" :key="item.value" :label="item.label"-->
<!--                     :value="item.value"/>-->
<!--        </el-select>-->
<!--      </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="调出门店" align="center" prop="outCorpId" width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.outCorpId)}}
        </template>
      </el-table-column>
      <el-table-column label="调入门店" align="center" prop="inCorpId" width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{getCompanyName(scope.row.inCorpId)}}
        </template>
      </el-table-column>
      <el-table-column label="调拨数量" align="center" prop="itemCount" width="150"/>
      <el-table-column v-if="stByType" label="调拨成本" align="center" prop="totalPrice" width="150"/>
      <el-table-column label="调拨总额" align="center" prop="totalPrice" width="150"/>
      <el-table-column v-if="stByType" label="差额" align="center" prop="itemCount" width="150"/>
      <el-table-column v-if="stByType" label="本次优惠" align="center" prop="discountPrice" width="150"/>
      <el-table-column v-if="stByType" label="本次收款" align="center" prop="thisCollection" width="150"/>
      <el-table-column v-if="stByType" label="本次欠款" align="center" prop="thisDebt" width="150"/>
      <el-table-column label="调拨日期" align="center" prop="stTime" width="150"/>
      <el-table-column label="调拨人" align="center" prop="userName" width="150"/>
      <el-table-column v-if="!stByType" label="审核日期" align="center" prop="itemCount" width="150"/>
      <el-table-column label="状态" align="center" prop="stStatus" width="150">
        <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 v-if="!stByType" label="退回备注" align="center" prop="remark" width="180"/>
      <el-table-column label="操作" fixed="right" width="180" align="center">
        <template v-if="stByType" v-slot="scope">
          <!--          <el-button size="mini" type="text" icon="el-icon-edit"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)"
          >查看
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleVoid(scope.row)"
          >作废
          </el-button>
        </template>
        <template v-else v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
          >审核
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-close"
          >退回
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination style="margin-bottom: 2rem" 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, voidSt} from "@/api/repair/stockTransfer/stockTransfer";
import {getCompanyList} from "@/api/base/company";
import StShow from "@/views/repair/stockTransfer/Components/StShow.vue";


export default {
  name: "StIndex",
  components: {StShow, 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,
        stStatus: null,
        outCorp: null,
        outCorpId: null,
        inCorp:null,
        inCorpId: null,
        searchTimeArray: [],
        stNo: null,
      },
      showSearch: true,
      loading: false,
      list: [],
      total: 0,
      formData:{},
      companyList: []
    }
  },
  mounted() {
    this.getList()
    this.companyInit()
  },
  methods: {
    async companyInit(){
      if (!(this.companyList && this.companyList.length > 0)){
        const res = await getCompanyList();
        this.companyList = res.data
      }
    },
    getCompanyName(id){
      if (!(this.companyList && this.companyList.length > 0)) return ""
      return this.companyList.find(item => item.id === id).corpName
    },
    async getList() {
      const res = await getStPage(this.queryParams)
      this.list = res.data.records
      this.total = res.data.total
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.resetForm('queryForm')
      this.queryParams.outCorp = null
      this.queryParams.inCorp = null
      this.handleQuery()
    },
    handleVoid(row){
      this.$prompt('作废备注', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        this.formData.id = row.id
        this.formData.remark = value
        this.formData.stStatus = "04"
        this.doVoid()
      }).catch(() => {
      })
    },
    async doVoid(){
      await voidSt(this.formData)
      this.$modal.msgSuccess("作废成功")
      await this.getList()
    },
    handleShow(row){
      row = {
        ...row,
        outCorpName: this.getCompanyName(row.outCorpId),
        inCorpName: this.getCompanyName(row.inCorpId),
      }
      this.$refs.stShow.open(row)
    },
  }
}
</script>

<style scoped lang="scss">

</style>
