<template>
  <div class="export-tax">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-select v-model="search.paymentType" placeholder="请选择账款类型" clearable>
            <el-option
              v-for="item in constant.payable_accounts_payment_type"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.status" placeholder="请选择支付状态" clearable>
            <el-option
              v-for="item in constant.payable_accounts_payment_status"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <el-date-picker
          v-model="createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="产生时间日期范围"
          end-placeholder="产生时间日期范围"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="max-width: 350px;"
        />
        <el-date-picker
          v-model="deadlineTime"
          type="daterange"
          range-separator="至"
          start-placeholder="付款截止日期范围"
          end-placeholder="付款截止日期范围"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="max-width: 350px;"
        />
        <div>
          <el-input v-model="search.voucherNo" placeholder="付款凭证号" />
        </div>
        <div>
          <el-date-picker
            v-model="paymentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="支付时间日期范围"
            end-placeholder="支付时间日期范围"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </div>
        <div class="right-btn">
          <el-button type="primary" @click="fetchData('reset')">重置</el-button>
          <el-button type="primary" @click="fetchData('search')">查询</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="ExportTax"
    />

    <cy-dialog ref="editDialog" title="付款" :width="'600px'" :top="'13vh'">
      <payment v-if="showDialog" ref="paymentForm" :money="money" :unit="unit" :info-id="infoId" />
      <div class="submit-btn" style="margin: 20px 0;">
        <el-button style="width: 150px;" @click="() => { $refs.editDialog.show = false;showDialog=false }">取消</el-button>
        <el-button type="primary" style="width: 150px;" @click="submit">确定</el-button>
      </div>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getList, submitPayment } from '@/api/myAccounts/payment'
import { formatMoney, codeChangeName } from '@/utils'
import Payment from './payment.vue'

export default {
  name: 'MyPayment',
  components: { Payment },
  data() {
    return {
      count: 0,
      search: {
        paymentType: '',
        applyNo: '',
        salesContractNo: '',
        status: '',
        voucherNo: ''
      },
      page: 1,
      size: 10,
      deadlineTime: [],
      paymentTime: [],
      createTime: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '账款类型',
          prop: 'paymentType',
          showTooltip: true,
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.payable_accounts_payment_type, row.paymentType, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.payable_accounts_payment_type, row.paymentType, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '应付金额',
          showTooltip: true,
          prop: 'paymentAmount',
          render: (h, row) => {
            return <div>{ row.paymentCurrency + ' ' + formatMoney(row.paymentAmount) }</div>
          }
        },
        {
          label: '产生时间',
          showTooltip: true,
          prop: 'gmtCreate'
        },
        {
          label: '付款截止时间',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.paymentDeadline }</div>
          }
        },
        {
          label: '支付状态',
          showTooltip: true,
          prop: 'status',
          render: (h, row) => {
            return <div> <el-tooltip class='item' effect='dark' content={codeChangeName(this.constant.payable_accounts_payment_status, row.status, 'dictValue', 'dictLabel')} placement='top'>
              <div class='one-ellipsis'>
                {codeChangeName(this.constant.payable_accounts_payment_status, row.status, 'dictValue', 'dictLabel')}
              </div>
            </el-tooltip></div>
          }
        },
        {
          label: '付款凭证号',
          showTooltip: true,
          prop: 'voucherNo'
        },
        {
          label: '支付时间',
          showTooltip: true,
          prop: 'paymentTime'
        },
        {
          label: '操作',
          width: 150,
          render: (h, row) => {
            const btnList = [
              {
                msg: '付款',
                icon: 'iconfont icon-a-Group8151',
                rowPower: '1',
                category: 'edit'
              },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                power: '',
                category: 'see'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) && <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} style={item.style} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          }
        }

      ],
      loading: false,
      showDialog: false,
      money: '',
      unit: '',
      infoId: ''
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.status)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['payable_accounts_payment_type', 'payable_accounts_payment_status'])
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type) {
        this.page = 1
        this.size = 10
        if (type === 'reset') {
          for (const key in this.search) {
            this.search[key] = ''
          }
          this.deadlineTime = []
          this.paymentTime = []
          this.createTime = []
        }
      }
      const params = {
        ...this.search,
        deadlineStartTime: this.deadlineTime.length ? this.deadlineTime[0] : '',
        deadlineEndTime: this.deadlineTime.length ? this.deadlineTime[1] : '',
        paymentStartTime: this.paymentTime.length ? this.paymentTime[0] : '',
        paymentEndTime: this.paymentTime.length ? this.paymentTime[1] : '',
        gmtCreateStartTime: this.createTime.length ? this.createTime[0] : '',
        gmtCreatEndTime: this.createTime.length ? this.createTime[1] : '',
        page: this.page || 1,
        rows: this.size || 10
      }
      getList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.rows
        this.count = Number(data.records)
      })
    },

    addContract() {
      this.$router.push({
        name: 'SalesContractAdd',
        query: {
          type: 'add'
        }
      })
    },
    handlerOperate(type, info) {
      if (type === 'edit') {
        this.money = info.paymentAmount
        this.unit = info.paymentCurrency
        this.infoId = info.id
        this.$refs.editDialog.show = true
        this.showDialog = true
      } else if (type === 'see') {
        this.$router.push({
          name: 'PaymentView',
          query: {
            type: 'view',
            id: info.id
          }
        })
      }
    },
    submit() {
      this.$refs.paymentForm.getParams().then(res => {
        console.log(res, 'helo')
        submitPayment(res).then(r => {
          this.$message.success('提交成功')
          this.showDialog = false
          this.$refs.editDialog.show = false
        })
      })
    }
  }
}
</script>

  <style lang="scss" scoped>
      .export-tax {

      }
  </style>
  <style lang="scss">
      .export-tax {
          .icon-a-Group957, .icon-a-Group1087 {
              font-size: 28px;
          }
      }
  </style>
