<template>
  <el-row class="el-row-container" v-loading="loading">
    <el-col
      :span="24"
      style="padding: 16px; height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div>
            <el-select
              style="display:inline-block;width: 100px;margin-right: "
              v-model="roleType"
            >
              <el-option
                label="买方"
                value="0"
              >
              </el-option>
              <el-option
                label="卖方"
                value="1"
              >
              </el-option>
            </el-select>
            <el-input
              v-model="trxrcdName"
              style="width: 250px;margin-left: 16px"
              placeholder="请输入买方姓名"
              @keyup.enter.native="handleSearch"
              clearable
            />
            <el-date-picker
              v-model="dateValue"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 300px; margin:0 16px"
            >
            </el-date-picker>
            <el-button
              type="primary"
              size="small"
              @click="handleSearch"
              >查询</el-button
            >
          </div>
          <div class="top-left">
            <el-button
              type="primary"
              size="small"
              @click="addAssociation"
              >新增交易
              </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 #operate="scope">
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="download(scope.row)"
                >修改</el-link
              >
              <el-link
              size="small"
              :underline="false"
              type="danger"
              style="margin-right: 10px"
              @click="download(scope.row)"
              >删除</el-link
            >
            </template>
          </b-table>
        </el-col>
      </el-card>
    </el-col>
    <water-deal
      :visible.sync="associationManageShow"
      :farmerList="farmerList"
      @addSuccess="addSuccessHandler"
    />
  </el-row>
</template>

<script>
import { getFarmerList } from '@/api/farmer/list.js'
import { getTrxrcdList } from '@/api/trxrcd/index.js'
import BTable from '@/components/Table/index.vue';
import WaterDeal from './component/waterDeal.vue'

export default {
  name:'waterMarket',
  components: { BTable, WaterDeal },
  data() {
    return {
      roleType: '0',
      dateValue:"",
      trxrcdName:'',
      loading: false,
      associationManageShow: false,
      columns: [
        {
          label: '卖方农户',
          prop: 'sellerName',
          customRender: '',
          align: 'center'
        },
        {
          label: '买方农户',
          prop: 'buyerName',
          customRender: '',
          align: 'center'
        },
        {
          label: '交易水量',
          prop: 'trxrcdNum',
          customRender: '',
          align: 'center'
        },
        {
          label: '交易价格（元/m³）',
          prop: 'trxrcdPrice',
          customRender: '',
          align: 'center'
        },{
          label: '成交金额（元）',
          prop: 'trxrcdMoney',
          customRender: '',
          align: 'center'
        },{
          label: '成交时间',
          prop: 'trxrcdCreateddate',
          customRender: '',
          align: 'center'
        }
      ],
      farmerList: []
    };
  },
  mounted() {
    this.getFarmerListHandler()
  },
  methods: {
    async getFarmerListHandler() {
      const res = await getFarmerList()
      this.farmerList = res.data
    },
    addAssociation() {
      this.associationManageShow = true
    },
    loadData() {
      return this.getTableData();
    },
    getTableData() {
      if (this.roleType == '0') {
        this.buyerName = this.trxrcdName
        this.sellerName = ''
      } else { 
        this.sellerName = this.trxrcdName
        this.buyerName = ''
      }
      const startTime = this.dateValue[0] || ''
      const endTime = this.dateValue[1] || ''
      const params = {
        buyerName: this.buyerName,
        sellerName: this.sellerName,
        startTime,
        endTime,
      }
      console.log(params, 'dateValuedateValue')
      return getTrxrcdList(params)
    },
    addSuccessHandler() { 
      this.handleSearch()
    },
    handleSearch() {
      this.$refs.tableRef.refresh(true);
    },
    goDetail(row) {
      this.$refs.sourceCodeDetail.open(row.tarcecodeapplyBatchno)
    },
    download(row) {
    }
  }
};
</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;
    }
  }
}
</style>
