<!--  -->
<template>
  <div class="m-core">
    <div class="topWrap">
      <div class="newBand"
           @click="newPay"
           v-permission="'musicGroupPaymentCalender/add'">新建缴费</div>
      <div class="newBand"
           v-permission="'/studentPayBase'"
           @click="setStudentPay">学员缴费设置</div>
    </div>
    <div class="tableWrap">
      <el-table style="width: 100%"
                :header-cell-style="{background:'#EDEEF0',color:'#444'}"
                :data="tableList">
        <el-table-column align="center"
                         prop="startPaymentDate"
                         label="缴费开始日期">
          <template slot-scope="scope">
            <div>
              {{scope.row.startPaymentDate | formatTimer}}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="deadlinePaymentDate"
                         label="缴费结束日期">

          <template slot-scope="scope">
            <div>
              {{scope.row.deadlinePaymentDate | formatTimer}}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="expectNum"
                         label="预计缴费人数"></el-table-column>
        <el-table-column align="center"
                         prop="actualNum"
                         label="实际缴费人数"></el-table-column>
        <el-table-column align="center"
                         prop="type"
                         label="状态">
          <template slot-scope="scope">
            <div>
              {{scope.row.type | paymentType}}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="type"
                         label="缴费状态">
          <template slot-scope="scope">
            <div>
              {{scope.row.paymentStatus | paymentListStatus}}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         prop="memo"
                         label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="text"
                         v-if="scope.row.paymentStatus != 0"
                         @click="lookDetail(scope.row)"
                         v-permission="'/strudentPayInfo'">查看</el-button>
              <el-button type="text"
                         v-if="scope.row.paymentStatus == 0"
                         v-permission="'musicGroupPaymentCalender/updateStartTime'"
                         @click="resetPay(scope.row)">修改</el-button>
              <el-button type="text"
                         v-permission="'musicGroupPaymentCalender/del'"
                         v-if="scope.row.paymentStatus == 0"
                         @click="detelePay(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :total="rules.total"
                  :page.sync="rules.page"
                  :limit.sync="rules.limit"
                  :page-sizes="rules.page_size"
                  @pagination="getList" />
    </div>
    <el-dialog :visible.sync="payVisible"
               :close-on-click-modal="false"
               width="500px"
               :title="diTitle">
      <el-form :model="payForm"
               :inline="true"
               label-width="120px"
               label-position="right"
               ref='payForm'>
        <el-form-item label="缴费开始日期"
                      :rules="[{ required: true, message: '请设置缴费开始日期',trigger: 'blur'}]"
                      prop="startPaymentDate">
          <el-date-picker v-model.trim="payForm.startPaymentDate"
                          type="date"
                          :picker-options="pickerOptions"
                          value-format="yyyy-MM-dd"
                          placeholder="开始日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="收费类型"
                      v-if='isNew'
                      :rules="[{ required: true, message: '请选择收费类型',trigger: 'blur'}]"
                      prop="type">
          <el-select v-model.trim="payForm.type"
                     style="width:220px!important;"
                     placeholder="课程类型">
            <el-option label="线上"
                       value="ONLINE"></el-option>
            <el-option label="线下"
                       value="OFFLINE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注"
                      v-if='isNew'
                      :rules="[{ required: true, message: '请填写备注',trigger: 'blur'}]"
                      prop="memo">
          <el-input type="textarea"
                    style="width:220px!important;"
                    :rows="4"
                    placeholder="请填写备注"
                    v-model="payForm.memo"></el-input>

        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="payVisible = false">取 消</el-button>
        <el-button type="primary"
                   v-if="isNew"
                   @click="newPayInfo">确 定</el-button>
        <el-button type="primary"
                   v-else
                   @click="resetPayDate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { addMusicGroupPaymentCalender, getMusicGroupPaymentCalender, resetMusicGroupPaymentCalender, delMusicGroupPaymentCalender } from "@/api/buildTeam";
export default {
  components: { pagination },
  data () {
    return {
      searchForm: {
        search: null
      },
      tableList: [{}],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      isInit: false,
      diTitle: '新增缴费',
      payVisible: false,
      payForm: {
        startPaymentDate: null,
        type: null,
        memo: null
      },
      isNew: false,
      activeRow: null,
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() + 86400000 <= new Date().getTime();
        }
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created () { },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    // 获取分部

    this.init();


  },
  activated () {
    this.init();
  },
  methods: {
    init () {
      this.getList()
    },
    getList () {
      getMusicGroupPaymentCalender({ page: this.rules.page, rows: this.rules.limit, musicGroupId: this.$route.query.id }).then(res => {
        if (res.code == 200) {
          this.rules.total = res.data.total
          this.tableList = res.data.rows;
        }
      })
    },
    newPay () {
      this.diTitle = '新增缴费'
      this.isNew = true
      this.payVisible = true;
    },
    resetPay (row) {
      this.diTitle = '修改缴费'
      this.isNew = false
      this.activeRow = row
      this.payVisible = true;
    },
    detelePay (row) {
      let id = row.id;
      this.$confirm(`确定删除该缴费周期?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMusicGroupPaymentCalender({ id }).then(res => {
          if (res.code == 200) {
            this.$message.success('删除成功')
            this.getList();
          }
        })
      }).catch(() => { })

    },
    lookDetail (row) {
      let query = this.$route.query
      this.$route.query.paymentId = row.id;
      this.$router.push({ path: '/business/strudentPayInfo', query })
    },
    setStudentPay () {
      let query = this.$route.query
      this.$router.push({ path: '/business/studentPayBase', query })
    },
    newPayInfo () {
      this.$refs['payForm'].validate(res => {
        if (res) {
          this.payForm.musicGroupId = this.$route.query.id
          addMusicGroupPaymentCalender(this.payForm).then(res => {
            if (res.code == 200) {
              this.$message.success('恭喜你创建成功')
              this.payVisible = false;
              this.getList()
            }
          })
        }
      })
    },
    resetPayDate () {
      resetMusicGroupPaymentCalender({ id: this.activeRow.id, startTime: this.payForm.startPaymentDate }).then(res => {
        if (res.code == 200) {
          this.$message.success('修改成功')
          this.payVisible = false;
          this.getList()
        }
      })
    }
  },
  watch: {
    payVisible (val) {
      if (!val) {
        this.payForm = {
          startPaymentDate: null,
          type: null,
          memo: null
        }
        this.$refs['payForm'].resetFields()
      }
    }
  }
};
</script>
<style lang='scss' scoped>
.topWrap {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  div {
    margin-right: 10px;
  }
}
</style>