<template>
  <el-dialog
    v-dialogDrag
    title="查看采购单明细"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
  >
    <!-- 操作 -->
    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-thumb"
          @click="deliveryEnsure"
        >交期确认</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-magic-stick"
          @click="handleGenerate"
        >生成送货通知单</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
      @selection-change="tableSelected"
    >
      <el-table-column
        type="selection"
        width="55"
        fixed="left"
        align="center"
      />
      <el-table-column
        label="物料编号"
        align="center"
        prop="materialNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="180"
      />
      <el-table-column label="物料数量" align="center" prop="materialQty" />
      <el-table-column
        label="物料规格型号"
        align="center"
        prop="materialModels"
        width="120"
      />
      <el-table-column
        label="物料单位名称"
        align="center"
        prop="unitName"
        width="140"
      />
      <el-table-column
        label="应收货数量"
        align="center"
        prop="shouldReceiveQty"
        width="120"
      />
      <el-table-column
        label="已收货数量"
        align="center"
        prop="receiveQty"
        width="120"
      />
      <el-table-column label="已退数量" align="center" prop="returnQty" />
      <el-table-column
        label="物料状态"
        align="center"
        prop="materialStatus"
        width="120"
      >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_MATERIALS_STATUS" :value="scope.row.materialStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="供应商编号"
        align="center"
        prop="supplierCode"
        width="120"
      />
      <el-table-column
        label="供应商名称"
        align="center"
        prop="supplierName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column label="税率" align="center" prop="taxRate" />
      <el-table-column
        label="确认交货日期"
        align="center"
        prop="confirmDeliveryTime"
        width="160"
      />
      <el-table-column
        label="确认交货数量"
        align="center"
        prop="confirmQty"
        width="160"
      />
      <el-table-column
        label="确认交货意见"
        align="center"
        prop="confirmComment"
        width="160"
      />
      <el-table-column
        label="交货时间"
        align="center"
        prop="deliveryTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <DeliveryConfirmation ref="deliveryConfimation" :current-rows="currentRows" @ensureComplete="ensureComplete" />
    <!-- 送货单 -->
    <GenerateDelivery ref="generateDelivery" :current-rows="currentRows" />
  </el-dialog>
</template>

<script>
import { getPurchaseOrderDetailPage, exportPurchaseOrderDetailExcel } from '@/api/wms/orders/purchase'
// 确认交期
import DeliveryConfirmation from './DeliveryConfirmation.vue'
import GenerateDelivery from './GenerateDelivery.vue'

export default {
  name: 'PurchaseOrderDetail',
  components: {
    DeliveryConfirmation,
    GenerateDelivery
  },
  props: {
    'id': {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ERP采购订单明细列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选中的
      currentRows: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        detailErpId: null,
        inStockId: null,
        erpInStockOrderId: null,
        erpInStockOrderNo: null,
        detailNumber: null,
        materialNo: null,
        materialName: null,
        materialQty: null,
        materialModels: null,
        unitName: null,
        shouldReceiveAmount: null,
        receiveQty: null,
        returnQty: null,
        materialStatus: null,
        supplierCode: null,
        supplierName: null,
        batch: null,
        price: null,
        taxPrice: null,
        allAmount: null,
        taxRate: null,
        comment: null,
        deliveryTime: [],
        createTime: [],
        erpMark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getPurchaseOrderDetailPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        detailErpId: undefined,
        inStockId: undefined,
        erpInStockOrderId: undefined,
        erpInStockOrderNo: undefined,
        detailNumber: undefined,
        materialNo: undefined,
        materialName: undefined,
        materialQty: undefined,
        materialModels: undefined,
        unitName: undefined,
        shouldReceiveAmount: undefined,
        receiveQty: undefined,
        returnQty: undefined,
        materialStatus: undefined,
        supplierCode: undefined,
        supplierName: undefined,
        batch: undefined,
        price: undefined,
        taxPrice: undefined,
        allAmount: undefined,
        taxRate: undefined,
        comment: undefined,
        deliveryTime: undefined,
        erpMark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有ERP采购订单明细数据项?').then(() => {
        this.exportLoading = true
        return exportPurchaseOrderDetailExcel(params)
      }).then(response => {
        this.$download.excel(response, 'ERP采购订单明细.xls')
        this.exportLoading = false
      }).catch(() => {})
    },
    dialogOpen() {
      this.getList()
    },
    // 交期确认
    deliveryEnsure() {
      if (this.currentRows.length <= 0) {
        this.$modal.msgWarning('请先选择您要确认交期的数据!')
        return
      }
      this.$refs.deliveryConfimation.open = true
    },
    // 表格选中
    tableSelected(val) {
      this.currentRows = val
    },
    ensureComplete() {
      this.getList()
      this.open = false
    },
    // 生成送货通知单
    handleGenerate() {
      if (this.currentRows.length <= 0) {
        this.$modal.msgWarning('请选择您要生成送货通知单的数据!')
        return
      }
      this.$refs.generateDelivery.open = true
    }
  }
}
</script>

