<template>
  <a-card :bordered="false" title="供货记录" v-if="visible">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.keyword" placeholder="请输入名称/类型名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="8 || 24" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
<!--      <a-button style="margin-left: 8px" v-if="$auth('purchase-supplier-goods-records-export')" type="primary" icon="download" @click="doExport">导出</a-button>-->
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
      :data="loadData"
      showPagination="auto"
    >
    </s-table>

  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getPurchaseListPage, exportPurchaseList } from '@/api/purchase/purchase-list'

export default {
  name: 'SupplierGoodsRecordList',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      visible: false,
      supplierId: '',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '编号',
          dataIndex: 'no',
          checked: true,
          width: 180
        },
        {
          title: '名称',
          dataIndex: 'name',
          checked: true,
          width: 150
        },
        {
          title: '规格',
          dataIndex: 'specs',
          checked: true,
          width: 100
        },
        {
          title: '单位',
          dataIndex: 'unit',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.unitMap, text)
          }
        },
        {
          title: '单价',
          dataIndex: 'supplyPrice',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '转换单价($)',
          dataIndex: 'dollarPrice',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '指导单价',
          dataIndex: 'price',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '总额',
          dataIndex: 'supplyTotalMoney',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '转换总额($)',
          dataIndex: 'dollarMoney',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '指导总价',
          dataIndex: 'totalMoney',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          checked: true,
          width: 120
        },
        {
          title: '适用车型',
          dataIndex: 'suitableModel',
          width: 120
        },
        {
          title: '车架号',
          dataIndex: 'vin',
          width: 150
        },
        {
          title: '发动机号',
          dataIndex: 'engineNum',
          width: 150
        },
        {
          title: '产地',
          dataIndex: 'producePlace',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.producePlaceMap, text)
          }
        },
        {
          title: '重量',
          dataIndex: 'weight',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '体积',
          dataIndex: 'volume',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '外形尺寸',
          dataIndex: 'dimension',
          width: 90
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 100

        },
        {
          title: '创建人名称',
          dataIndex: 'createdUserName',
          width: 120
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 120
        }
      ],
      // 下拉框map
      unitMap: {},
      statusMap: {},
      producePlaceMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          supplierId: this.supplierId,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseListPage(parameter)
          .then(res => {
            return res.data
          })
      }
    }
  },
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.producePlaceMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
  },
  methods: {
    base (supplierId) {
      this.supplierId = supplierId
      this.visible = true
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam,
        supplierId: this.supplierId
      }
      exportPurchaseList(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    },
    handleCancel (values) {
      this.visible = false
      this.$emit('ok', values)
    }
  }
}
</script>
