<template>
  <template-page
    v-if="showTable"
    ref="pageRef"
    :get-list="[getList1, getList2][pageLevel]"
    :exportList="[exportList1, exportList2][pageLevel]"
    :options-evens-group="[optionsEvensGroup1, optionsEvensGroup2][pageLevel]"
    :operation="[operation1, operation2][pageLevel]()"
    :expCode="['exp', 'detailExp'][pageLevel]"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
    :moreParameters="moreParameters"
    :column-parsing="columnParsing"
  >
    <div slot="moreSearch" v-if="pageLevel == 1">
      <el-radio-group v-model="tagStatus" size="mini" @change="changeType">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button :label="false">未收款</el-radio-button>
        <el-radio-button :label="true">已收款</el-radio-button>
      </el-radio-group>
      <br /><br />
    </div>
    <div class="cartographer_big">
      <el-dialog title="费用明细" width="100%" :modal="false" :visible.sync="formBool" :before-close="handleClose">
        <zj-page-container v-if="formBool">
          <zj-page-fill class="neibuview">
            <zj-form-container ref="formRef" :form-data="formData" :form-attributes="{ size: 'mini' }">
              <zj-form-module title="基本信息" label-width="130px" :form-data="formData" :form-items="formItems" />
            </zj-form-container>
          </zj-page-fill>
          <div>
            <div style="box-sizing: border-box; padding: 10px; text-align: right">
              <el-button size="mini" @click="handleClose">取 消</el-button>
              <el-button v-if="!formEdit" size="mini" @click="formConfirm" type="primary">确 定</el-button>
            </div>
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
    <website-parts-shop-sales-information
      v-if="showFromBool"
      :item="{ ...formData }"
      @success="
        () => {
          $refs.pageRef.refreshList()
        }
      "
      @goBack="
        () => {
          showFromBool = false
          $refs.pageRef.refreshList()
        }
      "
    />
    <pay-popup-window
      v-if="visible"
      :item="rowData"
      :visible="visible"
      @success="
        () => {
          $refs.pageRef.refreshList()
        }
      "
      @setVisible="
        bool => {
          visible = bool
          rowData = null
          $refs.pageRef.refreshList()
        }
      "
    />
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
import {
  partsCreditGatherList,
  partsCreditGatherListExport,
  partsCreditList,
  partsCreditListExport,
  ppartsCreditDetail
} from '@/api/masterAccountingManagement'
import PayPopupWindow from './components/pay-popup-window.vue'
import WebsitePartsShopSalesInformation from './components/website-parts-shop-sales-information.vue'
export default {
  components: { TemplatePage, PayPopupWindow, WebsitePartsShopSalesInformation },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
      pageLevel: 0,
      showTable: true,
      collectItem: {},
      formData: {},
      formBool: false,
      formEdit: false,
      tagStatus: '',
      visible: false,
      rowData: null,
      showFromBool: false
    }
  },
  computed: {
    optionsEvensGroup1() {
      return []
    },
    optionsEvensGroup2() {
      return [
        [
          [
            {
              name: '返回汇总页面',
              click: () => {
                this.pageLevel = 0
              }
            }
          ]
        ]
      ]
    },
    // 更多参数
    moreParameters() {
      return []
    },
    formItems() {
      return []
    }
  },
  watch: {
    pageLevel() {
      this.showTable = false
      this.$nextTick(() => {
        this.showTable = true
      })
    }
  },
  methods: {
    changeType(val) {
      this.$refs.pageRef.refreshList()
    },
    getList1(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      try {
        cb && cb(pam)
        return partsCreditGatherList(pam)
      } catch (err) {}
    },
    exportList1: partsCreditGatherListExport,
    getList2(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      try {
        pam.params.push({ param: 'a.websit_id', compare: '=', value: this.collectItem.websitId })
        pam.params.push({ param: 'a.worker_id', compare: '=', value: this.collectItem.workerId })
        if (this.tagStatus !== '') {
          pam.params.push({ param: 'a.credit_status', compare: '=', value: this.tagStatus })
        }
        cb && cb(pam)
        return partsCreditList(pam)
      } catch (err) {}
    },
    exportList2: partsCreditListExport,
    operation1() {
      return this.operationBtn({
        detail: {
          click: ({ row, index, column }) => {
            this.collectItem = { ...row }
            this.tagStatus = ''
            this.pageLevel = 1
          }
        }
      })
    },
    operation2() {
      return this.operationBtn({
        view: {
          conditions: ({ row, index, column }) => {
            return true
          },
          click: ({ row, index, column }) => {
            ppartsCreditDetail({ id: row.id }).then(res => {
              this.formData = res.data
              this.$nextTick(() => {
                this.showFromBool = true
              })
            })
          }
        },
        collection: {
          prompt: '是否收款?',
          conditions: ({ row, index, column }) => {
            return !row.creditStatus
          },
          click: ({ row, index, column }) => {
            ppartsCreditDetail({ id: row.id }).then(res => {
              this.rowData = res.data
              this.$nextTick(() => {
                this.visible = true
              })
            })
          }
        }
      })
    },
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    // 关闭弹窗
    handleClose() {
      this.$data.formData = this.$options.data().formData
      this.formBool = false
      this.formEdit = false
    },
    // 收款
    formConfirm() {}
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;

  ::v-deep & > .zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    & > div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>
