<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    v-if="visible"
    :visible.sync="visible"
    :title="title"
    :append-to-body="true"
    :close-on-click-modal="false"
    width="70%"
    :maxable="true"
    :resizable="true"
    @close="cancel"
  >
    <div class="switch">
      <div class="switch_left">
        <ul>
          <li
            v-for="item in tabOptions"
            :key="item.key"
            :class="{ active: activeComp == item.key }"
            @click="handleTag(item.key)"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </div>
    <div v-show="activeComp === 'main'">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <headerTitle title="退货信息"></headerTitle>
        <el-row>
          <el-col :span="12">
            <el-form-item label="退货类型：">
              <el-select
                v-model="form.returnSourceType"
                disabled
                style="width: 100%"
              >
                <el-option :value="3" label="销售发货退货"></el-option>
                <el-option :value="1" label="售后退货"></el-option>
                <el-option :value="2" label="受托收货退货"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="客户名称：" prop="contactName">
              <el-input v-model="form.contactName" disabled></el-input>
            </el-form-item>
            <el-form-item label="退货方式：">
              <el-input v-model="form.typeName" disabled></el-input>
            </el-form-item>
            <el-form-item
              label="赔偿金额："
              prop="redressAmount"
              v-show="['30', '50'].includes(form.type)"
            >
              <el-input v-model="form.redressAmount" disabled>
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="总金额：" prop="totalAmount">
              <el-input v-model="form.totalAmount" disabled>
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <!-- <el-form-item label="计价方式：">
              <el-select v-model="form.pricingWay" disabled style="width: 100%">
                <el-option :value="1" label="按数量计费"></el-option>
                <el-option :value="2" label="按重量计费"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="备注：" prop="remark">
              <el-input
                :rows="4"
                type="textarea"
                v-model="form.remark"
                disabled
              >
              </el-input>
            </el-form-item>
            <el-form-item label="回执附件：" prop="sendFiles">
              <fileMain v-model="row.repliedFiles" type="view"></fileMain>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货编码：" prop="returnNo">
              <el-input v-model="form.returnNo" disabled> </el-input>
            </el-form-item>
            <el-form-item label="受托收货编码：" prop="entrustedReceiveCode">
              <el-input v-model="form.entrustedReceiveCode" disabled>
              </el-input>
            </el-form-item>
            <el-form-item label="发货编码：" prop="sendNo">
              <el-input v-model="form.sendNo" disabled> </el-input>
            </el-form-item>
            <el-form-item prop="orderNo" label="订单编码：">
              <el-input v-model="form.orderNo" disabled> </el-input>
            </el-form-item>

            <el-form-item label="审核状态：" prop="reviewStatus">
              <el-select
                v-model="form.reviewStatus"
                disabled
                style="width: 100%"
              >
                <el-option
                  v-for="item in reviewStatusEnum"
                  :value="item.value"
                  :label="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="制单人：" prop="makerName">
              <el-input v-model="form.makerName" disabled> </el-input>
            </el-form-item>
            <el-form-item label="创建时间：" prop="createTime">
              <el-input v-model="form.createTime" disabled> </el-input>
            </el-form-item>
            <el-form-item label="退货附件：" prop="returnFiles">
              <fileMain v-model="row.returnFiles" type="view"></fileMain>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <headerTitle title="原物品清单" style="margin-top: 30px"></headerTitle>
      <inventorySaleOrderTableDetails
        ref="inventorySaleOrderTableDetailsRef"
      ></inventorySaleOrderTableDetails>
      <headerTitle
        title="退货物品明细"
        v-show="!['40', '50'].includes(form.type) || form.returnSourceType != 1"
      ></headerTitle>
      <ele-pro-table
        ref="table"
        @columns-change="handleColumnChange"
      :cache-key="cacheKeyUrl"
        :needPage="false"
        :columns="columns"
        v-show="!['40', '50'].includes(form.type) || form.returnSourceType != 1"
        :datasource="detailData.productList"
        row-key="id"
      >
      </ele-pro-table>
      <headerTitle
        title="实物赔偿清单"
        style="margin-top: 30px"
        v-show="['20', '40'].includes(form.type)"
      ></headerTitle>
      <inventoryTabledetail
        v-show="['20', '40'].includes(form.type)"
        ref="inventoryTabledetailRef"
        :isSinglePrice="false"
        :isOrderNo="true"
        cacheKeyUrl="eos-saleManage-returnGoods-redressProductListDetail"
      ></inventoryTabledetail>
    </div>
    <out-bound-details
      v-if="activeComp === 'store'"
      ref="outBoundDetailsRef"
    ></out-bound-details>
    <bpmDetail
      :key="activeComp"
      v-if="activeComp === 'bpm' && form.processInstanceId"
      :id="form.processInstanceId"
    ></bpmDetail>
    <customerReturnOrderDetail
      v-if="activeComp === 'cMain'"
      :row="data"
    ></customerReturnOrderDetail>
    <bpmDetail
      :key="activeComp"
      v-if="activeComp === 'cBpm' && data.processInstanceId"
      :id="data.processInstanceId"
    ></bpmDetail>
    <div slot="footer" class="footer">
      <el-button @click="cancel">返回</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import {
    getReturnSaleOrderrecordDetail,
    saleReturnGetByIdAPI
  } from '@/api/saleManage/returnGoods';
  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  import { reviewStatusEnum } from '@/enum/dict';
  import { copyObj } from '@/utils/util';
  import bpmDetail from '@/views/bpm/processInstance/detail.vue';
  import inventoryTabledetail from '@/BIZComponents/inventoryTableDetails.vue';
  import customerReturnOrderDetail from '@/views/saleManage/saleOrder/returnGoods/components/customerReturnOrderDetail.vue';
  import outBoundDetails from '@/BIZComponents/outdetails.vue';
  // import fileMain from '@/components/addDoc/index.vue';
  import { getSendSaleOrderrecordDetailSplit } from '@/api/saleManage/saleordersendrecord';
  import { getPSaleEntrustedReceiveDetailAPI } from '@/api/saleManage/entrustedReceive';
  import inventorySaleOrderTableDetails from '@/views/saleManage/saleOrder/invoice/components/inventoryTableDetails.vue';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import { levelList } from '@/enum/dict.js';

  export default {
    mixins: [dictMixins,tabMixins],
    components: {
      outBoundDetails,
      bpmDetail,
      inventoryTabledetail,
      customerReturnOrderDetail,
      inventorySaleOrderTableDetails,
      // fileMain
    },
    data() {
      return {
        activeComp: 'main',
        tabOptions: [
          { key: 'main', name: '退货单详情' },
          { key: 'bpm', name: '流程详情' },
          // {key: 'store', name: '出库单详情'},
          { key: 'cMain', name: '退货处理单详情' },
          { key: 'cBpm', name: '退货处理单流程详情' }
        ],
        fullscreen: false, //全屏
        cacheKeyUrl: 'eos-saleManage-returnorder-inventoryTable',
        reviewStatusEnum,
        visible: false,
        detailId: '',
        title: '详情',
        row: {},
        activeName: 'base',
        form: {
          orderFiles: [{ name: '222' }]
        },
        data: {},
        rules: {},
        detailData: {},
        productList: [],

        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            minWidth: 200,
            prop: 'orderNo',
            slot: 'orderNo',
            label: '订单编码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'productCode',
            label: '编码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'productName',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center'
          },

          {
            minWidth: 160,
            prop: 'batchNo',
            label: '批次号',
            showOverflowTooltip: true,
            slot: 'batchNo',
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'barcodes',
            label: '发货条码',
            showOverflowTooltip: true,
            slot: 'barcodes',
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'materielDesignation',
            label: '物料代号',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'clientCode',
            label: '客户代号',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 120,
            prop: 'engrave',
            label: '刻码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 160,
            prop: 'packageNo',
            align: 'center',
            label: '包装编码',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'packingQuantity',
            align: 'center',
            label: '包装数量',
            showOverflowTooltip: true
          },
          {
            minWidth: 120,
            prop: 'packingUnit',
            align: 'center',
            label: '包装单位',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'totalCount',
            label: '计量数量',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 150,
            prop: 'measuringUnit',
            label: '计量单位',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            width: 160,
            prop: 'pricingWay',
            label: '计价方式',
            slot: 'pricingWay',
            align: 'center',
            formatter: (row, column) => {
              return row.pricingWay == 1
                ? '按数量计费'
                : row.pricingWay == 2
                ? '按重量计费'
                : '';
            }
          },
          {
            width: 160,
            prop: 'goodsPriceType',
            label: '价格类型',
            formatter: (row, column) => {
              return this.getDictValue('商品价格类型', row.goodsPriceType);
            },
            align: 'center'
          },
          {
            width: 100,
            prop: 'singlePrice',
            label: '单价',
            slot: 'singlePrice',
            align: 'center'
          },
          {
            width: 160,
            prop: 'taxRate',
            label: '税率',
            formatter: (row, column) => {
              return row.taxRate ? row.taxRate + '%' : '';
            },
            align: 'center'
          },
          {
            width: 180,
            prop: 'notaxSinglePrice',
            label: '不含税单价',
            // slot: 'notaxSinglePrice',
            align: 'center'
          },
          {
            width: 100,
            prop: 'totalPrice',
            label: '合计',
            align: 'center'
          },

          {
            minWidth: 120,
            prop: 'receiveTotalWeight',
            label: '重量',
            // slot: 'singleWeight',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            minWidth: 100,
            prop: 'weightUnit',
            label: '重量单位',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'provenance',
            label: '产地',
            slot: 'provenance',
            align: 'center',
            minWidth: 200,
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return row.provenance && row.provenance.length
                ? row.provenance
                    .map((item) => this.getDictValue('产地', item))
                    .join(',')
                : '';
            }
          },
          {
            minWidth: 120,
            prop: 'goodsLevel',
            label: '物品级别',
            formatter: (_row, _column, cellValue) => {
              return levelList.find((item) => item.value == _row.goodsLevel)
                ?.label;
            },
            align: 'center'
          },
          {
            minWidth: 200,
            prop: 'returnReason',
            label: '退货原因',
            slot: 'returnReason',
            align: 'center'
          },

          {
            width: 220,
            prop: 'remark',
            label: '备注',
            slot: 'remark',
            align: 'center'
          }
        ]
      };
    },
    created() {
      this.requestDict('产地');
      this.requestDict('商品价格类型');

    },
    methods: {

      async open(row) {
        //this.form = row;
        this.activeComp='main',
        this.visible = true;
        this.getDetailData(row.id);
        this.detailId = row.id;
        if (row.handleReceiptId) {
          this.data = await saleReturnGetByIdAPI(row.handleReceiptId);
          this.data = {
            processInstanceId: this.data.processInstanceId,
            handleReceiptId: this.data.id,
            sendNo: this.data.sendNo
          };
        }
      },
      handleTag(val) {
        this.activeComp = val;
        if (val == 'store') {
          this.$nextTick(() => {
            this.$refs.outBoundDetailsRef._getInfo(this.form.orderNo);
          });
        }
    
      },
      cancel() {
        this.$nextTick(() => {
          // 关闭后，销毁所有的表单数据
          (this.form = copyObj(this.formDef)),
            (this.otherForm = copyObj(this.otherFormDef)),
            (this.tableBankData = []);
          this.tableLinkData = [];
          this.visible = false;
        });
      },
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      //发货单详情
      async getSendSaleOrderDetail(id) {
        const data = await getSendSaleOrderrecordDetailSplit(id);
        this.$nextTick(() => {
          this.$refs.inventorySaleOrderTableDetailsRef &&
            this.$refs.inventorySaleOrderTableDetailsRef.putTableValue(data.productList);
        });
      },
      //受托收货单详情
      async getPSaleEntrustedReceiveDetailAPI(id) {
        const data = await getPSaleEntrustedReceiveDetailAPI(id);
        this.$nextTick(() => {
          this.$refs.inventorySaleOrderTableDetailsRef &&
            this.$refs.inventorySaleOrderTableDetailsRef.putTableValue(data.productList);
        });
      },

      async getDetailData(id) {
        this.loading = true;
        const data = await getReturnSaleOrderrecordDetail(id);
        if (data.returnSourceType != 2) {
          this.getSendSaleOrderDetail(data.sendId);
        } else {
          this.getPSaleEntrustedReceiveDetailAPI(data.entrustedReceiveId);
        }
        this.loading = false;
        this.form = data;
        if (data) {
          this.$nextTick(() => {
            this.$refs.inventoryTabledetailRef &&
              this.$refs.inventoryTabledetailRef.putTableValue(data);
          });

          this.detailData = data;
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .ele-dialog-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }

  .headbox {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .amount {
      font-size: 14px;
      font-weight: bold;
      margin-right: 20px;
    }
  }
</style>
