<template>
  <div class="app-container">
    <div class="head-container">
      <el-input
        v-model="productName"
        clearable
        placeholder="产品名称"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      />
      <!-- <el-input
        v-model="tgName"
        clearable
        placeholder="投顾名称、账号"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      /> -->
      <el-autocomplete
        v-model="tgSearchName"
        class="filter-item"
        clearable
        :fetch-suggestions="querySearchTg"
        placeholder="投顾名称、账号"
        :trigger-on-focus="false"
        @select="handleSelectTg"
      />
      <el-input
        v-model="orderId"
        clearable
        placeholder="订单编号"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      />
      <el-input
        v-model="mobile"
        clearable
        placeholder="客户手机号"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      />
      <el-input
        v-model="fundAccount"
        clearable
        placeholder="资金账号"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      />
      <el-input
        v-model="payId"
        clearable
        placeholder="微信支付流水号"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      />
      <!-- <el-input
        v-model="sdOrderId"
        clearable
        placeholder="思迪订单id"
        style="width: 200px; padding-right: 10px"
        class="filter-item"
        @keyup.enter.native="toQuery(1)"
      /> -->
      <el-autocomplete
        v-model="custOrgNameSearch"
        class="filter-item"
        clearable
        :fetch-suggestions="querySearchCust"
        placeholder="客户营业部"
        :trigger-on-focus="false"
        @select="handleSelectCust"
      />
      <el-select
        v-model="orderStatus"
        clearable
        placeholder="订单状态"
        class="filter-item"
        style="width: 130px; padding-right: 5px"
      >
        <el-option
          v-for="item in ORDER_STATUS_OPTIONS"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="payType"
        clearable
        placeholder="支付方式"
        class="filter-item"
        style="width: 275px; padding-right: 5px"
        multiple
      >
        <el-option
          v-for="item in PAY_TYPE_OPTIONS"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="productType"
        clearable
        placeholder="产品类型"
        class="filter-item"
        style="width: 140px; padding-right: 5px"
      >
        <el-option
          v-for="item in PRODUCT_TYPE_OPTIONS"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="valid"
        clearable
        placeholder="权限状态"
        class="filter-item"
        style="width: 120px; padding-right: 5px"
      >
        <el-option
          v-for="item in validList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="isRenew"
        clearable
        placeholder="是否续订"
        class="filter-item"
        style="width: 120px; padding-right: 5px"
      >
        <el-option
          v-for="item in isRenewList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="coupon"
        clearable
        placeholder="是否用优惠券"
        class="filter-item"
        style="width: 150px; padding-right: 5px"
      >
        <el-option
          v-for="item in couponList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="iPlat"
        clearable
        placeholder="渠道"
        class="filter-item"
        style="width: 150px; padding-right: 5px"
      >
        <el-option
          v-for="item in iPlatList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      退款时间限制:
      <el-switch
        v-model="limit"
        style="margin-left:10px"
        active-color="#13ce66"
        inactive-color="#ff4949"
        active-text="开"
        inactive-text="关"
        @change="changeLimit"
      />
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        append-to-body
      >
        <span
          v-if="limit"
        ><i class="el-icon-info" style="color: #fcb82c;" />
          是否关闭退款时间限制?关闭后,用户可随时申请退款</span>
        <span
          v-else
        ><i class="el-icon-info" style="color: #fcb82c;" />
          是否开启退款时间限制?开启后,用户只能在订单开头后5个交易日内申请退款</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <br>
      <div class="head-down">
        <span style="margin-right:10px;">时间筛选 :</span>
        <el-select
          v-model="dateType"
          clearable
          placeholder="时间类型"
          class="filter-item"
          style="width: 120px; padding-right: 5px"
        >
          <el-option
            v-for="item in dateTypeList"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
        <el-date-picker
          v-model="date"
          style="padding-right: 10px"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          class="filter-item"
          value-format="yyyyMMdd"
        />
        <!-- icon="el-icon-search" -->
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          size="mini"
          type="primary"
          @click="toQuery(1)"
        >查询</el-button>
        <el-button
          :loading="downloadLoading"
          size="mini"
          class="filter-item"
          type="warning"
          @click="toQuery(2)"
        >导出</el-button>
      </div>
    </div>
    <div class="tips">
      <i class="el-icon-warning" />
      连续10个交易日低于签约门槛的佣金支付订单，<span
        @click="$router.push('/order/belowThresholdOrder')"
      >点击查看</span>
    </div>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      border
      :data="data"
      size="small"
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" fixed="left" align="center" />
      <el-table-column
        prop="orderId"
        :show-overflow-tooltip="true"
        fixed="left"
        label="订单编号(商户订单号)"
        width="140"
        align="center"
      />
      <el-table-column prop="iPlat" label="渠道" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.iPlat | getChannel }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="payId"
        :show-overflow-tooltip="true"
        label="订单号"
        align="center"
        width="140"
      />
      <el-table-column prop="custName" label="客户姓名" align="center" />
      <el-table-column prop="custOrgName" label="客户营业部" align="center" />
      <el-table-column
        prop="fundAccount"
        label="资金账号(用户标识)"
        align="center"
        width="130"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.fundAccount == scope.row.mobile
              ? "--"
              : scope.row.fundAccount
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="手机号"
        align="center"
        width="110"
      />
      <el-table-column
        prop="custRiskLevel"
        :show-overflow-tooltip="true"
        label="客户风测等级"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ getRiskLevel(scope.row.custRiskLevel) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="productName"
        :show-overflow-tooltip="true"
        label="产品名称"
        align="center"
      />
      <el-table-column
        prop="productType"
        :show-overflow-tooltip="true"
        label="产品类型"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.productType | getProductType }}</span>
        </template>
      </el-table-column>
      <!-- 风险等级 -->
      <el-table-column
        prop="productRiskLevel"
        :show-overflow-tooltip="true"
        label="产品风险等级"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ getProductRiskLevel(scope.row.productRiskLevel) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投顾账号" align="center" prop="tgAccount" />
      <el-table-column label="投顾名称" align="center" prop="tgName" />
      <el-table-column label="投顾营业部" align="center" prop="tgOrgName" />
      <el-table-column
        prop="payType"
        :show-overflow-tooltip="true"
        label="支付方式"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ getPayType(scope.row.payType) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="productPrice" label="产品定价" align="center">
        <template slot-scope="scope">
          <span
            v-if="scope.row.payType === 13"
          >{{ scope.row.commissionRate }}‰</span>
          <span v-else>{{ (scope.row.productPrice / 100).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券ID" align="center" prop="couponId" />
      <el-table-column label="优惠券名称" align="center" prop="couponName" />
      <el-table-column label="优惠金额/天数" align="center" prop="couponValue" />
      <el-table-column prop="payTotal" label="支付金额" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13">--</span>
          <span v-else>{{ (scope.row.payTotal / 100).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="cancelAmount" label="退款金额" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.orderStatus === 100">{{
            (scope.row.cancelAmount / 100).toFixed(2)
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        :show-overflow-tooltip="true"
        label="订单状态"
        align="center"
        min-width="85"
      >
        <template slot-scope="scope">
          <span>{{
            getOrderStatus(scope.row.orderStatus, scope.row.payType)
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="supportStatus"
        label="信用账号提拥状态"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.payType === 13 ? scope.row.supportStatus : '' | getCommissionStatus }}
        </template>
      </el-table-column>
      <el-table-column
        prop="conquerStatus"
        label="信用账号降拥状态"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.payType === 13 && scope.row.orderStatus === 100 ? scope.row.conquerStatus : '' | getCommissionStatus }}
        </template>
      </el-table-column> -->
      <el-table-column
        prop="refundStatus"
        :show-overflow-tooltip="true"
        label="退款状态"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13">--</span>
          <span v-else>{{ getRefundStatus(scope.row.refundStatus) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="refundStatus"
        :show-overflow-tooltip="true"
        label="解约状态"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13">{{ getRefundStatus(scope.row.refundStatus, 13) }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="isRenew"
        :show-overflow-tooltip="true"
        label="是否续订"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.isRenew === 1 ? "续订" : "新订" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="生效时间"
        align="center"
        prop="moduleStart"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.moduleStart) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="到期时间"
        align="center"
        prop="moduleEnd"
        width="90"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.moduleEnd) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请退款/解约时间"
        align="center"
        prop="applyRefundTime"
        width="90"
      >
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.payType === 13 || !scope.row.applyRefundTime">--</span>
          <span v-else>{{ parseTime(scope.row.applyRefundTime) }}</span> -->
          {{
            scope.row.applyRefundTime
              ? parseTime(scope.row.applyRefundTime)
              : "--"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="退款/解约成功时间"
        align="center"
        prop="approveRefundTime"
        width="90"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.orderStatus === 100 && scope.row.approveRefundTime"
          >{{ parseTime(scope.row.approveRefundTime) }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="申请解约时间"
        align="center"
        prop="applyRefundTime"
        width="90"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13 && scope.row.applyRefundTime">{{ parseTime(scope.row.applyRefundTime) }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        label="解约成功时间"
        align="center"
        prop="approveRefundTime"
        width="90"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13 && scope.row.approveRefundTime">{{ parseTime(scope.row.approveRefundTime) }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="applyReason"
        :show-overflow-tooltip="true"
        label="解约理由"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.payType === 13 && scope.row.applyReason">{{ scope.row.applyReason }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="agreementNo"
        :show-overflow-tooltip="true"
        label="天威协议编号"
        align="center"
      >
      </el-table-column> -->
      <el-table-column
        prop="valid"
        :show-overflow-tooltip="true"
        label="权限状态"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.valid === 1 ? "有效" : "无效" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="applyReason"
        :show-overflow-tooltip="true"
        label="退款理由"
        align="center"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.payType === 13 || !scope.row.applyReason"
          >--</span>
          <span v-else>{{ scope.row.applyReason }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="examineReason"
        :show-overflow-tooltip="true"
        label="退款审核说明"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.refundStatus === 210">{{
            scope.row.examineReason
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="tyReason"
        :show-overflow-tooltip="true"
        label="签约审核理由"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="referNo"
        :show-overflow-tooltip="true"
        label="推荐人编号"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="referName"
        :show-overflow-tooltip="true"
        label="推荐人姓名"
        align="center"
      >
      </el-table-column> -->
      <el-table-column
        prop="ascriptionId"
        :show-overflow-tooltip="true"
        label="归属关系ID"
        align="center"
      />
      <el-table-column
        prop="ascriptionMobile"
        :show-overflow-tooltip="true"
        label="归属关系手机号"
        align="center"
      />
      <el-table-column
        prop="productSponsorId"
        :show-overflow-tooltip="true"
        label="产品推荐人ID"
        align="center"
      />
      <el-table-column
        prop="productSponsorMobile"
        :show-overflow-tooltip="true"
        label="产品推荐人手机号"
        align="center"
      />
      <el-table-column label="操作" align="center" width="80" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-show="
              scope.row.refundStatus === 180 && scope.row.orderStatus === 95
            "
            type="text"
            size="mini"
            class="inline-btn"
            @click="reviewed(scope.row)"
          >{{ scope.row.payType === 13 ? "解约" : "退款" }}审核</el-button>
          <el-button
            v-show="
              [220].includes(scope.row.orderStatus) &&
                scope.row.payType !== 14 &&
                scope.row.payType !== 13
            "
            type="text"
            size="mini"
            class="inline-btn"
            style="margin-left: 0px;"
            @click="refund(scope.row)"
          >直接退款</el-button>
          <el-button
            v-show="
              [95, 220].includes(scope.row.orderStatus) &&
                scope.row.payType === 13
            "
            type="text"
            size="mini"
            class="inline-btn"
            style="margin-left: 0px;"
            @click="refund(scope.row)"
          >直接解约</el-button>
          <!-- <el-button
            v-show="[170].includes(scope.row.orderStatus)"
            type="text"
            size="mini"
            class="inline-btn"
            style="margin-left: 0px;"
            @click="signing(scope.row)"
          >签约审核</el-button> -->
        </template>
      </el-table-column>
      <!-- v-show="scope.row.orderStatus === 220 && scope.row.valid === 1" -->
      <el-table-column
        prop="title"
        fixed="right"
        :show-overflow-tooltip="true"
        label="签约协议"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            class="inline-btn"
            @click="agreement(scope.row)"
          >协议详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
    <refundApplication ref="refund" />
    <refundLication ref="directRefund" />
    <agreementDetails ref="details" />
    <SigningReview ref="signingReview" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { parseTime } from "@/utils/index";
import keyBy from "lodash/keyBy";
import refundApplication from "./refundApplication.vue";
// vip退款
import refundLication from "./refundLication.vue";
import agreementDetails from "./agreementDetails.vue";
// 签约审核
import SigningReview from "./signingReview.vue";
import { queryOrderList, getDeptList, queryContractList } from "@/api/order";
import { tgList } from "@/api/adviser";
// import { DAY_TYPE } from '@/utils/options'
import { queryPopContent, updateContent } from "@/api/pop";
import {
  PRODUCT_TYPE_OPTIONS,
  PAY_TYPE_OPTIONS,
  ORDER_STATUS_OPTIONS
} from "@/utils/options.js";

export default {
  name: "",
  components: {
    refundApplication,
    agreementDetails,
    refundLication,
    SigningReview
  },
  data() {
    return {
      total: 1,
      page: 1,
      size: 10,
      loading: false,
      date: [],
      title: "",
      productName: "",
      tgName: "",
      advisorId: null,
      userOrgId: null,
      tgSearchName: "",
      custOrgName: "",
      custOrgNameSearch: "",
      orderId: "",
      mobile: "",
      fundAccount: "",
      sdOrderId: "",
      orderStatus: "",
      SingleStatusList: [
        {
          key: 70,
          value: "签约失败"
        },
        {
          key: 95,
          value: "解约中"
        },
        {
          key: 100,
          value: "已解约"
        },
        {
          key: 180,
          value: "新订单"
        },
        {
          key: 220,
          value: "已签约"
        }
      ],
      payType: [],
      payTypeList: [
        {
          key: 1,
          value: "支付宝"
        },
        {
          key: 5,
          value: "微信"
        },
        {
          key: 13,
          value: "佣金支付"
        },
        {
          key: 14,
          value: "保证金"
        }
      ],
      productType: "",
      productTypeList: [
        {
          key: 1,
          value: "观点包"
        },
        {
          key: 2,
          value: "单篇观点"
        },
        {
          key: 7,
          value: "投资组合"
        },
        {
          key: 8,
          value: "股票池"
        },
        {
          key: 9,
          value: "套餐产品"
        },
        {
          key: 32,
          value: "课程"
        },
        {
          key: 31,
          value: "投资课堂"
        },
        {
          key: 21,
          value: "三方产品-增值产品"
        },
        // {
        //   key: 22,
        //   value: '三方产品-课程'
        // },
        {
          key: 23,
          value: "三方产品-ETF专区"
        },
        {
          key: 24,
          value: "三方产品-选股工具"
        },
        {
          key: 25,
          value: "小飞机理财"
        }
      ],
      valid: "",
      validList: [
        {
          key: 1,
          value: "有效"
        },
        {
          key: 2,
          value: "无效"
        }
      ],
      isRenew: "",
      isRenewList: [
        {
          key: 1,
          value: "新订"
        },
        {
          key: 2,
          value: "续订"
        }
      ],
      coupon: "",
      couponList: [
        {
          key: 1,
          value: "使用优惠券"
        },
        {
          key: 2,
          value: "未使用优惠券"
        },
        {
          key: 0,
          value: "全部的订单"
        }
      ],
      iPlat: "",
      iPlatList: [{ value: 1, label: "APP" }, { value: 2, label: "小程序" }],
      dateType: "",
      dateTypeList: [
        {
          key: 1,
          value: "下单时间"
        },
        {
          key: 2,
          value: "支付时间"
        },
        {
          key: 3,
          value: "生效时间"
        },
        {
          key: 4,
          value: "到期时间"
        }
      ],
      status: "",
      downloadLoading: false,
      queryTypeOptions: [{ key: 3, value: "上架" }, { key: 5, value: "下架" }],
      payId: "",
      data: [],
      deriveData: [],
      delLoading: false,
      limit: true,
      dialogVisible: false,
      PRODUCT_TYPE_OPTIONS,
      PAY_TYPE_OPTIONS,
      ORDER_STATUS_OPTIONS
    };
  },
  computed: {
    ...mapGetters(["user"])
  },
  watch: {
    deriveData: {
      handler(newName) {
        if (newName !== [] || newName) {
          this.download();
        }
      },
      deep: true
    },
    tgSearchName: {
      handler(newName) {
        if (newName.length < 6) {
          this.tgName = this.tgSearchName;
        }
      },
      deep: true
    },
    custOrgNameSearch: {
      handler(newName) {
        if (newName.length < 12) {
          this.custOrgName = this.custOrgNameSearch;
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getOrderList();
    this.queryPopContent();
  },

  methods: {
    parseTime,
    getSubString(value) {
      return value.length > 15 ? value.substring(0, 15) + "..." : value;
    },
    // 查询订单
    async getOrderList(arr) {
      const params = {};
      params.offset = (this.page - 1) * this.size;
      params.size = this.size;
      if (arr) {
        params.offset = arr.offset;
        params.size = arr.size;
      }
      // 投顾
      if (this.advisorId || this.advisorId === 0) {
        params.advisorId = this.advisorId;
      }
      // 营业部
      if (this.userOrgId || this.userOrgId === 0) {
        params.userOrgId = this.userOrgId;
      }
      if (params.advisorId === 0) {
        this.data = [];
        this.total = 0;
        return;
      }
      // 产品名称
      if (this.productName) {
        params.productName = this.productName;
      }
      // 微信支付流水号
      if (this.payId) {
        params.payId = this.payId;
      }
      // // 思迪订单id
      // if (this.sdOrderId) {
      //   params.sdOrderId = this.sdOrderId
      // }
      // 订单id
      if (this.orderId) {
        params.orderId = this.orderId;
      }
      // 客户手机号
      if (this.mobile) {
        params.mobile = this.mobile;
      }
      // 资金账号
      if (this.fundAccount) {
        params.fundAccount = this.fundAccount;
      }
      // 客户营业部
      // params.userOrgId = this.userOrgId
      // 订单状态
      if (this.orderStatus) {
        params.orderStatus = this.orderStatus;
      }
      // 支付方式
      if (this.payType.length > 0) {
        params.payType = this.payType;
      }
      // 产品类型
      if (this.productType) {
        params.productType = this.productType;
      }
      // 权限状态
      if (this.valid) {
        params.valid = this.valid;
      }
      // 是否续订
      if (this.isRenew) {
        params.isRenew = this.isRenew;
      }
      // 是否用优惠券
      if (this.coupon) {
        params.coupon = this.coupon;
      }
      // 渠道
      if (this.iPlat) {
        params.iPlat = this.iPlat;
      }
      let timeNum = 0;
      // 时间
      if (this.date && this.date.length !== 0) {
        params.beginDate = this.date[0];
        params.endDate = this.date[1];
        if (!this.dateType) {
          timeNum = 1;
        }
      }
      // 查询时间类型
      if (this.dateType) {
        params.dateType = this.dateType;
        if (!this.date || this.date.length === 0) {
          timeNum = 2;
        }
      }
      if (timeNum === 1) {
        return this.$message.error("请选择时间类型");
      }
      if (timeNum === 2) {
        return this.$message.error("请选择时间区间");
      }
      this.loading = true;
      const data = await queryOrderList(params);
      if (data.code === 0) {
        this.loading = false;
        if (arr) {
          if (arr.status) {
            this.deriveData = data.data.list;
            // console.log(arr.status)
          } else {
            this.data = data.data.list;
            this.total = data.data.total;
          }
        } else {
          this.data = data.data.list;
          this.total = data.data.total;
        }
      } else {
        this.loading = false;
        this.$message.error(data.message);
      }
      // console.log(params)
    },
    // 查询退款时间限制
    async queryPopContent() {
      const { code, data } = await queryPopContent();
      if (code === 0) {
        if (data === null) return;
        this.limit = data.refundTimeLimit === 1;
      }
    },
    changeLimit() {
      this.dialogVisible = true;
      this.limit = !this.limit;
    },
    async submit() {
      const { code, message } = await updateContent({
        refundTimeLimit: this.limit ? 2 : 1
      });
      if (code === 0) {
        this.$message.success("修改退款时间限制成功!");
        this.dialogVisible = false;
        this.queryPopContent();
      } else {
        this.$message.error(message);
      }
    },
    cancel() {
      this.dialogVisible = false;
    },
    // 状态
    getStatus(status) {
      const statusObj = keyBy(this.queryTypeOptions, "key")[status];
      return statusObj ? statusObj.display_name : "";
    },
    async toQuery(status) {
      if (status) {
        this.page = 1;
      }
      this.userOrgId = null;
      this.advisorId = null;
      // console.log(status)
      const all = {};
      if (status === 2) {
        all.offset = 0;
        all.size = 10000;
        all.status = 1;
      } else {
        all.offset = (this.page - 1) * this.size;
        all.size = this.size;
        all.status = 0;
      }
      if (this.custOrgName) {
        const params = {};
        params.keyword = this.custOrgName;
        const data = await getDeptList(params);
        if (data.code === 0) {
          if (data.data.length === 0 || data.data.length > 1) {
            all.userOrgId = 0;
            this.userOrgId = 0;
          } else {
            all.userOrgId = data.data[0].id;
            this.userOrgId = data.data[0].id;
          }
        } else {
          this.$message.error(data.message);
        }
        // console.log(data)
      }
      if (this.tgName) {
        const params = {};
        params.current = 1;
        params.size = 10;
        params.status = 3;
        if (this.isNumber(this.tgName)) {
          params.staffNo = this.tgName;
        } else {
          params.name = this.tgName;
        }
        const data = await tgList(params);
        if (data.code === 0) {
          if (data.data.list.length === 0 || data.data.list.length > 1) {
            all.advisorId = 0;
            this.advisorId = 0;
          } else {
            all.advisorId = data.data.list[0].id;
            this.advisorId = data.data.list[0].id;
          }
          // console.log(all.advisorId)
        } else {
          this.$message.error(data.message);
        }
        // console.log(data.data.list[0].id)
      }
      this.getOrderList(all);
    },
    // 分页
    sizeChange(e) {
      this.page = 1;
      this.size = e;
      this.getOrderList();
    },
    pageChange(e) {
      this.page = e;
      this.getOrderList();
    },
    // 审核退款
    reviewed(row) {
      // console.log(row)
      // this.$confirm('是否要进行退款申请?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      const _this = this.$refs.refund;
      _this.id = row.orderId;
      _this.createTime = row.createTime;
      _this.form.applyReason = row.applyReason;
      _this.form.cancelAmount = row.cancelAmount;
      _this.data = row;
      _this.dialog = true;
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消申请'
      //   })
      // })
    },
    // 签约审核
    signing(row) {
      const _this = this.$refs.signingReview;
      _this.id = row.orderId;
      _this.createTime = row.createTime;
      _this.data = row;
      _this.dialog = true;
    },
    // vip直接退款
    refund(row) {
      const _this = this.$refs.directRefund;
      _this.id = row.orderId;
      _this.createTime = row.createTime;
      _this.data = row;
      _this.dialog = true;
    },
    // 协议详情(有弹窗)
    // agreement(row) {
    //   console.log(row)
    //   this.$confirm('是否要查看协议详情?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     const _this = this.$refs.details
    //     _this.id = row.orderId
    //     _this.createTime = row.createTime
    //     _this.data = row
    //     _this.dialog = true
    //   }).catch((e) => {
    //     console.log(e)
    //   })
    // },
    // 协议详情(无弹窗)
    async agreement(row) {
      const _this = this.$refs.details;
      const params = {};
      params.agreementNo = row.agreementNo;
      // params.sdOrderId = row.sdOrderId
      if (row.agreementNo.indexOf("-") !== -1) {
        return this.$message.error("该历史订单没有找到原合同");
      }
      const data = await queryContractList(params);
      if (data.code === 0) {
        // _this.createTime = row.createTime
        // data.data.map((v, k) => {
        //   v.status = k + 1
        // })
        // console.log(data.data)
        if (!data.data.length) {
          return this.$message.error("暂无协议!");
        }
        _this.data = data.data;
        _this.dialog = true;
        // if (!data.data[0].htid) {
        //   return this.$message.error('天威电子协议生成中，如有需要请联系天威相关人员。')
        // } else {
        //   _this.dialog = true
        // }
        // if (row.sdOrderId) {
        //   if (data.data.length === row.agreementNo.split(',').length) {
        //     _this.dialog = true
        //   } else {
        //     this.$message.error('天威电子协议生成中，如有需要请联系天威相关人员。')
        //   }
        // } else {
        //   if (data.data.length === 3) {
        //     _this.dialog = true
        //   } else {
        //     this.$message.error('天威电子协议生成中，如有需要请联系天威相关人员。')
        //   }
        // }
      } else {
        _this.error = data.message;
        this.$message.error(data.message);
      }
    },
    toPrice(row) {
      let price = "";
      if (row.feesList) {
        row.feesList.forEach((val, index) => {
          const fen = val.price / 100;
          // switch (val.period) {
          //   case 0:
          //     price += '免费永久,单笔提佣'
          //     break
          //   case 7:
          //     price = price + fen + '元/周'
          //     break
          //   case 14:
          //     price = price + fen + '元/月'
          //     break
          //   case 16:
          //     price = price + fen + '元/季度'
          //     break
          //   case 19:
          //     price = price + fen + '元/半年'
          //     break
          //   case 20:
          //     price = price + fen + '元/年'
          //     break
          //   case 99:
          //     price = price + fen + '元/篇，收费永久,单篇收费'
          //     break
          //   default:
          //     break
          // }
          switch (val.period) {
            // case 0:
            //   price += '免费永久,单笔提佣'
            //   break
            case 1:
              price = price + fen + "元/周";
              break;
            case 2:
              price = price + fen + "元/月";
              break;
            case 3:
              price = price + fen + "元/季度";
              break;
            case 4:
              price = price + fen + "元/半年";
              break;
            case 5:
              price = price + fen + "元/年";
              break;
            case 0:
              price = price + fen + "元/篇，收费永久,单篇收费";
              break;
            default:
              break;
          }
          if (index + 1 < row.feesList.length) {
            price += "；";
          }
        });
      }
      return price;
    },
    getRiskLevel(row) {
      switch (row) {
        case 1:
          return "C1保守型";
        case 2:
          return "C2谨慎型";
        case 3:
          return "C3稳健型";
        case 4:
          return "C4积极型";
        case 5:
          return "C5激进型";
        default:
          break;
      }
    },
    getProductRiskLevel(row) {
      switch (row) {
        case 1:
          return "低风险";
        case 2:
          return "中低风险";
        case 3:
          return "中风险";
        case 4:
          return "中高风险";
        case 5:
          return "高风险";
        default:
          return "";
      }
    },
    getProductType(status) {
      const statusObj = keyBy(this.PRODUCT_TYPE_OPTIONS, "key")[status];
      return statusObj ? statusObj.value : "";
    },
    getPayType(status) {
      const statusObj = keyBy(this.PAY_TYPE_OPTIONS, "key")[status];
      return statusObj ? statusObj.value : "";
    },
    getOrderStatus(status, type) {
      if (type === 13) {
        const statusObj = keyBy(this.SingleStatusList, "key")[status];
        return statusObj ? statusObj.value : "";
      } else {
        const statusObj = keyBy(this.ORDER_STATUS_OPTIONS, "key")[status];
        return statusObj ? statusObj.value : "";
      }
    },
    getRefundStatus(row, type = 1) {
      let mould = "";
      if (type === 13) {
        mould = "解约";
      } else {
        mould = "退款";
      }
      switch (row) {
        case 180:
          return `申请${mould}`;
        case 200:
          return `${mould}审核通过`;
        case 210:
          return `${mould}失败`;
        case 220:
          return `已${mould}`;
        default:
          return `--`;
      }
    },
    isNumber(val) {
      // 非负浮点数
      var regPos = /^\d+(\.\d+)?$/;
      // 负浮点数
      var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
      if (regPos.test(val) || regNeg.test(val)) {
        return true;
      } else {
        return false;
      }
    },
    // 投顾远程框
    async querySearchTg(queryString, cb) {
      // console.log(queryString)
      const arr = [];
      const params = {};
      params.current = 1;
      params.size = 100000;
      params.status = 3;
      if (this.isNumber(queryString)) {
        params.staffNo = queryString;
      } else {
        params.name = queryString;
      }
      const data = await tgList(params);
      if (data.code === 0) {
        for (let i = 0; i < data.data.list.length; i++) {
          const element = data.data.list[i];
          const value = `${element.name}[${element.staffNo}]`;
          arr.push({ value, ...element });
        }
      } else {
        this.$message.error(data.message);
      }
      // console.log(data)
      // 调用 callback 返回建议列表的数据
      cb(arr);
    },
    handleSelectTg(item) {
      this.tgName = item.name;
    },
    // 营业部远程框
    async querySearchCust(queryString, cb) {
      // console.log(queryString)
      const arr = [];
      const params = {};
      params.keyword = queryString;
      const data = await getDeptList(params);
      if (data.code === 0) {
        for (let i = 0; i < data.data.length; i++) {
          const element = data.data[i];
          const value = element.name;
          arr.push({ value, ...element });
        }
      } else {
        this.$message.error(data.message);
      }
      // console.log(data)
      // 调用 callback 返回建议列表的数据
      cb(arr);
    },
    handleSelectCust(item) {
      this.custOrgName = item.name;
    },
    // 导出
    async download() {
      // return
      this.downloadLoading = true;
      // console.log(this.deriveData)
      import("@/utils/export2Excel").then(excel => {
        const tHeader = [
          "订单编号(商户订单号)",
          "渠道",
          "订单号",
          "客户姓名",
          "客户营业部",
          "资金账号(用户标识)",
          "手机号",
          "客户风测等级",
          "产品名称",
          "产品类型",
          "产品风险等级",
          "投顾账号",
          "投顾名称",
          "投顾营业部",
          "支付方式",
          "产品定价",
          "优惠券ID",
          "优惠券名称",
          "优惠金额",
          "支付金额",
          "退款金额",
          "订单状态",
          "退款状态",
          "是否续订",
          "下单时间",
          "支付时间",
          "生效时间",
          "到期时间",
          "申请退款时间",
          "退款成功时间",
          "权限状态",
          "退款理由",
          "退款审核说明",
          "归属关系ID",
          "归属关系手机号",
          "产品推荐人ID",
          "产品推荐人手机号"
        ];
        const filterVal = [
          "orderId",
          "channel",
          "payId",
          "custName",
          "custOrgName",
          "fundAccount",
          "mobile",
          "custRiskLevel",
          "productName",
          "productType",
          "productRiskLevel",
          "tgAccount",
          "tgName",
          "tgOrgName",
          "payType",
          "productPrice",
          "couponId",
          "couponName",
          "couponValue",
          "payTotal",
          "cancelAmount",
          "orderStatus",
          "refundStatus",
          "isRenew",
          "createTime",
          "payTime",
          "moduleStart",
          "moduleEnd",
          "applyRefundTime",
          "approveRefundTime",
          "valid",
          "applyReason",
          "examineReason",
          "ascriptionId",
          "ascriptionMobile",
          "productSponsorId",
          "productSponsorMobile"
        ];
        console.log();
        const data = this.formatJson(filterVal, this.deriveData);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "订单"
        });
        this.downloadLoading = false;
      });
    },
    // 数据转换
    formatJson(filterVal, jsonData) {
      console.log(filterVal, jsonData);
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "channel") {
            return "APP";
          }
          // if (!['isRenew', 'productPrice', 'payTotal', 'refundStatus1', 'applyRefundTime1', 'approveRefundTime1', 'applyReason1'].includes(j) && !v[j]) {
          //   return '--'
          // }
          if (
            j === "createTime" ||
            j === "payTime" ||
            j === "moduleStart" ||
            j === "moduleEnd"
          ) {
            return parseTime(v[j]);
          } else if (j === "applyRefundTime") {
            if (v["payType"] === 13 || !v[j]) {
              return "--";
            } else {
              return parseTime(v[j]);
            }
          } else if (j === "applyRefundTime1" || j === "approveRefundTime1") {
            if (v["payType"] === 13) {
              if (j === "applyRefundTime1") {
                if (v["applyRefundTime"]) {
                  return parseTime(v["applyRefundTime"]);
                } else {
                  return "--";
                }
              } else {
                if (v["approveRefundTime"]) {
                  return parseTime(v["approveRefundTime"]);
                } else {
                  return "--";
                }
              }
            } else {
              return "--";
            }
          } else if (j === "applyReason") {
            if (v["payType"] === 13) {
              return "--";
            } else {
              return v[j];
            }
          } else if (j === "applyReason1") {
            if (v["payType"] === 13 && v["applyReason"]) {
              return v["applyReason"];
            } else {
              return "--";
            }
          } else if (j === "custRiskLevel") {
            return this.getRiskLevel(v[j]);
          } else if (j === "productType") {
            return this.getProductType(v[j]);
          } else if (j === "productRiskLevel") {
            return this.getProductRiskLevel(v[j]);
          } else if (j === "payType") {
            return this.getPayType(v[j]);
          } else if (j === "orderStatus") {
            return this.getOrderStatus(v[j], v["payType"]);
          } else if (j === "refundStatus") {
            if (v["payType"] === 13) {
              return "--";
            } else {
              return this.getRefundStatus(v[j], v["payType"]);
            }
          } else if (j === "refundStatus1") {
            if (v["payType"] === 13) {
              return this.getRefundStatus(v["refundStatus"], v["payType"]);
            } else {
              return "--";
            }
          } else if (j === "isRenew") {
            return v[j] === 1 ? "续订" : "新订";
          } else if (j === "valid") {
            return v[j] === 1 ? "有效" : "无效";
          } else if (j === "productPrice" || j === "payTotal") {
            if (v["payType"] === 13) {
              if (j === "productPrice") {
                return v["commissionRate"] + "‰";
              } else {
                return "--";
              }
            } else {
              return (v[j] / 100).toFixed(2);
            }
          } else if (j === "fundAccount") {
            return v[j] === v["mobile"] ? "--" : v[j];
          } else if (j === "cancelAmount") {
            if (v["orderStatus"] === 100) {
              return (v[j] / 100).toFixed(2);
            } else {
              return "--";
            }
          } else if (j === "approveRefundTime") {
            if (v["orderStatus"] === 100) {
              return parseTime(v[j]);
            } else {
              return "--";
            }
          } else {
            return v[j];
          }
        })
      );
    },
    // 风险等级
    getRisk(riskLevel) {
      switch (riskLevel) {
        case 0:
          return "低风险";
        case 1:
          return "低风险";
        case 2:
          return "中低风险";
        case 3:
          return "中风险";
        case 4:
          return "中高风险";
        case 5:
          return "高风险";
      }
    }
  }
};
</script>

<style scoped lang="scss">
.tips {
  margin: 15px 0;
  font-size: 16px;
  i {
    color: #e6a23c;
  }
  span {
    color: #409eff;
    text-decoration: underline;
    cursor: pointer;
  }
}
.compress {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.el-pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
::v-deep.el-input__suffix {
  margin-right: 3px;
}
.head-down {
  display: flex;
  align-items: baseline;
}
</style>
