<template>
  <div class="app-container">
    <div v-if="showPage == 1">
      <el-radio-group @change="changeRadioFn" v-model="searchForm.examineStatus" size="mini">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button label="SAVE">保存</el-radio-button>
        <el-radio-button label="WAIT">待审核</el-radio-button>
        <el-radio-button label="OK_ONE">初审通过</el-radio-button>
        <el-radio-button label="FAIL_ONE">初审不通过</el-radio-button>
        <el-radio-button label="OK">复核通过</el-radio-button>
        <el-radio-button label="FAIL">复核不通过</el-radio-button>
        <el-radio-button label="CLOSE">已关闭</el-radio-button>
      </el-radio-group>
      <br /><br />
      <!-- 筛选条件 -->
      <div>
        <el-form ref="searchForm" :model="searchForm" label-width="100px" size="mini" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商名称" prop="customerName">
                <el-input v-model="searchForm.customerName" placeholder="请输入经销商名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="返利类型" prop="walletName">
                <el-input v-model="searchForm.walletName" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商编码" prop="customerNumber">
                <el-input v-model="searchForm.customerNumber" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker class="selectStyle" v-model="searchForm.startTime" placeholder="选择日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker class="selectStyle" v-model="searchForm.endTime" placeholder="选择日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="商家已确认" prop="isConfirm">
                <el-radio-group v-model="searchForm.isConfirm">
                  <el-radio-button label="">全部</el-radio-button>
                  <el-radio-button label="true">是</el-radio-button>
                  <el-radio-button label="false">否</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="18">
              <el-form-item label="" class="fr">
                <el-button size="mini" @click="cancelFn">清空</el-button>
                <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix">
        <div class="fl">
          <el-popconfirm v-if="$checkBtnRole('del', $route.meta.roles)" class="delClass" @onConfirm="deleFn" title="这是一段内容确定删除吗？">
            <el-button :disabled="deleList.length < 1" slot="reference" type="danger" icon="el-icon-minus" size="mini">批量删除</el-button>
          </el-popconfirm>
        </div>
        <div class="fr">
          <ExportButton :exUrl="'/rebate/order/export'" :exParams="exParams" />
        </div>
      </div>
      <!-- 列表 -->
      <div class="mymain-container">
        <div class="table">
          <el-table @selection-change="selectionChangeFn" v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe show-summary :summary-method="$getSummaries">
            <el-table-column v-if="$checkBtnRole('del', $route.meta.roles)" :selectable='selectableFn' align="center" type="selection" width="51" show-overflow-tooltip></el-table-column>
            <el-table-column align="left" label="状态" prop="examineStatus" min-width="160" show-overflow-tooltip v-if="!isCustomer">
              <template slot-scope="scope">
                <el-tag size="mini" v-show="scope.row.examineStatus == 'SAVE'">保存</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'WAIT'" type="warning">待审核</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'OK_ONE'" type="success">初审通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'FAIL_ONE'" type="danger">初审不通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'OK'" type="success">复核通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'FAIL'" type="danger">不通过</el-tag>
                <el-tag size="mini" v-show="scope.row.examineStatus == 'CLOSE'" type="info">已关闭</el-tag>
              </template>
            </el-table-column>
            <el-table-column v-if="isCustomer" align="left" label="是否确认" prop="customerIsConfirm" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag v-show="scope.row.customerIsConfirm == true">已确认</el-tag>
                <el-tag v-show="scope.row.customerIsConfirm == false">未确认</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="left" label="返利单号" prop="rebateOrderId" min-width="200" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.rebateOrderId" />
                <span>{{scope.row.rebateOrderId}}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="返利日期" prop="theTime" min-width="180" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="isCustomer" align="left" label="标题备注" prop="policyDocNo" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="经销商编码" prop="customerNumber" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerNumber" />
                <span>{{scope.row.customerNumber}}</span>
              </template>
            </el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="经销商名称" prop="customerName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerName" />
                <span>{{scope.row.customerName}}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="返利类型" prop="walletName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="right" label="总返利金额" prop="amount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.amount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column align="right" label="返利金额" prop="rebateAmount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.rebateAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column align="right" label="暂扣返利" prop="withholdAmount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.withholdAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column align="right" label="折让金额" prop="allowanceAmount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.allowanceAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="折让编号" prop="allowanceCode" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="折让账号" prop="allowanceAccount" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="right" label="已办理折让金额" prop="handledAllowanceAmount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.handledAllowanceAmount | numToFixed }}
              </template>
            </el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="折让对应收款单号" prop="allowanceOrderNo" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="政策文件流水号" prop="policyFileNo" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="政策文号" prop="policyDocNo" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="政策年份" prop="policyYear" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="政策月份" prop="policyMonth" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="政策归属部门" prop="policyOrg" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="left" v-if="!isCustomer" label="客户区域" prop="customerArea" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="客户属性" prop="customerAttr" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="left" v-if="!isCustomer" label="奖励实际归属客户" prop="rewardActualCustomers" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="备注1" prop="remark1" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="备注2" prop="remark2" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="制单人" prop="createBy" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="制单时间" prop="createTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="审核人" prop="examineBy" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="审核时间" prop="examineTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="确认人" prop="customerName" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="确认时间" prop="customerConfirmTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="复核人" prop="secondExamineBy" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="!isCustomer" align="left" label="复核时间" prop="secondExamineTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column v-if="isCustomer" align="left" label="确认时间" prop="customerConfirmTime" min-width="160" show-overflow-tooltip></el-table-column>
            <el-table-column align="center" label="操作" min-width="240" show-overflow-tooltip fixed="right">
              <template slot-scope="scope">
                <el-button type="text" class="textColor" v-if="
                    !isCustomer &&
                    $checkBtnRole('edit', $route.meta.roles) &&
                    scope.row.examineStatus != 'FAIL_ONE'
                  " @click="editFn(scope.row.rebateOrderId)">编辑</el-button>
                <el-button type="text" class="textColor" v-show="
                    scope.row.examineStatus == 'SAVE' &&
                    !isCustomer &&
                    $checkBtnRole('apply', $route.meta.roles)
                  " @click="applyFn(scope.row.rebateOrderId)">申请</el-button>
                <el-button type="text" class="textColor" v-show="
                    scope.row.examineStatus == 'WAIT' &&
                    !isCustomer &&
                    $checkBtnRole('examine', $route.meta.roles)
                  " @click="examineFn(scope.row.rebateOrderId)">审核</el-button>
                <el-button type="text" class="textColor" v-show="
                    (scope.row.examineStatus == 'OK_ONE' ||
                      scope.row.examineStatus == 'OK' ||
                      scope.row.examineStatus == 'FAIL') &&
                    !isCustomer &&
                    $checkBtnRole('examine', $route.meta.roles)
                  " @click="reviewFn(scope.row.rebateOrderId)">{{
                    scope.row.examineStatus == "OK" ? "取消复核" : "复核"
                  }}</el-button>
                <el-button type="text" class="textColor" v-show="
                    (scope.row.examineStatus == 'WAIT' ||
                      scope.row.examineStatus == 'SAVE') &&
                    !isCustomer
                  " @click="infoFn(scope.row.rebateOrderId)">详情</el-button>
                <el-button type="text" class="textColor" v-show="
                    (scope.row.examineStatus == 'OK_ONE' ||
                      scope.row.examineStatus == 'FAIL_ONE' ||
                      scope.row.examineStatus == 'FAIL' ||
                      scope.row.examineStatus == 'OK') &&
                    !isCustomer
                  " @click="detail2(scope.row.rebateOrderId)">详情</el-button>
                <el-button type="text" class="textColor" v-if="
                    isCustomer &&
                    scope.row.withholdAmount == 0 &&
                    (scope.row.examineStatus == 'OK_ONE' ||
                      scope.row.examineStatus == 'OK' ||
                      scope.row.examineStatus == 'FAIL')
                  " @click="
                    confirmFn(
                      scope.row.rebateOrderId,
                      scope.row.customerIsConfirm
                    )
                  ">{{
                    scope.row.customerIsConfirm == true ? "详情" : "确认"
                  }}</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>

    <RebateListApply :detailId="detailId" v-else-if="showPage == 2" />
    <RebateListExamine @updateList="updateList" :detailId="detailId" v-else-if="showPage == 3" />
    <RebateListReview @updateList="updateList" :detailId="detailId" v-else-if="showPage == 4" />
    <!-- 详情 -->
    <RebateListDetail :detailId="detailId" v-else-if="showPage == 5" />
    <!-- 确定 -->
    <RebateListConfirm @updateList="updateList" :detailId="detailId" :isShow="isShow" v-else-if="showPage == 6" />
    <!-- 编辑 -->
    <RebateListEdit @updateList="updateList" :detailId="detailId" v-else-if="showPage == 7" />
    <!-- <EditDateTimeDialog
      :isShow.sync="isShowEditDateDialog"
      :dateForm.sync="dateForm"
    /> -->
  </div>
</template>

<script>
import {
  getRebateOrderList,
  getRebateOrderApply,
  getRebateOrderBatchDel,
} from "@/api/finance/rebate_list";
import RebateListApply from "./components/rebate_list-apply.vue";
import RebateListExamine from "./components/rebate_list-examine.vue";
import RebateListReview from "./components/rebate_list-review.vue";
import RebateListDetail from "./components/rebate_list-detail";
import RebateListConfirm from "./components/rebate_list-confirm";
import RebateListEdit from "./components/rebate_list-edit.vue";

export default {
  name: "rebate_list",
  components: {
    RebateListDetail,
    RebateListApply,
    RebateListExamine,
    RebateListReview,
    RebateListConfirm,
    RebateListEdit,
  },
  data() {
    return {
      deleList: [],
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        customerName: "",
        walletName: "",
        customerNumber: "",
        startTime: "",
        endTime: "",
        examineStatus: "",
        isConfirm: ""
      }, //搜索表单
      listLoading: false, // 列表加载loading
      showPage: 1,
      detailId: null,
      isCustomer: null,
      secondId: null,
      isShow: null,
    };
  },
  computed: {
    exParams() {
      return {
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
    },
  },
  created() {
    this.getDataList({
      pageSize: this.pageSize,
      pageNum: this.currentPage,
    });
    const res = JSON.parse(localStorage.getItem("supply_user"));

    this.isCustomer = res.isCustomer;
    if (this.$route.query.id) {
      if (this.isCustomer) {
        this.confirmFn(this.$route.query.id, false);
      } else {
        this.examineFn(this.$route.query.id);
      }
    }
  },
  methods: {
    // 批量删除
    selectionChangeFn(value) {
      // console.log(value);
      const res = value.map((v) => v.rebateOrderId);
      // console.log(res);
      this.deleList = res;
    },
    //删除
    async deleFn() {
      let res = this.deleList.toString();
      // console.log(res);
      await getRebateOrderBatchDel({ ids: res });
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      });
      this.$message.success("删除成功");
      this.deleList = [];
    },

    selectableFn(row, index) {
      // console.log(row, index, 444);
      if (row.examineStatus !== "SAVE") {
        return false;
      } else {
        return true;
      }
    },
    //切换radio获取数据
    changeRadioFn(v) {
      // console.log(111);

      this.currentPage = 1;
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      });
    },
    //清除
    cancelFn() {
      this.$refs.searchForm.resetFields();
    },
    //搜索
    searchFn() {
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      });
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getDataList({
        pageNum: 1,
        pageSize: this.pageSize,
        ...this.searchForm,
      });
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList({
        pageNum: val,
        pageSize: this.pageSize,
        ...this.searchForm,
      });
    },
    //更新列表数据
    updateList() {
      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        ...this.searchForm,
      });
    },
    //获取列表数据
    async getDataList(data) {
      const res = await getRebateOrderList(data);
      this.listTotal = res.data.total;
      let arr = res.data.records;
      arr.forEach((value) => {
        value.sums1 = [];
        value.sums2 = [
          "amount",
          "rebateAmount",
          "withholdAmount",
          "allowanceAmount",
          "handledAllowanceAmount",
        ];
      });
      if (this.isCustomer) {
        let arr2 = arr.filter((v) => {
          if (
            (v.examineStatus == "OK_ONE" ||
              v.examineStatus == "OK" ||
              v.examineStatus == "FAIL") &&
            v.withholdAmount == 0
          ) {
            return v;
          }
        });
        this.dataList = arr2;
        this.listTotal = arr2.length;
      } else {
        this.dataList = arr;
        this.listTotal = res.data.total;
      }
    },
    //确认
    confirmFn(id, isShow) {
      this.isShow = isShow;
      this.detailId = id;
      this.showPage = 6;
    },
    //复核
    reviewFn(id) {
      this.detailId = id;
      this.showPage = 4;
    },
    //审核
    examineFn(id) {
      this.detailId = id;
      this.showPage = 3;
    },
    //申请
    async applyFn(id) {
      await getRebateOrderApply({ id });

      this.getDataList({
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        ...this.currentPage,
      });
      this.$message.success("申请成功");
    },
    //详情2
    detail2(id) {
      this.detailId = id;
      this.showPage = 2;
    },
    //详情
    infoFn(id) {
      this.detailId = id;
      this.showPage = 5;
    },
    //编辑
    editFn(id) {
      this.detailId = id;
      this.showPage = 7;
    },
  },
};
</script>
<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
</style>
