<!--采购历史-->
<!--2019年6月23日 19:44:56 by jlx-->
<template>
  <div class="pur-dialog">
    <WinseaContentModal v-model='dialogFormVisible'
                        :title="$t('cg.purchaseHistory.history')">
      <!-- <ws-dialog :title="$t('cg.purchaseHistory.history')"
               :visible.sync="dialogFormVisible"
               style="min-width:1000px;"> -->
      <!-- 备件start -->
      <div v-if="showType == 'parityDetail' || !showType">
        <BaseRow>
          <BaseCol :span="10"
                   style="text-align: left;">
            {{ $t('cg.cgsparedetail.spName') }}{{$t('common.colon')}}
            {{partRow.spName}}
          </BaseCol>
          <BaseCol :span="5"
                   style="text-align: left;">
            {{ $t('cg.cgsparedetail.spCode') }}{{$t('common.colon')}}
            {{ $ph(partRow.spCode)}}
          </BaseCol>
          <BaseCol :span="6"
                   style="text-align: left;">
            {{ $t('cg.cgsparedetail.specification') }}{{$t('common.colon')}}
            {{$ph(partRow.drawNumber)}}
          </BaseCol>
          <BaseCol :span="3"
                   style="text-align: left;">
            {{ $t('cg.cgsparedetail.measurementUnit') }}{{$t('common.colon')}}
            {{$ph(partRow.measurementUnit)}}
          </BaseCol>
        </BaseRow>
      </div>
      <!-- 备件end -->
      <!-- 物料start -->
      <div v-if="showType == 'parityDetailMa'">
        <BaseRow>
          <BaseCol :span="24"
                   style="text-align: left;">{{ $t('cg.cgsmadetail.maTypeName') }}{{$t('common.colon')}}{{
            deviceRow.spName
          }}</BaseCol>
        </BaseRow>
      </div>
      <!-- 物料end -->
      <!-- 海图start -->
      <div v-if="showType == 'parityDetailMap'">
        <BaseRow>
          <BaseCol :span="24"
                   style="text-align: left;">{{ $t('cg.cgsmadetail.maTypeName') }}{{$t('common.colon')}}{{
            deviceRow.spName
          }}</BaseCol>
        </BaseRow>
      </div>
      <!-- 海图end -->

      <ws-normal-table :data="Info"
                       height="300"
                       :empty-text="$t('common.empty')"
                       highlight-current-row>
        <ws-table-column type="index"
                         :label="$t('cg.pSparepart.sparepartList.spId')"
                         width="50" />
        <!--供货商-->
        <ws-table-column prop="vendorName"
                         :label="$t('cg.purchaseHistory.vendorName')"
                         width="160" />
        <!-- 采购数量 -->
        <ws-table-column prop="verifyQuantity"
                         width="120"
                         :label="$t('cg.purchaseHistory.purchaseQuantity')" />
        <!-- 定价 -->
        <ws-table-column prop="unitPrice"
                         width="80"
                         :label="$t('cg.purchaseHistory.unitPrice')">
          <template slot-scope="scope">
            <span>{{
              scope.row.unitPrice
                ? scope.row.unitPrice.toFixed(2)
                : scope.row.unitPrice
            }}</span>
          </template>
        </ws-table-column>
        <!-- 小计 -->
        <ws-table-column prop="subtotal"
                         width="120"
                         :label="$t('cg.purchaseHistory.subtotal')">
          <template slot-scope="scope">
            <span>{{
              scope.row.subtotal
                ? scope.row.subtotal.toFixed(2)
                : scope.row.subtotal
            }}</span>
          </template>
        </ws-table-column>
        <!-- 币种 -->
        <ws-table-column prop="currencyId"
                         width="80"
                         :label="$t('cg.purchaseHistory.currencyType')" />
        <!-- 采购单号 -->
        <ws-table-column prop="purchaseNo"
                         :label="$t('cg.purchaseHistory.sparepartNo')" />
        <!-- 采购日期 -->
        <ws-table-column width="120"
                         prop="makeDate"
                         :label="$t('cg.purchaseHistory.purDate')" />
      </ws-normal-table>
      <!-- 列表-页码 -->
      <pagination style="padding-right: 40px;"
                  v-show="total > 0"
                  :total="total"
                  :page.sync="listQuery.currentPage"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getlistbyWhere" />

      <div slot="footer"
           class="dialog-footer">
        <ws-button @click="dialogFormVisible = false">{{
          $t('tagsView.close')
        }}</ws-button>
      </div>
    </WinseaContentModal>
    <!-- </ws-dialog> -->
    <!-- 弹出页面-新增修改 -->
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { selectHistory, getTypeList } from '@/model/procurement/spare'
import { selectHistoryMa } from '@/model/procurement/material'
import { selectHistoryMap } from '@/model/procurement/map'
export default {
  name: 'purHistory',
  components: {
    Pagination
  },
  props: ['showType'],
  data () {
    return {
      dialogFormVisible: false,
      loading: false,
      Info: [],
      // 查询总条数
      total: 10,
      // 查询数据结构
      listQuery: {
        compId: localStorage.getItem('ws-pf_compId'), // 公司ID
        currentPage: 1, // 当前页码
        pageSize: 10 // 一页记录数
      },
      deviceRow: {},
      partRow: {},
      // 币种
      currencyList: null,
      language: ''
    }
  },
  created () {
    this.language = this.$store.getters.language
  },
  methods: {
    // 数据请求
    getlistbyWhere () {
      if (this.showType == 'parityDetail' || !this.showType) {
        // 备件数据查询
        const queryModel = {
          compId: this.listQuery.compId, // 公司ID
          currentPage: this.listQuery.currentPage, // 当前页码
          pageSize: this.listQuery.pageSize, // 一页记录数
          spId: this.partRow.spId,
        }
        selectHistory(queryModel).toPromise().then(() => this.getDateSet)
      } else if (this.showType == 'parityDetailMa') {
        // 物料数据查询
        const queryModel = {
          compId: this.listQuery.compId, // 公司ID
          currentPage: this.listQuery.currentPage, // 当前页码
          pageSize: this.listQuery.pageSize, // 一页记录数
          maId: this.partRow.spId,
        }
        selectHistoryMa(queryModel).toPromise().then(this.getDateSet)
      } else if (this.showType == 'parityDetailMap') {
        // 海图数据查询
        const queryModel = {
          compId: this.listQuery.compId, // 公司ID
          currentPage: this.listQuery.currentPage, // 当前页码
          pageSize: this.listQuery.pageSize, // 一页记录数
          mcId: this.partRow.spId,
        }
        selectHistoryMap(queryModel).toPromise().then(() => this.getDateSet)
      }
    },
    // 请求后设置返回值
    getDateSet (succ) {
      this.Info = succ.records
      this.setTypeList()
      this.total = succ.total
      this.listQuery.currentPage = succ.current
      this.listQuery.pageSize = succ.size
    },
    // 金额获取
    setTypeList () {
      getTypeList({
        compId: this.listQuery.compId,
        constCode: 'CURRENCY'
      }).toPromise().then(response => {
        this.currencyList = response
        for (const k in this.Info) {
          this.Info[k].subtotal =
            this.Info[k].verifyQuantity * this.Info[k].unitPrice
          for (const c in this.currencyList) {
            if (this.currencyList[c].parameterKey === this.Info[k].currencyId) {
              this.Info[k].currencyId =
                this.language === 'en'
                  ? this.currencyList[c].parameterValueEn
                  : this.currencyList[c].parameterValue
            }
          }
        }
        this.currencyList.reverse()
        this.currencyList.reverse()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.pur-dialog {
  ::v-deep .el-dialog {
    min-width: 1000px;
  }
  .el-row {
    margin-bottom: 10px;
    border: 0px solid red;
  }
}
</style>
