<template>
  <el-row class="el-row-container">
    <el-card style="margin:16px;min-height: calc(100% - 32px); overflow-y: auto">
      <el-row style="margin-bottom: 10px">
        <el-col :span="12" class="header-label">
          基础信息
        </el-col>
        <el-col :span="12" style="text-align:right">
          <el-button type="primary" @click="backPage">返回</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <span class="label"><span>IC卡卡号：</span>{{baseForm.cardNo}}</span>
        </el-col>
        <el-col :span="5">
          <span class="label"><span>农户姓名：</span>{{baseForm.farmerName}}</span></el-col>
        <el-col :span="5">
          <span class="label"><span>卡状态：</span>{{baseForm.cardStatusContent}}</span></el-col>
        <el-col :span="4">
          <span class="label"><span>办卡时间：</span>{{baseForm.cardCreateddate}}</span></el-col>
        <el-col :span="5">
          <span class="label"><span>所属区域：</span>{{baseForm.areaFullName}}</span></el-col>
      </el-row>
      <el-row style="margin: 20px 0">
        <el-col :span="5">
          <span class="label"><span>总用水量：</span>{{baseForm.waterNumTotal || 0}} m³</span>
        </el-col>
        <el-col :span="5">
          <span class="label"><span>总用电量：</span>{{baseForm.electricNumTotal || 0}} m³</span></el-col>
      </el-row>
      <div class="dateType">
        <el-date-picker
          v-model="dateValue"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <el-button type="primary" style="margin-left:10px;" @click="handleSearch">查询</el-button>
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane 
          label="灌溉记录"
          name="irrigation"
        >
          <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="cardusercdList"
            :columns="columns1"
            isShowIndex
          >
          </b-table>
        </el-tab-pane>
        <el-tab-pane 
          label="充值记录"
          name="recharge"
        >
          <b-table
            ref="tableRechargercd"
            :args="{ 'highlight-current-row': true }"
            :data="rechargercdList"
            :columns="columns2"
            isShowIndex
          >
            <template #ICCard="scope">
              {{ scope }}
              <b-table
                :args="{ 'highlight-current-row': true }"
                :data="loadData"
                :columns="scope.columns"
                isShowIndex
              >
              </b-table>
            </template>
            <template #rechargercdRechargeamount="scope">
              <el-link
                type="primary"
                :underline="false"
                >+{{ Number(scope.row.rechargercdRechargeamount).toFixed(2) }}</el-link
              >
            </template>
            <template #cardAmount="scope">
              {{ Number(scope.row.cardAmount).toFixed(2) }}
            </template>
          </b-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </el-row>
</template>

<script>
import { getCardDetail,getRechargercdList, getCardusercdList } from '@/api/card/index.js'
import BTable from '@/components/Table/index.vue';
export default {
  name: 'dataDetail',
  props: {
    detailCardId:{ 
      type:String,
    }
  },
  components:{
    BTable
  },
  data() {
    return {
      activeName: 'irrigation',
      baseForm: {},
      dateValue: '',
      columns1: [
        {
          label: '水源编号',
          prop: 'waterNum',
          customRender: '',
          align: 'center',
        },
        {
          label: '水源名称',
          prop: 'waterName',
          customRender: '',
          align: 'center'
        },
        {
          label: '本次耗水量（m³）',
          prop: 'cardusercdWaternum',
          customRender: '',
          align: 'center'
        },
        {
          label: '本次用水量（°）',
          prop: 'cardusercdWaternum',
          customRender: '',
          align: 'center'
        },
        {
          label: '开始时间',
          prop: 'cardusercdStarttime',
          customRender: '',
          align: 'center'
        },
        {
          label: '结束时间',
          prop: 'cardusercdEndtime',
          customRender: '',
          align: 'center'
        },
        {
          label: '使用时长',
          prop: 'cardusercdDuration',
          customRender: '',
          align: 'center'
        },
        {
          label: '计费方式',
          prop: 'cardusercdBillingtype',
          customRender: '',
          align: 'center'
        },
        {
          label: '计费标准',
          prop: 'cardusercdPriceinfo',
          customRender: '',
          align: 'center'
        },
        {
          label: '扣除金额',
          prop: 'cardusercdUsemoney',
          customRender: '',
          align: 'center'
        }
      ],
      columns2: [
        {
          label: 'IC卡号',
          prop: 'cardNo',
          customRender: '',
          align: 'center',
        },
        {
          label: '充值金额（元）',
          prop: 'rechargercdRechargeamount',
          customRender: 'rechargercdRechargeamount',
          align: 'center'
        },
        {
          label: '充值后金额（元）',
          prop: 'cardAmount',
          customRender: 'cardAmount',
          align: 'center'
        },
        {
          label: '充值时间',
          prop: 'rechargercdRechargetime',
          customRender: '',
          align: 'center'
        }
      ]
    };
  },
  methods: {
    async rechargercdList() {
      const res = await getCardDetail({
        cardId:this.detailCardId
      })
      this.baseForm = res.data
      return this.getRechargercdData(res.data);
    },
    async cardusercdList() {
      const res = await getCardDetail({
        cardId: this.detailCardId
      })
      this.baseForm = res.data
      return this.getCardusercdListHandler(res.data)
    },
    getCardusercdListHandler(payload) { 
      const { cardId } = payload
      const [startTime, endTime] = this.dateValue || []
      return getCardusercdList({
        cardId,
        startTime,
        endTime
      })
    },
    getRechargercdData(payload) {
      const {farmerId,cardNo,cardId} = payload
      const [startTime, endTime] = this.dateValue || []
      return getRechargercdList({
        farmerId,
        cardNo,
        cardId,
        startTime,
        endTime
      })
    },
    backPage() {
      this.$emit("backPage")
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.resetFormData();
      this.dialogVisible = false;
    },
    resetFormData() {
      this.baseForm = {};
    },
    
    handleSearch() {
      this.activeName === 'recharge' ?  this.$refs.tableRechargercd.refresh(true) : this.$refs.tableRef.refresh(true) 
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid)
      })
    },

    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
.el-row-container{
  height: calc(100% - 32px);
  .header-label{
    color: #333333;
    font-size: 16px;
    font-weight: 700;
    font-family: "Source Han Sans CN VF";
    text-align: left;
  }
}
::v-deep .el-card__body{
  position: relative;
}
.dateType{
  position: absolute;
  right: 20px;
  margin-top: -5px;
  width: 500px;
  text-align: right;
  z-index:1000;
}
::v-deep.el-dialog__title{
  color: #333333;
  font-size: 16px;
  font-weight: 700;
}
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
.label{
  color: #333;
  span{
    color: #999;
    font-size: 14px;
    font-style: normal;
  }
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
