<!-- 合同付款 -->
<template>
  <div class="container-wrap">
    <div class="bar-wrap">
      <div class="bar-title">付费统计</div>
      <div class="bar">
        <div class="item">
          <div class="left">本月</div>
          <div class="center">
            <p>{{ new Date(titleData.curMonthPay.firDateTime).getDate() }}日</p>
            <p class="number" @click="toDetail(titleData.curMonthPay.firDateTime, '本月')">
              {{ titleData.curMonthPay.firAmount }}元
              <Icon type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
          <div class="right">
            <p>{{ new Date(titleData.curMonthPay.secDateTime).getDate() }}日</p>
            <p class="number" @click="toDetail(titleData.curMonthPay.secDateTime, '本月')">
              {{ titleData.curMonthPay.secAmount }}元
              <Icon type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
        </div>
        <div class="item">
          <div class="left">下月</div>
          <div class="center">
            <p>{{ new Date(titleData.nextMonthPay.firDateTime).getDate() }}日</p>
            <p class="number" @click="toDetail(titleData.nextMonthPay.firDateTime, '下月')">
              {{ titleData.nextMonthPay.firAmount }}元
              <Icon type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
          <div class="right">
            <p>{{ new Date(titleData.nextMonthPay.secDateTime).getDate() }}日</p>
            <p class="number">
              {{ titleData.nextMonthPay.secAmount }}元
              <Icon @click="toDetail(titleData.nextMonthPay.secDateTime, '下月')" type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
        </div>
        <div class="item">
          <div class="left">隔月</div>
          <div class="center">
            <p>{{ new Date(titleData.lastMonthPay.firDateTime).getDate() }}日</p>
            <p class="number">
              {{ titleData.lastMonthPay.firAmount }}元
              <Icon @click="toDetail(titleData.lastMonthPay.firDateTime, '隔月')" type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
          <div class="right">
            <p>{{ new Date(titleData.lastMonthPay.secDateTime).getDate() }}日</p>
            <p class="number">
              {{ titleData.lastMonthPay.secAmount }}元
              <Icon @click="toDetail(titleData.lastMonthPay.secDateTime, '隔月')" type="ios-arrow-dropright" color="#333" size="20"/>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
    <!--    <my-table :data="data" :columns="columns" :table-filter="tableFilter" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>-->
    <!-- 检索 -->
    <div class="table-filter" @keyup.enter="search">
      <div class="filter-item">
        <Input v-model="key" placeholder="请输入关键字" clearable />
      </div>
      <div class="filter-item">
        <Button type="primary" style="width: 90px;" @click="search">查询</Button>
      </div>
      <div class="filter-item">
        <Button type="primary" style="width: 90px;" @click="exportExcel">导出Excel</Button>
      </div>
      <div class="filter-item" v-if="tabsIndex === 1">
        <RadioGroup v-model="status">
          <Radio :label="0">
            <span>审批中</span>
          </Radio>
          <Radio :label="1">
            <span>进行中</span>
          </Radio>
          <Radio :label="2">
            <span>已完成</span>
          </Radio>
        </RadioGroup>
      </div>
<!--      <div class="filter-item" style="float: right;margin-right: 0">-->
<!--        <Button type="primary" style="width: 90px;">打印详情</Button>-->
<!--      </div>-->
      <div class="filter-item" style="float: right;">
        <Button type="primary" style="width: 90px;" @click="toBill">查看账单</Button>
      </div>
    </div>
    <!-- 表格 -->
    <div v-if="tabsIndex === 0" class="table-content">
      <div v-if="data.length === 0" style="height: 100%;display: flex;justify-content: center;align-items: center">
        暂无数据
      </div>
      <Collapse v-for="(item, index) in data" :key="index">
        <Panel>
          <div class="table-item">
            <div class="row">
              <div class="col">供应商：{{ item.supplierName }}</div>
              <div class="col"></div>
              <div class="col">账期：{{ item.accountPeriod }}天</div>
              <div class="col"></div>
              <div class="col" style="flex: .2"></div>
            </div>
            <div class="row">
              <div class="col">累计采购：{{ item.totalPurQty }}米</div>
              <div class="col orange">累计金额：{{ item.totalAmount }}元</div>
              <div class="col red">累计退货：{{ item.totalBack }}米</div>
              <div class="col"></div>
              <div class="col"></div>
            </div>
          </div>
          <div slot="content" class="content">
            <my-table :data="bigArray[index]" :columns="columns" :isShowFilter="false" :table-page="bigParams[index]" @on-change="ChilchangePage($event, item.supplierId, index)"></my-table>
          </div>
        </Panel>
      </Collapse>
    </div>
    <div v-if="tabsIndex === 1" class="table-content">
      <Table :columns="columns" :data="data"></Table>
    </div>
    <!-- 分页 -->
    <div class="table-page">
      <Page :current="params.pageIndex" :total="params.total" :page-size="params.pageSize" :show-total="true" show-elevator @on-change="changePage" />
      <div class="tabs">
        <div class="tab"
             v-for="(item, index) in tabs" :key="index"
             :class="index === tabsIndex? 'active' : ''"
             @click="tabClick(index)">
          {{ item }}
        </div>
      </div>
    </div>
    <my-modal v-model="show" :data="formData"></my-modal>
  </div>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import { GetContractPayPageList, GetContractSupplier, GetContractPayTotal } from '@/api/applyPurchase'
import MyTable from '_c/my-table/my-table'
import MyModal from './modal'
export default {
  name: 'material_pay',
  components: {
    MyTable,
    MyModal
  },
  data () {
    return {
      titleData: {
        'curMonthPay': {
          'firDateTime': '',
          'firAmount': 0,
          'secDateTime': '',
          'secAmount': 0
        },
        'nextMonthPay': {
          'firDateTime': '',
          'firAmount': 0,
          'secDateTime': '',
          'secAmount': 0
        },
        'lastMonthPay': {
          'firDateTime': '',
          'firAmount': 0,
          'secDateTime': '',
          'secAmount': 0
        }
      },
      tabs: ['供应商视图', '合同视图'],
      tabsIndex: 0,
      bigArray: [],
      bigParams: [],
      status: 0,
      key: '',
      filterData: {},
      formData: {},
      show: false,
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '合同编码',
          key: 'purchaseBillNo',
          minWidth: 180,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  _this.formData = { ...params.row }
                  _this.show = true
                }
              }
            }, params.row.purchaseBillNo)
          }
        },
        {
          title: '交货日期',
          key: 'deliverTime',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            return h('span', _this.$dayjs(params.row.deliverTime).format('YYYY-MM-DD'))
          }
        },
        {
          title: '入库日期',
          key: 'inStockDate',
          minWidth: 150
        },
        {
          title: '质检日期',
          key: 'qtyCheckDate',
          minWidth: 150
        },
        {
          title: '件号',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '规格',
          key: 'spec',
          minWidth: 150
        },
        {
          title: '采购数量',
          key: 'purchaseQty',
          minWidth: 150
        },
        {
          title: '单价',
          key: 'price',
          minWidth: 150
        },
        {
          title: '金额',
          key: 'totalAmount',
          minWidth: 150
        },
        {
          title: '付款期限',
          key: 'payTimeLimit',
          minWidth: 150
        },
        {
          title: '合同状态',
          key: 'contractPayState',
          minWidth: 150
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  methods: {
    /* 跳转订单详情 */
    toDetail (date, type) {
      this.$router.push({
        name: 'material_pay_detail',
        query: {
          payDateTime: date,
          type
        }
      })
    },
    /* 跳转账单 */
    toBill () {
      this.$router.push({
        name: 'material_bill'
      })
    },
    /* 选项卡切换 */
    tabClick (index) {
      this.tabsIndex = index
      this.key = ''
      this.status = 0
      this.data = []
      this.params = {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      }
      this.getList()
    },
    // 跳转对应记录列表 0入库记录 1出库记录
    toHistory (type) {
      if (type === 0) {
        this.$router.push({
          name: 'store_in_record'
        })
      } else if (type === 1) {
        this.$router.push({
          name: 'store_out_record'
        })
      }
    },
    // 获取控件宽度
    getComWidth (item) {
      if (item.name === 'Button') {
        return '80px'
      } else if (item.name === 'Input') {
        return '200px'
      }
    },
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.purchaseBillNo = option.key
          this.params.purContractState = option.purContractState
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'back':
          this.$router.go(-1)
          break
      }
    },
    /* 子列表分页 */
    ChilchangePage (pageIndex, supplierId, index) {
      this.bigParams[index].pageIndex = pageIndex
      this.getChildrenList(supplierId, index)
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    search () {
      this.params.keyWord = this.key
      this.params.contractPayState = this.status
      this.getList()
    },
    exportExcel () {
      GetContractPayPageList({
        ...this.params,
        pageIndex: 1,
        pageSize: 99999
      }).then(res => {
        if (res.code === 0) {
          let data = res.result.list
          exportExcel(this.columns, data, '合同付款')
        }
      })
      // if (this.data.length < 1) return this.$Message.error('数据为空！')
      // if (this.tabsIndex === 1) {
      //   exportExcel(this.columns, this.data, '合同付款')
      // } else {
      //   let arr = []
      //   this.bigArray.forEach((item, index) => {
      //     arr.push(...item)
      //   })
      //   exportExcel(this.columns, arr, '合同付款')
      // }
    },
    /* 获取供应商下的合同列表 */
    getChildrenList (supplierId, index) {
      GetContractPayPageList({
        supplierId,
        ...this.bigParams[index]
      }).then(res => {
        this.$set(this.bigArray, index, res.result.list)
        this.$set(this.bigParams[index], 'total', res.result.totalCount)
      })
    },
    getList () {
      /* 获取供应商聚合列表 */
      if (this.tabsIndex === 0) {
        GetContractSupplier(this.params).then(res => {
          if (res.code === 0) {
            this.bigParams = []
            this.bigArray = []
            this.data = res.result.list
            this.params.total = res.result.totalCount
            // 初始化所有子列表的分页
            this.data.forEach((item, index) => {
              this.$set(this.bigParams, index, {
                pageIndex: 1,
                pageSize: 4,
                total: 10
              })
              this.getChildrenList(item.supplierId, index)
            })
          }
        })
      } else if (this.tabsIndex === 1) {
        this.params.contractPayState = this.status
        GetContractPayPageList(this.params).then(res => {
          if (res.code === 0) {
            this.data = res.result.list
            this.params.total = res.result.totalCount
          }
        })
      }
    },
    getContractPayTotal () {
      GetContractPayTotal().then(res => {
        if (res.code === 0) {
          this.titleData = res.result
        }
      })
    }
  },
  mounted () {
    this.getList()
    this.getContractPayTotal()
  }
}
</script>

<style lang="less" scoped>
.container-wrap {
  height: 100%;
  overflow: hidden;
  .bar-wrap {
    margin-bottom: 10px;
    background: #FFFFFF;
    border-radius: 3px;
    .bar-title {
      margin-bottom: 10px;
      padding: 0 10px;
      height: 25px;
      line-height: 25px;
      border-radius: 3px 3px 0 0;
      background: #dcdee2;
      font-weight: bold;
    }
    .bar {
      padding: 0 20px;
      height: 80px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      .item {
        flex: 1;
        margin-right: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 70px;
        border-radius: 5px;
        background-color: #FFFFFF;
        border: 1px solid #e6e6e6;
        &:last-child {
          margin-right: 0;
        }
        .left, .center, .right {
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-direction: column;
          text-align: center;
          .number {
            margin-top: 5px;
            font-size: 16px;
            font-weight: bold;
            color: #3F92F9;
            cursor: pointer;
            &.red {
              color: #DB0020;
            }
          }
        }
        .left {
          flex: 4;
          font-size: 16px;
          font-weight: bold;
          border-right: 1px solid #e6e6e6;
        }
        .center {
          flex: 5;
        }
        .right {
          flex: 5;
        }
      }
    }
  }
  .container {
  height: ~"calc(100% - 125px)";
  .table-filter {
    padding-bottom: 10px;
    height: 42px;
    line-height: 32px;
    overflow: hidden;
    .filter-item {
      margin-right: 10px;
      float: left;
      //&:last-child {
      //  margin-right: 0;
      //}
    }
  }
  .table-content {
    overflow: auto;
    height: ~"calc(100% - 85px)";
    .table-item {
      width: 100%;
      .row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .col {
          padding: 0 5px;
          flex: 1;
          display: flex;
          align-items: center;
          height: 30px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          word-break: break-all;
          &.red {
            color: red;
          }
          &.orange {
            color: orange;
          }
          &.blue {
            color: #169bd5;
          }
        }
      }
    }
    .content {
      padding-left: 30px;
    }
  }
  .table-page {
    position: relative;
    padding-top: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 43px;
    overflow: hidden;
    .tabs {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      display: flex;
      .tab {
        padding: 5px 0;
        width: 100px;
        border-radius: 5px;
        text-align: center;
        background-color: #FFFFFF;
        cursor: pointer;
        &.active {
          color: #FFFFFF;
          background-color: #3F92F9E5;
        }
      }
    }
  }
}
}
/deep/ .ivu-collapse-header {
  min-width: 750px;
  display: flex;
  height: 60px !important;
  background-color: #FFFFFF;
  border-bottom: none !important;
}
/deep/ .ivu-icon-ios-arrow-forward {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
/deep/ .ivu-collapse-content-box {
  padding: 0;
  padding-bottom: 10px;
}
/deep/ .ivu-collapse {
  margin-bottom: 5px;
}
</style>
