<template>
  <div class="purchase-order">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.purchaseOrderNo" placeholder="采购订单ID" />
        </div>
        <div v-if="enterpriseInfo.exportType == 'self_export' && enterpriseInfo.enterpriseType !== '1' && enterpriseInfo.enterpriseType !== '2'">
          <el-select v-model="search.vendorEntityNo" placeholder="请选择供应商" clearable>
            <el-option
              v-for="item in supplierList"
              :key="item.entityNo"
              :label="item.entityName"
              :value="item.entityNo"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.agencyCertNo" placeholder="请选择代理公司" clearable>
            <el-option
              v-for="item in tradePlatformList"
              :key="item.platformCode"
              :label="item.platformName"
              :value="item.platformCode"
            />
          </el-select>
        </div>
        <div>
          <el-input v-model="search.purchaseContractNo" placeholder="采购合同编号" />
        </div>
        <div>
          <el-input v-model="search.salesOrderNo" placeholder="相关销售订单ID" />
        </div>
        <div>
          <el-date-picker
            v-model="addDate"
            type="daterange"
            range-separator="至"
            start-placeholder="创建日期范围"
            end-placeholder="创建日期范围"
          />
        </div>
        <div class="right-btn">
          <el-button type="primary" @click="fetchData('search')">查询</el-button>
          <!-- <el-button type="primary" @click="addOrder">创建订单</el-button> -->
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="PurchaseOrder"
    />

    <cy-dialog ref="editDialog" title="物流单据" :width="'600px'" :top="'13vh'">
      <transport v-if="showUpload" ref="contractUpload" :order-no="transOrderNo" />
      <div class="submit-btn" style="margin: 20px 0;">
        <el-button style="width: 150px;" @click="() => { $refs.editDialog.show = false;showUpload=false }">取消</el-button>
        <el-button type="primary" style="width: 150px;" @click="handleUpload">确定</el-button>
      </div>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import store from '@/store'
import { getSupplier, getPlat } from '@/api/myTrade/salesBill'
import { getOrderList, submitTrans } from '@/api/myTrade/purchaseBill'
import Transport from './components/Transport.vue'
import { codeChangeName, handleMoney } from '@/utils'
export default {
  name: 'PurchaseOrder',
  components: { Transport },
  data() {
    return {
      count: 0,
      search: {
        agencyCertNo: '',
        purchaseContractNo: '',
        purchaseOrderNo: '',
        salesOrderNo: '',
        vendorEntityNo: ''
      },
      page: 1,
      size: 10,
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '采购订单ID',
          prop: 'purchaseOrderNo',
          showTooltip: true
        },
        {
          label: '订单金额',
          showTooltip: true,
          prop: 'amountTotal',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={'￥ ' + handleMoney(row.amountTotal)} placement='top'>
              <div class='one-ellipsis'>
                {'￥ ' + handleMoney(row.amountTotal)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '订单税额',
          showTooltip: true,
          prop: 'taxAmountTotal',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={'￥ ' + handleMoney(row.taxAmountTotal)} placement='top'>
              <div class='one-ellipsis'>
                {'￥ ' + handleMoney(row.taxAmountTotal)}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '供应商名称',
          prop: 'vendorEntityName',
          showTooltip: true
        },
        {
          label: '代理公司名称',
          showTooltip: true,
          prop: 'agencyEnterpriseName'
        },
        {
          label: '采购合同编号',
          showTooltip: true,
          prop: 'purchaseContractNo'
        },
        {
          label: '发票类型',
          prop: 'invoiceType',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.invoice_type, row.invoiceType, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.invoice_type, row.invoiceType, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '相关销售订单ID',
          showTooltip: true,
          prop: 'salesOrderNo'
        },
        {
          label: '创建时间',
          showTooltip: true,
          prop: 'gmtCreate'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              {
                msg: '物流单据',
                icon: 'iconfont icon-a-Group8151',
                rowPower: ['0'],
                category: 'edit'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                // rowPower: ['', null],
                category: 'see'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) && <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      loading: false,
      supplierList: [],
      tradePlatformList: [],

      uploadId: null,
      transOrderNo: null,
      showUpload: false

    }
  },
  computed: {
    ...mapGetters(['constant', 'enterpriseInfo']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.logisticsStatus)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['payment_way', 'sales_order_status', 'invoice_type'])

    this.getSupplierList()

    this.getPlatList()

    // 代理出口 || 内资生产企业 外商投资生产企业 --无供应商列
    if (this.enterpriseInfo.exportType === 'proxy_export' || (this.enterpriseInfo.exportType === 'self_export' && ['1', '2'].includes(this.enterpriseInfo.enterpriseType))) {
      this.columns = this.columns.slice().filter(el => el.label !== '供应商名称')
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.size = 10
      }
      const params = {
        ...this.search,
        timeStart: this.addDate && this.addDate.length ? this.addDate[0] : '',
        timeEnd: this.addDate && this.addDate.length ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getOrderList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = data.records
      })
    },
    handlerOperate(type, row) {
      if (type === 'edit') {
        this.uploadId = row.id
        this.$refs.editDialog.show = true
        this.showUpload = true
        this.transOrderNo = row.purchaseOrderNo
      } else if (type === 'see') {
        this.$router.push({
          name: 'PurchaseOrderView',
          query: {
            type: 'view',
            id: row.id
          }
        })
      }
    },
    async handleUpload() {
      const uploadData = await this.$refs.contractUpload.getParams()
      uploadData.purchaseOrderId = this.uploadId
      uploadData.Loading = true
      submitTrans(uploadData).then(res => {
        this.$message.success('上传成功')
        this.showUpload = false
        this.$refs.editDialog.show = false
        this.fetchData()
      })
    },

    getSupplierList() {
      const userId = store.getters.userInfo.id
      getSupplier({ userId: userId }).then(res => {
        this.supplierList = res.data || []
      })
    },
    getPlatList() {
      getPlat().then(res => {
        this.tradePlatformList = res.data || []
      })
    }
  }
}
</script>

<style lang="scss" scoped>
    .purchase-order {

    }
</style>
<style lang="scss">
    .purchase-order {
        .icon-a-Group957, .icon-a-Group1087 {
            font-size: 28px;
        }
    }
</style>
