<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="油站名称" prop="stationId" >
        <el-select
          v-model="queryParams.stationId"
          placeholder="请选择油站"
          clearable
          size="small"
        >
          <el-option
            v-for="item in stationOptions"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="handleAdd">添加班结</el-button>
      </el-form-item>
    </el-form>
    <!--按员工汇总-->
    <span  style="color:#ff9955;font-size:25px;" >|</span><span style="font-size:25px;">按员工汇总</span>
    <el-table v-loading="loading" show-summary :data="structureList"   :summary-method="getTotal">
      <el-table-column label="员工姓名" align="center" prop="oilPersonnel" />
      <el-table-column label="负责枪号" align="center" prop="oilGun" />
      <el-table-column label="销量" align="center" prop="orderLiters" />
      <el-table-column label="销额" align="center" prop="amt" />
      <el-table-column label="销售笔数" align="center" prop="orderNo" />
    </el-table>
    <!--按油品汇总-->
    <span  style="color:#ff9955;font-size:25px;padding-top: 15px;" >|</span><span style="font-size:25px;">按油品汇总</span>
    <el-table v-loading="loading" :data="structureList1"  show-summary   :summary-method="getTotal2" >
      <el-table-column label="油品" align="center" prop="oilName"  />
      <el-table-column label="负责枪号" align="center" prop="oilGun" />
      <el-table-column label="销量" align="center" prop="orderLiters" />
      <el-table-column label="销额" align="center" prop="amt" />
      <el-table-column label="销售笔数" align="center" prop="orderNo" />
    </el-table>
    <!-- 添加或修改班结管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <span>请确认没有车主使用系统支付</span>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { stationinfo,listStructure,listPersonnelStructure,listOilStructure, getStructure, delStructure, addStructure, updateStructure, exportStructure } from "@/api/station/structure";
export default {
  name: "Structure",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班结管理表格数据
      structureList: [],
      structureList1: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: null,
        stationName: null,
        person: null,
        gunNo: null,
        salesVolume: null,
        salesAmount: null,
        discount: null,
        coupon: null,
        netReceipts: null,
        salesNum: null,
        printNum: null,
        classStructureNo: null,
        classStructureDate: null,
        classStructureMan: null
      },
      stationOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getList2();
    stationinfo().then(response => {
      this.stationOptions = response.rows;
      this.loading = false;
    });
  },
  methods: {
    /** 查询班结管理列表 */
    getList() {
      this.loading = true;
      listPersonnelStructure(this.queryParams).then(response => {
        this.structureList = response.rows;
        this.loading = false;
      });
    },
    getList2() {
      this.loading = true;
      listOilStructure(this.queryParams).then(response => {
        this.structureList1 = response.rows;
        this.loading = false;
      });
    },
    getTotal(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property === 'orderLiters') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(2);
        }else if(column.property === 'amt'){
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(2);
        }else if(column.property === 'orderNo'){
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(0);
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },
    getTotal2(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property === 'orderLiters') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(2);
        }else if(column.property === 'amt'){
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(2);
        }else if(column.property === 'orderNo'){
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]=sums[index].toFixed(0);
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stationId: null,
        stationName: null,
        person: null,
        gunNo: null,
        salesVolume: null,
        salesAmount: null,
        discount: null,
        coupon: null,
        netReceipts: null,
        salesNum: null,
        printNum: null,
        classStructureNo: null,
        classStructureDate: null,
        classStructureMan: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班结管理";
    },
    /** 提交按钮 */
    submitForm() {
      //获取当前登录人信息
      //根据获取的用户信息来生成班结数据
      this.form.stationName=this.$store.state.user.stationName;
      this.form.stationId=this.$store.state.user.stationId;
      this.form.classStructureMan=this.$store.state.user.name;
      addStructure(this.form).then(response => {
        this.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    }

  }
};
</script>
