<!-- 合同付款详情 -->
<template>
  <div class="container-wrap">
    <div class="top-title">
      <span>{{ type }}{{ new Date(payDateTime).getDate() }}日付款统计</span>
      <span style="font-size: 12px;font-weight: normal" @click="$router.back()">
        <Icon type="ios-arrow-dropleft" size="16"/>
        返回上一级
      </span>
    </div>
    <div class="container">
      <div class="tabs">
        <div
          @click="tabClick(index)"
          :class="tabsIndex === index ? 'active' : ''"
          class="tab-item"
          v-for="(item, index) in tabs"
          :key="index">
          {{ item + (index === 0? tabsData.allPay.amount : index === 1? tabsData.directPay.amount: index === 2? tabsData.paperPay.amount : tabsData.heatPay.amount ) + '元' }}
        </div>
      </div>
      <div class="filter">
        账期筛选：
        <Button @click="setaAcountDate()" :type="btnIndex === '' ? 'primary' : 'default'">全部</Button>
        <Button
          :type="btnIndex === index ? 'primary' : 'default'"
          @click="setaAcountDate(item, index)"
          v-for="(item, index) in getTabsData" :key="index">
          {{ item }}天
        </Button>
      </div>
    <!-- 表格 -->
    <div 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 class="table-page">
      <Page :current="params.pageIndex" :total="params.total" :page-size="params.pageSize" :show-total="true" show-elevator @on-change="changePage" />
    </div>
    <my-modal v-model="show" :data="formData"></my-modal>
  </div>
  </div>
</template>

<script>
import { GetContractPayPageList, GetContractSupplier, GetContractPayTechSum } from '@/api/applyPurchase'
import MyTable from '_c/my-table/my-table'
import MyModal from './modal'
export default {
  name: 'material_pay_detail',
  components: {
    MyTable,
    MyModal
  },
  data () {
    return {
      type: '',
      payDateTime: '',
      tabsData: {
        'allPay': {
          'amount': 0,
          'accountDateItems': []
        },
        'directPay': {
          'amount': 0,
          'accountDateItems': []
        },
        'paperPay': {
          'amount': 0,
          'accountDateItems': []
        },
        'heatPay': {
          'amount': 0,
          'accountDateItems': []
        }
      },
      tabs: ['总额', '直喷', '打纸', '热转'],
      tabsIndex: 0,
      btnIndex: '',
      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
        },
        {
          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
      }
    }
  },
  computed: {
    getTabsData () {
      let arr = []
      if (this.tabsIndex === 0) {
        arr = this.tabsData.allPay.accountDateItems
      } else if (this.tabsIndex === 1) {
        arr = this.tabsData.directPay.accountDateItems
      } else if (this.tabsIndex === 2) {
        arr = this.tabsData.paperPay.accountDateItems
      } else if (this.tabsIndex === 3) {
        arr = this.tabsData.heatPay.accountDateItems
      }
      return arr
    }
  },
  methods: {
    /* 点击账期 */
    setaAcountDate (accountDate = '', index = '') {
      this.params.accountDate = accountDate
      this.btnIndex = index
      this.getList()
    },
    /* 跳转账单 */
    toBill () {
      this.$router.push({
        name: 'material_bill'
      })
    },
    /* 选项卡切换 */
    tabClick (index) {
      this.data = []
      this.tabsIndex = index
      this.btnIndex = ''
      this.params.pageIndex = 1
      this.params.accountDate = ''
      if (index === 0) {
        this.params.technologyType = ''
      } else if (index === 1) {
        this.params.technologyType = 0
      } else if (index === 2) {
        this.params.technologyType = 2
      } else if (index === 3) {
        this.params.technologyType = 1
      }
      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()
    },
    /* 获取供应商下的合同列表 */
    getChildrenList (obj, index) {
      GetContractPayPageList({
        payDateTime: this.payDateTime,
        ...obj,
        ...this.bigParams[index]
      }).then(res => {
        this.$set(this.bigArray, index, res.result.list)
        this.$set(this.bigParams[index], 'total', res.result.totalCount)
      })
    },
    getList () {
      /* 获取供应商聚合列表 */
      GetContractSupplier({
        ...this.params,
        payDateTime: this.payDateTime
      }).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
            })
            let obj = {
              accountDate: this.params.accountDate,
              technologyType: this.params.technologyType,
              supplierId: item.supplierId
            }
            this.getChildrenList(obj, index)
          })
        }
      })
    },
    getContractPayTechSum () {
      GetContractPayTechSum({
        payDateTime: this.payDateTime
      }).then(res => {
        if (res.code === 0) {
          this.tabsData = res.result
        }
      })
    }
  },
  mounted () {
    this.payDateTime = this.$route.query.payDateTime || ''
    this.type = this.$route.query.type || ''
    this.getList()
    this.getContractPayTechSum()
  }
}
</script>

<style lang="less" scoped>
.container-wrap {
  height: 100%;
  overflow: hidden;
  .top-title {
    margin-bottom: 18px;
    height: 16px;
    line-height: 16px;
    font-size: 16px;
    font-weight: bold;
    display: flex;
    justify-content: space-between;
  }
  .container {
    padding-bottom: 10px;
    height: ~"calc(100% - 34px)";
    background: #FFFFFF;
    .tabs {
      box-sizing: border-box;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .tab-item {
        box-sizing: border-box;
        flex: 1;
        overflow: auto;
        border-right: 1px solid #dcdee2;
        border-bottom: 1px solid #dcdee2;
        text-align: center;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-weight: bold;
        cursor: pointer;
        &:last-child {
          border-right: none;
        }
        &.active {
          color: #3F92F9;
          border-bottom: 3px solid #3F92F9;
        }
      }
    }
    .filter {
      padding: 0 20px;
      height: 60px;
      line-height: 60px;
      font-weight: bold;
      button {
        margin-right: 10px;
        width: 100px;
      }
    }
    .table-content {
      padding: 0 20px;
      overflow: auto;
      height: ~"calc(100% - 153px)";
      .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>
