<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>更换列表
    </h2>
    <div class="m-core">
      <el-form :inline="true"
               :model="searchForm">
        <el-form-item>
          <el-input v-model.trim="searchForm.search"
                    @keyup.enter.native="search"
                    placeholder='流水号 订单号 学生姓名'></el-input>
        </el-form-item>
        <el-form-item prop="organId">
          <el-select class="multiple"
                     v-model.trim="searchForm.organIdList"
                     filterable
                     clearable
                     placeholder="请选择分部">
            <el-option v-for="(item,index) in organList"
                       :key="index"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="organId">
          <el-select clearable
                     filterable
                     placeholder="请选择所属学校"
                     v-model="searchForm.cooperationOrganId">
            <el-option v-for="(item,index) in cooperations"
                       :key="index"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item prop="organId">
          <el-select class="multiple"
                     v-model.trim="searchForm.organId"
                     filterable
                     clearable
                     placeholder="销售类型">
            <el-option v-for="(item,index) in saleStatus"
                       :key="index"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-date-picker style="width:410px;"
                          v-model.trim="resetDate"
                          type="daterange"
                          value-format="yyyy-MM-dd"
                          range-separator="至"
                          start-placeholder="销售开始日期"
                          end-placeholder="销售结束日期"
                          :picker-options="{ firstDayOfWeek: 1 }">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search"
                     type="danger">搜索</el-button>
          <el-button @click="onReSet"
                     type="primary">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tableWrap">
        <el-table style="width: 100%"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}"
                  :data="tableList">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left"
                       inline
                       label-width="120px"
                       class="demo-table-expand">
                <el-form-item label="交易流水号">
                  <span>{{ props.row.transNo }}</span>
                </el-form-item>
                <el-form-item label="订单号">
                  <span>{{ props.row.orderNo }}</span>
                </el-form-item>
                <el-form-item label="更换金额(元)">
                  <span>{{ props.row.changeMusicalPrice |moneyFormat}}</span>
                </el-form-item>
                <el-form-item label="乐团主管">
                  <span>{{ props.row.eduTeacher }}</span>
                </el-form-item>
                <el-form-item label="交易方式">
                  <span>{{ props.row.paymentChannel| paymentChannelStatus}}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column align="center"
                           prop="studentName"
                           label="学员姓名"></el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="原乐器">
            <template slot-scope="scope">
              <div v-if="scope.row.originalMusicalGoods">
                {{scope.row.originalMusicalGoods.name}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="原教辅"
                           :formatter="(row)=>{return accessoriesFormatter(row.originalAccessoriesGoods)}">
          </el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="原支付金额(元)">
            <template slot-scope="scope">
              <div>
                {{(scope.row.originalMusicalPrice)|moneyFormat}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="更换乐器">
            <template slot-scope="scope">
              <div v-if="scope.row.changeMusicalGoods">
                {{scope.row.changeMusicalGoods.name}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           label="更换教辅"
                           :formatter="(row)=>{return accessoriesFormatter(row.changeAccessoriesGoods)}"></el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="差价(元)">
            <template slot-scope="scope">
              <div>
                {{((scope.row.changeMusicalPrice+scope.row.changeAccessoriesPrice)-(scope.row.originalMusicalPrice+scope.row.originalAccessoriesPrice))|moneyFormat}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="成本差(元)">
            <template slot-scope="scope">
              <div>
                {{scope.row.costMargin | moneyFormat}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="organName"
                           label="所属分部"></el-table-column>
          <el-table-column align="center"
                           prop="cooperationOrganName"
                           label="所属学校"></el-table-column>
          <el-table-column align="center"
                           prop="musicGroupName"
                           label="乐团"></el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="销售日期">
            <template slot-scope="scope">
              <div>
                {{scope.row.sellTime | dayjsFormat}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           prop="studentId"
                           label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="text"
                           @click="reset(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>
    </div>
    <resetSound :show="show"
                @closeReset='closeReset'
                @getList='getList'
                :item='activeRow' />
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getTeacher, getEmployeeOrgan, getCooperation } from "@/api/buildTeam";
import { getSubjectChangeList } from "@/api/businessManager"
import resetSound from './resetSound'
import { saleStatus } from '@/utils/searchArray'
import cleanDeep from 'clean-deep'
import { getNowDateAndMonday, getNowDateAndSunday } from '@/utils/utils'
let nowTime = new Date();
nowTime =
  nowTime.getFullYear() +
  "-" +
  (nowTime.getMonth() + 1) +
  "-" +
  nowTime.getDate();
export default {
  components: { pagination, resetSound },
  data () {
    return {
      searchForm: {
        search: null,

      },
      resetDate: [],
      show: false,
      teacherList: [],
      tableList: [],
      organList: [],
      cooperations: [],
      saleStatus,
      activeRow: null,
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created () { },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.resetDate = [];
    this.resetDate.push(getNowDateAndMonday(nowTime));
    this.resetDate.push(getNowDateAndSunday(nowTime));
    getCooperation({ 'rows': 1000, })
      .then(res => {
        if (res.code == 200) {
          this.cooperations = res.data.rows;
        }
      })
    getEmployeeOrgan().then(res => {
      if (res.code == 200) {
        this.organList = res.data;
      }
    });
    // 获取分部

    this.init();


  },
  activated () {
    this.init();
  },
  methods: {
    init () {
      this.getList()
    },
    getList () {
      let params = this.searchForm
      params.rows = this.rules.limit
      params.page = this.rules.page
      if (this.resetDate && this.resetDate.length > 1) {
        params.startTime = this.resetDate[0]
        params.endTime = this.resetDate[1]
      } else {
        params.startTime = null
        params.endTime = null
      }


      getSubjectChangeList(cleanDeep(params)).then(res => {
        if (res.code === 200) {
          this.rules.total = res.data.total;
          this.tableList = res.data.rows
        }
      })
    },
    reset (row) {
      this.activeRow = row
      this.show = true
    },
    closeReset () {
      this.show = false
    },
    search () {
      this.rules.page = 1;
      this.getList()
    },
    onReSet () {
      this.searchForm.resetFields()
      this.search()
    },
    accessoriesFormatter (accessories) {
      if (accessories && accessories.length > 0) {
        let arr = accessories.map(res => {
          return res.name
        })
        return arr.join(',')
      } else {
        return ''
      }

    }
  }, watch: {

  }
};
</script>
<style lang='scss' scoped>
/deep/.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
/deep/.demo-table-expand label {
  color: #99a9bf;
}
</style>
