<template>
  <a-card :bordered="false">
    <div v-if="visible">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="关键字">
                <a-input v-model="queryParam.keyword" placeholder="请输入名称/类型名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8 || 24" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="small"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="action" slot-scope="record">
          <template>
            <a @click="handleAudit(record)">审批</a>
          </template>
        </span>
      </s-table>
    </div>
    <audit ref="auditModal" @ok="handleOk"/>
    <purchase-plan-audit-form ref="purchasePlanAuditModal" @ok="handleOk"/>
    <purchase-apply-audit-form ref="purchaseApplyAuditModal" @ok="handleOk"/>
    <purchase-demand-plan-audit-form ref="purchaseDemandPlanAuditModal" @ok="handleOk"/>
    <purchase-demand-plan-audit-add-form ref="purchaseDemandPlanAuditAddModal" @ok="handleOk"/>
    <suplier-audit-form ref="supplierAuditModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import Audit from '../audit/Audit'
import PurchasePlanAuditForm from '@/views/purchase/purchase-plan/modules/AuditForm'
import PurchaseApplyAuditForm from '@/views/purchase/purchase-apply/modules/AuditForm'
import PurchaseDemandPlanAuditForm from '@/views/purchase/purchase-demand-plan/modules/AuditForm'
import PurchaseDemandPlanAuditAddForm from '@/views/purchase/purchase-demand-plan/modules/AuditAddForm'
import SuplierAuditForm from '@/views/purchase/supplier/modules/AuditForm'
import { getTaskPage } from '@/api/activiti/activiti'

export default {
  name: 'TaskList',
  components: {
    STable,
    Ellipsis,
    PurchasePlanAuditForm,
    PurchaseApplyAuditForm,
    PurchaseDemandPlanAuditForm,
    SuplierAuditForm,
    PurchaseDemandPlanAuditAddForm,
    Audit
  },
  data () {
    return {
      mdl: {},
      // 查询参数
      queryParam: {
      },
      // 表头

      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '类型',
          dataIndex: 'targetCode',
          customRender: (text, record, index) => {
            if (text === 'purchase_plan') {
              return '采购计划'
            } else if (text === 'purchase_apply') {
              return '采购申请'
            } else if (text === 'purchase_demand_plan') {
              return '采购总需求计划'
            } else if (text === 'purchase_demand_plan_add') {
              return '采购总需求计划增补'
            } else if (text === 'supplier') {
              return '供应商'
            }
          }
        },
        {
          title: '任务名称',
          dataIndex: 'targetName'
        },
        {
          title: '申请人',
          dataIndex: 'userName'
        },
        {
          title: '创建时间',
          dataIndex: 'applyTime'
        },
        {
          title: '操作',
          key: 'action',
          width: '220px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getTaskPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      menus: [],
      visible: true,

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleAudit (record) {
      this.visible = false
      if (record.targetCode === 'purchase_plan') {
        this.$refs.purchasePlanAuditModal.base(record)
      } else if (record.targetCode === 'purchase_apply') {
        this.$refs.purchaseApplyAuditModal.base(record)
      } else if (record.targetCode === 'purchase_demand_plan') {
        this.$refs.purchaseDemandPlanAuditModal.base(record)
      } else if (record.targetCode === 'purchase_demand_plan_add') {
        this.$refs.purchaseDemandPlanAuditAddModal.base(record)
      } else if (record.targetCode === 'supplier') {
        this.$refs.supplierAuditModal.base(record)
      }
      // this.$refs.auditModal.base(record)
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less" scoped>

</style>
