<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      更换列表
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
        ref='searchForm'
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="流水号 订单号 学生姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="organIdList">
          <el-select
            class="multiple"
            v-model.trim="searchForm.organIdList"
            filterable
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in selects.branchs"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="cooperationOrganId">
          <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 prop="resetDate">
          <el-date-picker
            style="width: 410px"
            v-model.trim="searchForm.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 native-type="submit" type="danger">搜索</el-button>
          <el-button native-type="reset" type="primary">重置</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 type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                label-width="120px"
                class="demo-table-expand"
              >
                <el-form-item label="交易流水号">
                  <copy-text>{{ props.row.transNo }}</copy-text>
                </el-form-item>
                <el-form-item label="订单号">
                  <copy-text>{{ props.row.orderNo }}</copy-text>
                </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="学员姓名">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.studentName }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="organName" label="所属分部">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.organName }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="cooperationOrganName"
            label="所属学校"
          >
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.cooperationOrganName }}</copy-text>
              </div>
            </template>
          </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)}" -->
            <template slot-scope="scope">
              <overflow-text width="100%" :text="accessoriesFormatter(scope.row.originalAccessoriesGoods)"></overflow-text>
            </template>
          </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)}" -->
            <template slot-scope="scope">
              <overflow-text width="100%" :text="accessoriesFormatter(scope.row.changeAccessoriesGoods)"></overflow-text>
            </template>
          </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="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
          sync
          :total.sync="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";
import { closeSalaryConfirm } from "@/api/journal";
import { getTimes } from "@/utils"
let nowTime = new Date();
nowTime =
  nowTime.getFullYear() +
  "-" +
  (nowTime.getMonth() + 1) +
  "-" +
  nowTime.getDate();
export default {
  components: { pagination, resetSound },
  data() {
    return {
      searchForm: {
        search: null,
        resetDate: [],
        organIdList: null,
        cooperationOrganId: null,
      },

      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() {
    getCooperation({ rows: 1000 }).then((res) => {
      if (res.code == 200) {
        this.cooperations = res.data.rows;
      }
    });
    this.$store.dispatch("setBranchs");
    // getEmployeeOrgan().then((res) => {
    //   if (res.code == 200) {
    //     this.organList = res.data;
    //   }
    // });
    // 获取分部

    this.init();
  },
  activated() {
    // this.init();
  },
  methods: {
    init() {
      if (this.searchForm.resetDate?.length < 1) {
        this.searchForm.resetDate = [];
        this.searchForm.resetDate.push(getNowDateAndMonday(nowTime));
        this.searchForm.resetDate.push(getNowDateAndSunday(nowTime));
      }

      this.getList();
    },
    getList() {
      const { resetDate, ...rest } = this.searchForm;
      let params = {
        ...rest,
        ...getTimes(resetDate, ["startTime", "endTime"]),
        page: this.rules.page,
        rows: this.rules.limit,
      };
      // 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.$refs.searchForm.resetFields();
      console.log(this.searchForm)
      this.searchForm.startTime = null;
       this.searchForm.endTime = null;
      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>
