<template>
  <div class="card-container">
    <el-card class="card-heard">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
        <el-form-item label="订单号" prop="id" style="margin-left: 14px;">
          <el-input
            v-model="queryParams.param.orderId"
            placeholder="请输入交易单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="停车场名称" prop="parkingId">
          <el-select
            v-model="queryParams.param.parkingId"
            placeholder="请输入停车场名称"
            clearable
          >
            <el-option
              v-for="item in parkNameList"
              :key="item.id"
              :label="item.parkingName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车位编号" prop="spaceNo">
          <el-input
            v-model="queryParams.param.spaceNo"
            placeholder="请输入车位编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业主手机号" prop="userPhone">
          <el-input
            v-model="queryParams.param.userPhone"
            placeholder="请输入业主手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select
            v-model="queryParams.param.payType"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="支付日期" prop="createTime">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 280px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div style="display: flex;justify-content: right;margin: 0px 80px -15px 0px;">
        <label style="font-size: 14px">业主入账</label>
        <div style="color: #ff6600; font-weight: bold; font-size: 20px;margin-left: 10px;margin-top: -3px;">
          <span>￥</span>&nbsp;
          <span  v-if="totalObj!=null">
            {{ totalObj.acountTotal? totalObj.acountTotal.toFixed(2) : 0 }}
          </span>
          <span v-else >0</span>
        </div>
        <label style="font-size: 14px;margin-left: 50px;">平台提成</label>
        <div style="color: #ff6600; font-weight: bold; font-size: 20px;margin-left: 10px;margin-top: -3px;">
          <span>￥</span>&nbsp;
          <span v-if="totalObj!=null">
             {{ totalObj.stageTotal? totalObj.stageTotal.toFixed(2) : 0 }}
          </span>
          <span v-else >0</span>
        </div>
      </div>
    </el-card>
    <el-card class="card-body">
<!--      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['record:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['record:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['record:remove']"
          >删除
          </el-button>
        </el-col>
      </el-row>-->

      <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="订单号" align="center" prop="orderId" width="200" :show-overflow-tooltip="true"/>
        <el-table-column label="停车场名称" align="center" prop="parkingName" :show-overflow-tooltip="true"/>
        <el-table-column label="车位编号" align="center" prop="spaceNo" :show-overflow-tooltip="true"/>
        <el-table-column label="业主手机号" align="center" prop="userPhone"/>
        <el-table-column label="支付金额" align="center" prop="payPrice"/>
        <el-table-column label="业主入账金额" align="center" prop="priceReceived">
          <template slot-scope="scope">
            {{ scope.row.priceReceived ? scope.row.priceReceived.toFixed(2) : 0.00 }}
          </template>
        </el-table-column>
        <el-table-column label="平台提成" align="center" prop="royaltyPrice">
          <template slot-scope="scope">
            {{ scope.row.royaltyPrice ? scope.row.royaltyPrice.toFixed(2) : 0.00 }}
          </template>
        </el-table-column>
        <el-table-column label="租客手机号" align="center" prop="phone"/>
        <el-table-column label="支付方式" align="center" prop="payType"/>
        <el-table-column label="支付时间" align="center" prop="createTime"/>
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['record:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['record:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>-->
      </el-table>
    </el-card>
    <pagination
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改账单记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID"/>
        </el-form-item>
        <el-form-item label="停车场/小区名称" prop="parkingId">
          <el-input v-model="form.parkingId" placeholder="请输入停车场/小区名称"/>
        </el-form-item>
        <el-form-item label="支付金额" prop="payPrice">
          <el-input v-model="form.payPrice" placeholder="请输入支付金额"/>
        </el-form-item>
        <el-form-item label="平台提成" prop="royaltyPrice">
          <el-input v-model="form.royaltyPrice" placeholder="请输入平台提成"/>
        </el-form-item>
        <el-form-item label="入账金额" prop="priceReceived">
          <el-input v-model="form.priceReceived" placeholder="请输入入账金额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除标志"/>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号"/>
        </el-form-item>
      </el-form>
      <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 {
  listParkName
} from '@/api/carManage/parking'
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  exportRecord,
  getAcountStatics
} from '@/api/orderManage/record'

export default {
  name: 'Record',
  components: {},
  data() {
    return {
      totalObj:{},//业主入账和平台提成
      // 状态数据字典
      statusOptions: [],
      // 选择停车场名称列表
      parkNameList: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账单记录表格数据
      recordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        param: {
          id: null,
          orderId: null,
          parkingId: null,
          payPrice: null,
          royaltyPrice: null,
          priceReceived: null,
          isDel: null,
          version: null,
          spaceNo: null,
          userPhone: null,
          payType: null
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        parkingId: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        payPrice: [
          {
            required: true,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.initTime();
    this.getList()
    this.getDicts("pay_type").then(response => {
      this.statusOptions = response.data;
    });
    listParkName({}).then(response => {
      this.parkNameList = response.result
    })
  },
  methods: {
    initTime() {
      // 初始化查询，默认为前30天到今天
      let date = new Date();
      let startDate = new Date(date.getTime() - 24 * 60 * 60 * 1000 * 30);
      let endDate = date;
      this.dateRange = [
        this.getDateDay(startDate),
        this.getDateDay(endDate),
      ];
    },
    getDateDay(date) {
      //获取当前时间的年份转为字符串
      let year = date.getFullYear().toString(); //'2019'
      //获取月份，由于月份从0开始，此处要加1，判断是否小于10，如果是在字符串前面拼接'0'
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1).toString()
          : (date.getMonth() + 1).toString(); //'04'
      //获取天，判断是否小于10，如果是在字符串前面拼接'0'
      let da =
        date.getDate() < 10
          ? "0" + date.getDate().toString()
          : date.getDate().toString(); //'12'
      //字符串拼接，开始时间，结束时间
      return year + "-" + month + "-" + da; //当天'2019-04-12'
    },
    /** 查询账单记录列表 */
    getList() {
      this.loading = true
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.param["beginTime"] = this.dateRange[0];
        this.queryParams.param["endTime"] = this.dateRange[1];
      }
      listRecord(this.queryParams).then(response => {
        this.recordList = response.records
        this.total = response.total
        this.loading = false
      })
      getAcountStatics(this.queryParams).then(response => {
        this.totalObj = response.result
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        parkingId: null,
        payPrice: null,
        royaltyPrice: null,
        priceReceived: null,
        createTime: null,
        updateTime: null,
        remark: null,
        isDel: null,
        version: null
      }

      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        current: 1,
        size: 10,
        param: {},
      };
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加账单记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id =
        row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改账单记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除账单记录编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delRecord(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
