<template>
  <el-row  class="el-row-container" v-loading="loading">
    <el-col
      v-if="!detailType"
      :span="4"
      style="height:100%;padding-bottom:32px;"
    >
    <el-card style="margin:16px;height: 100%; overflow-y: auto">
      <data-report-left
        @setCurrentData="setCurrentData"
        :treeData="treeData"
      ></data-report-left>
    </el-card>
    </el-col>
    <el-col
      v-show="!detailType"
      :span="20"
      style="padding: 16px 16px 16px 0;  height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div>
            <el-select 
              placeholder="用户卡号" 
              v-model="labelType"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-input
              v-model="inputContent"
              style="width: 250px;margin:0 16px;"
              placeholder="请输入内容"
              @keyup.enter.native="handleSearch"
              clearable
            />
            <el-select
              placeholder="IC卡状态"
              v-model="cardStatus"
              style="margin-right:16px"
            >
              <el-option
                v-for="item in icType"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-button
              type="primary"
              size="small"
              @click="handleSearch"
              >查询</el-button
            >
          </div>
          <div class="top-left">
            <el-button
              type="primary"
              size="small"
              @click="addICard"
              >新增IC卡
              </el-button
            ></div>
        </el-col>
        <el-col :span="24" class="elrow-main__col-bottom">
          <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="loadData"
            :columns="columns"
            isShowIndex
          >
            <template #status="scope">
              <span style="display:flex;align-items:center;justify-content:center">
                {{ scope.row.cardStatusContent }}
              </span>
            </template>
            <template #cardAmount="scope">
              <span style="display:flex;align-items:center;justify-content:center">
                {{ Number(scope.row.cardAmount).toFixed(2) }}
              </span>
            </template>
            <template #operate="scope">
              <el-link
                size="small"
                :underline="false"
                :type="scope.row.cardStatus == '2'?'info':'primary'"
                style="margin-right: 10px"
                @click="editCardHandler(scope.row,2)"
                >注销</el-link
              >
              <el-link
                size="small"
                v-if="scope.row.cardStatus == '0'"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="editCardHandler(scope.row,1)"
              >挂失</el-link>
              <el-link
                size="small"
                v-if="scope.row.cardStatus == '1'"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="editCardHandler(scope.row,0)"
              >解挂</el-link>
              <el-link
                size="small"
                :underline="false"
                type="danger"
                style="margin-right: 10px"
                @click="deleteCardHandler(scope.row)"
              >删除</el-link>
              <el-link
                v-if="scope.row.cardStatus !== '2'"
                size="small"
                :underline="false"
                type="warning"
                style="margin-right: 10px"
                @click="rechargeCardHandler(scope.row)"
              >充值</el-link>
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="changeDataDetailShow(scope.row)"
              >数据详情</el-link>
            </template>
          </b-table>
        </el-col>
      </el-card>
    </el-col>
    <AddedICCard 
      :visible.sync="associationManageShow"
      :areaId="currentClickId"
      :userList="userList"
      @refresh="handleSearch"
    />
    <recharge-card
      :visible.sync="rechargeShow"
      :rechargeCardData="rechargeCardData"
      @refresh="handleSearch"
    />
    <data-detail 
      v-if="detailType"
      :detailCardId="detailCardId"
      @backPage="showList"
    />
  </el-row>
</template>

<script>
import { Message } from 'element-ui';
import { getFarmerList } from '@/api/farmer/list.js'
import { getTree } from '@/api/tree.js'
import { getCardList,editCard,deleteCard } from '@/api/card/index.js'
import BTable from '@/components/Table/index.vue';
import DataReportLeft from '@/components/DataReportLeft/index.vue'
import AddedICCard from './components/addedICCard.vue'
import RechargeCard from './components/rechargeCard.vue';
import DataDetail from './dataDetail.vue';

export default {
  name: 'waterCardManage',
  components: { BTable,DataReportLeft,AddedICCard,RechargeCard,DataDetail },
  data() {
    return {
      inputContent:'',
      labelType:0,
      options:[{
        value: 0,
        label: '农户手机号',
      },{
        value: 1,
        label: '用户卡号',
      },{
        value: 2,
        label: '农户姓名',
      }],
      cardStatus: '',
      icType: [{
        value:'',
        label:'全部'
      },{
        value: 0,
        label: '正常'
      },{
        value: 1,
        label: '挂失'
      },{
        value: 2,
        label: '注销'
      }],
      detailType: false,
      form: {
        productName: ''
      },
      currentClickId:'',
      treeData: [],
      loading: false,
      associationManageShow: false,
      dataDetailShow: false,
      rechargeShow: false,
      userList: [],
      rechargeCardData: {},
      detailCardId: '',
      columns: [
        {
          label: 'IC卡号',
          prop: 'cardNo',
          customRender: '',
          align: 'center'
        },
        {
          label: '农户姓名',
          prop: 'farmerName',
          customRender: '',
          align: 'center'
        },
        {
          label: 'IC卡状态',
          prop: 'cardStatusContent',
          customRender: 'status',
          align: 'center'
        },{
          label: '停泵下限',
          prop: 'phone',
          customRender: '',
          align: 'center'
        },{
          label: '卡内余额',
          prop: 'cardAmount',
          customRender: 'cardAmount',
          align: 'center'
        },{
          label: '累计用水量',
          prop: 'waterNumTotal',
          customRender: '',
          align: 'center'
        },{
          label: '累计用电量',
          prop: 'electricNumTotal',
          customRender: '',
          align: 'center'
        },
        {
          label: '操作',
          customRender: 'operate',
          width: 250,
          align: 'right'
        }
      ]
    };
  },
  methods: {
    // 注销 cardStatus = 2,解挂 cardStatus = 0,挂失 cardStatus = 1
    async editCardHandler(row,cardStatus) {
      if(cardStatus == '2')return
      const { farmerId,cardNo,cardAmount,cardId } = row
      await editCard({
        farmerId,
        cardNo,
        cardAmount,
        cardId,
        cardStatus
      })
      this.handleSearch()
    },
    // 删除
    deleteCardHandler(row){
      const {cardId,farmerName} = row
      this.$modal
      .confirm(`是否确认删除农户${farmerName}`)
      .then(()=> {
        deleteCard({
          cardIds: cardId
        }).then(() => {
          Message({ message: "删除成功", type: 'success' });
          this.handleSearch()
        })
      })
    },
    // 充值
    async rechargeCardHandler() {},
    async searchHandler(farmerName,areaId) {
      const res = await getFarmerList({
        farmerName,
        areaId
      })
      this.userList = res.data
    },
    setCurrentData(areaId) {
      this.currentClickId = areaId
      this.$refs.tableRef.refresh(false);
      this.searchHandler(this.farmerName,areaId)
    },
    showList(){
      this.detailType = false
    },
    async addICard() {
      this.associationManageShow = true
    },
    async loadData() {
      if(!this.currentClickId ) {
        const treeList = this.$store.state.tree.treeList
        let res = []
        if (treeList.code === '000000') {
          res = treeList
        } else {
          res = await getTree()
          this.$store.dispatch('tree/setTree', res)
        }
        this.treeData = res?.data
        this.currentClick = res?.data[0]
        this.currentClickId = this.currentClick?.cusareaId
      }
      this.searchHandler(this.farmerName,this.currentClickId)
      return this.getTableData(this.currentClickId);
    },
    changeDataDetailShow(row) {
      this.detailType = true
      const {cardId} = row
      this.detailCardId = cardId
    },
    getTableData(areaId) {
      switch(this.labelType){
        case 0:
          this.farmerPhone = this.inputContent
          break
        case 1:
          this.cardNo = this.inputContent
          break
        case 2:
          this.farmerName = this.inputContent
          break
        default:
          this.farmerPhone = this.inputContent
      }
      return getCardList({
        farmerName:this.farmerName,
        cardNo:this.cardNo,
        farmerPhone: this.farmerPhone,
        areaId,
        cardStatus: this.cardStatus
      })
    },
    handleSearch() {
      this.$refs.tableRef.refresh(true);
    },
    goDetail(row) {
      this.$refs.sourceCodeDetail.open(row.tarcecodeapplyBatchno)
    },
    rechargeCardHandler(row) {
      this.rechargeCardData = row
      this.rechargeShow = true
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row-container{
  height: 100%;
}
.elrow-main__col-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  .top-left {
    height: 100%;
    display: flex;
    align-items: center;
    i {
      background-color: #40d5ec;
      height: 45%;
      width: 5px;
      margin-right: 5px;
    }
  }
}
.status{
  display: inline-block;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: red;
  margin-left: 5px;
}
</style>
