<template>
  <a-card :bordered="false">
    <div v-if="visible">
      <!--      <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-purchase-order-lists-export')" type="primary" icon="download" @click="doExport">导出</a-button>
        <a-button style="margin-left: 8px" v-if="$auth('purchase-purchase-order-lists-export') && !viewFlag && user.userId === order.purchaserId" type="primary" icon="upload" @click="handleImport">导入</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
        :data="loadData"
        :showSizeChanger="showSizeChanger"
        :showPagination="showSizeChanger"
      >
        <span slot="action" slot-scope="record">
          <template>
            <!--            <a @click="handleView(record)">查看</a>-->
            <operation-button
              :show-divider="false"
              v-if="user.userId === order.purchaserId"
              :type="1"
              @click="handleEdit(record)">编辑</operation-button>
          </template>
        </span>
      </s-table>
    </div>
    <enquiry-form ref="baseEnquiryModal" @ok="handleOk"/>
    <import-form ref="baseImportModal" @ok="handleOk"/>
    <enquiry-detail ref="detailEnquiryModal"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getPurchaseOrderListPage, exportPurchaseOrderListForEnquiry } from '@/api/purchase/purchase-order-list'
import EnquiryForm from './enquiry-modules/BaseForm'
import ImportForm from './enquiry-modules/ImportForm'
import EnquiryDetail from './enquiry-modules/Detail'

export default {
  name: 'PurchaseOrderList',
  components: {
    STable,
    Ellipsis,
    ImportForm,
    EnquiryForm,
    EnquiryDetail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        pageSize: 9999
      },
      visible: false,
      purchaseOrderId: '-1',
      user: this.$store.getters.userInfo,
      showSizeChanger: false,
      columns: [],
      // 表头
      columnItems: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '编号',
          dataIndex: 'no',
          checked: true,
          width: 200
        },
        {
          title: '名称',
          dataIndex: 'name',
          checked: true,
          width: 150
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '供应商名称',
          dataIndex: 'supplierName',
          width: 150,
          checked: true
        },
        {
          title: '供应商单价',
          dataIndex: 'supplyPrice',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '供应商单价(按记账主币)',
          dataIndex: 'accountPrice',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '指导单价',
          dataIndex: 'price',
          checked: true,
          width: 100,
          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: 'accountTotalMoney',
          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: 'specs',
          checked: true,
          width: 100
        },
        {
          title: '单位',
          dataIndex: 'unit',
          checked: true,
          width: 90
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          checked: true,
          width: 120
        },
        {
          title: '已购数量',
          dataIndex: 'boughtQuantity',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已到数量',
          dataIndex: 'arrivedQuantity',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已发数量',
          dataIndex: 'dispatchedQuantity',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '未发运数量',
          dataIndex: 'notDispatchQuantity',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已用金额',
          dataIndex: 'usedMoney',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '产地',
          dataIndex: 'producePlace',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.areaMap, text)
          }
        },
        {
          title: '重量',
          dataIndex: 'weight',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '体积',
          dataIndex: 'volume',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '外形尺寸',
          dataIndex: 'dimension',
          width: 90
        },
        {
          title: '适用车型',
          dataIndex: 'suitableModel',
          width: 120,
          checked: true
        },
        // {
        //   title: '车架号',
        //   dataIndex: 'vin',
        //   width: 150
        // },
        // {
        //   title: '发动机号',
        //   dataIndex: 'engineNum',
        //   width: 150
        // },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 100

        },
        {
          title: '创建人名称',
          dataIndex: 'createdUserName',
          width: 120
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 120
        }
      ],
      // 下拉框map
      unitMap: {},
      type: null,
      statusMap: {},
      areaMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          purchaseOrderId: this.purchaseOrderId,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseOrderListPage(parameter)
          .then(res => {
            const data = res.data
            data.rows = this.BaseTool.ListForm.getOneListHandledRows(this, data.rows)
            data.total = res.data.rows.length
            let money = 0
            data.rows.forEach((item) => {
              money += item.supplyTotalMoney
            })
            this.$emit('ok', money)
            return data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      order: {},
      isCreated: false,

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  props: {
    viewFlag: {
      type: Boolean,
      default: false
    }
  },
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_LIST_STATUS)
    this.areaMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
    if (!this.viewFlag) {
      this.columns = [
        ...this.columnItems,
        {
          title: '操作',
          key: 'action',
          width: '200px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]
      this.optionAlertShow = true
    } else {
      this.columns = [
        ...this.columnItems
      ]
      this.optionAlertShow = false
    }
    this.tableOption()
  },
  methods: {
    base (record) {
      this.visible = true
      console.log(record)
      this.purchaseOrderId = record.id
      this.order = record
      this.type = record.type
      if (this.isCreated) {
        this.handleOk()
      } else {
        this.tableOption()
        this.isCreated = true
      }
      this.visible = true
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleView (record) {
      const modal = this.$refs.detailEnquiryModal
      modal.base(record)
    },
    handleImport () {
      const modal = this.$refs.baseImportModal
      modal.base(this.order)
    },
    handleEdit (record) {
      const moneyType = this.$store.getters.moneyType
      if (!moneyType) {
        this.$message.error('请先选择币种')
        return
      }
      // const changeFlag = this.$store.getters.changeFlag
      // if (!changeFlag) {
      //   this.$message.error('已完成询价的需要切换币种才能修改')
      //   return
      // }
      const modal = this.$refs.baseEnquiryModal
      modal.base(record, this.type)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        purchaseOrderId: this.purchaseOrderId
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam,
        purchaseOrderId: this.purchaseOrderId
      }
      exportPurchaseOrderListForEnquiry(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
