<template>
  <div class="m-container">
    <el-page-header @back="goBack" :content="name"> </el-page-header>
    <div class="m-core" style="margin-top:20px;">
      <save-form
        :inline="true"
        :model="searchForm"
        ref="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="
              (e) => {
                e.target.blur();
                $refs.saveForm.save();
                search();
              }
            "
            placeholder="乐团名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="refundStatus">
          <el-select
            v-model.trim="searchForm.refundStatus"
            clearable
            placeholder="付款状态"
          >
            <el-option label="回款中" value="ING"></el-option>
            <el-option label="已回款" value="END"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="payTime">
          <el-date-picker
            v-model.trim="searchForm.payTime"
            style="width: 400px"
            type="daterange"
            range-separator="至"
            start-placeholder="缴费开始日期"
            end-placeholder="缴费结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="{
              firstDayOfWeek: 1,
            }"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="musicGroupName"
            label="乐团名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="batchNo"
            label="缴费批次"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="缴费类型">
            <template slot-scope="scope">
              <div>
                {{ scope.row.paymentType | userPaymentTypeFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="计价标准">
            <template slot-scope="scope">
              <div v-if="scope.row.calenderFeeType">
                {{
                  scope.row.calenderFeeType == "TEACHER" ? "按老师" : "按学员"
                }}
              </div>
              <div v-else>--</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="应收总额">
            <template slot-scope="scope">
              <div>
                {{ scope.row.receivable | moneyFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="实收总额">
            <template slot-scope="scope">
              <div>
                {{ scope.row.income | moneyFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="待收金额">
            <template slot-scope="scope">
              <div>
                {{ scope.row.prepaidFee | moneyFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="educationUserName"
            label="下一回款日期"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.nextRefundDate | dayjsFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="educationUserName"
            label="回款状态"
            width="100px"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.refundStatus == "ING" ? "回款中" : "已回款" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <auth auths="musicGroupPaymentCalender/getDetail/barnch">
                  <el-button type="text" @click="lookList(scope.row)"
                    >详情</el-button
                  >
                </auth>

                <el-button
                  type="text"
                  :disabled="!scope.row.contractUrl"
                  @click="downLoad(scope.row)"
                  >下载合同</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
      <el-dialog
        title="查看"
        :visible.sync="reviewVisible"
        width="900px"
        v-if="reviewVisible"
      >
        <reviewDetail
          @close="reviewVisible = false"
          @submited="getList"
          :detail="viewDetail"
          :musicGroupId="$route.query.id"
          destroy-on-close
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="reviewVisible = false"
            >关闭</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import { getTimes } from "@/utils";
import load from "@/utils/loading";
import { backTypeList } from "@/utils/searchArray";
import { Export } from "@/utils/downLoadFile";
import { getCoopCalenderList } from "./api";
import { queryByOrganId } from "@/api/systemManage";
import reviewDetail from "@/views/resetTeaming/modals/review-detail";
import qs from "qs";
export default {
  components: { pagination, reviewDetail },
  data() {
    return {
      searchForm: {
        cooperationId: this.$route.query.id,
        refundStatus:''
      },
      name: this.$route.query.name,
      backTypeList,
      tableList: [],
      cooperationList: [],
      activeRow: null,
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      form: {
        id: null,
      },
      reviewVisible: false,
      viewDetail: null,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  async mounted() {
    // 获取分部
    await this.$store.dispatch("setBranchs");
    this.init();
  },
  methods: {
    init() {
      this.getList();
    },
    async getList() {
      let { payTime, ...rest } = this.searchForm;
      let params = {
        ...rest,
        ...getTimes(payTime, ["refundStartDate", "refundEndDate"]),
        page: this.rules.page,
        rows: this.rules.limit,
      };
      try {
        const res = await getCoopCalenderList(params);
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs["searchForm"].resetFields();
      this.searchForm.cooperationId = this.$route.query.id;
      this.search();
    },
    goBack() {
      this.$store.dispatch('delVisitedViews', this.$route)
      this.$router.push('/branchPayManager')
    },
    lookList(row) {
      this.viewDetail = row;
      this.viewDetail.id = row.calenderId;
      this.reviewVisible = true;
    },
    async downLoad(row) {
      try {
        await this.$confirm(`是否下载批次号为${row.batchNo}合同`, "提示", {
          type: "warning",
        });
        window.open(row.contractUrl);
      } catch (e) {
        console.log(e);
      }
    },
    resetBackForm(row) {
      this.activeRow = row;
      this.form = { ...row };
      this.resetVisible = true;
    },
    resetRorm() {
      this.$refs.form.validate(async (flag) => {
        if (flag) {
          try {
            if (!this.form.transNo) {
              this.form.payTime = null;
            }
            const res = await resetUserCashAccountLog(this.form);
            this.$message.success("修改成功");
            this.resetVisible = false;
            this.getList();
          } catch (e) {
            console.log(e);
          }
        }
      });
    },
    exportBack() {
      let { backTime, payTime, organId, ...rest } = this.searchForm;
      let params = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        organId: organId.join(","),
        ...getTimes(backTime, ["createStartTime", "createEndTime"]),
        ...getTimes(payTime, ["payStartTime", "payEndTime"]),
      };
      Export(
        this,
        {
          url: "/api-web/export/userCashAccountLog",
          fileName: "退费列表.xls",
          method: "post",
          params: qs.stringify(params),
        },
        "您确定导出退费列表?"
      );
    },
    changeOrgan(val) {
      this.searchForm.cooperationId = null;
    },
  },
  watch: {
    // "searchForm.organId"(val) {
    //   if (val) {
    //     queryByOrganId({ organId: val }).then((res) => {
    //       if (res.code == 200) {
    //         this.cooperationList = res.data;
    //       }
    //     });
    //   }
    // },
    // "searchForm.cooperationId"(val) {
    //   if (val) {
    //     getMusicGroup({ cooperationId: val }).then((res) => {
    //       this.musicList = res.data;
    //     });
    //   }
    // },
  },
};
</script>
<style lang='scss' scoped>
.search {
  ::v-deep .el-input__inner {
    width: 260px !important;
  }
}
.form {
  ::v-deep .el-input {
    width: 220px;
  }
}
</style>
