<template>
  <a-card :bordered="false">
    <div v-show="visible">
      <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="闲置名称">
                <a-input v-model.trim="queryParam.name" 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>

      <div class="table-operator" style="margin-bottom: 8px;">
        <a-button v-if="$auth('purchase-purchase-orders-add')" type="primary" icon="plus" @click="$refs.baseForm.base()">新增</a-button>
        <!--        <a-button v-if="$auth('purchase-purchase-orders-add')" style="margin-left: 8px" type="primary" icon="plus" @click="$refs.addSpareForm.base()">新增物料</a-button>-->
        <!-- <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('purchase-purchase-orders-del')">
          <a-menu slot="overlay">
            <a-popconfirm title="是否要删除所选数据？" @confirm="batchDelete()">
              <a-menu-item key="1"><a-icon type="delete" /><a>删除</a></a-menu-item>
            </a-popconfirm>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown> -->
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
      >
        <span slot="action" slot-scope="record">
          <a @click="handleView(record)">查看</a>
          <!-- <operation-button
            @click="handleEdit(record)"
          >修改</operation-button> -->
          <operation-button
            v-if="record.status===1||record.status===2"
            @click="handleAudit(record)"
          >审核</operation-button>
        </span>
        <template #status="text">
          <badge :text="BaseTool.Object.getField(statusMap,text)" :status="DictCache.COLOR.SB_UNUSED_STATUS[text]"/>
        </template>
      </s-table>
    </div>
    <BaseForm ref="baseForm" @ok="handleOk"/>
    <AuditForm ref="auditForm" @ok="handleOk"/>
    <Detail ref="detail" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getSbUnusedPage, getUnusedInfo } from '@/api/idle-assets/idle-assets'
import BaseForm from './modules/BaseForm.vue'
import AuditForm from './modules/AuditForm.vue'
import Detail from './modules/Detail.vue'
export default {
  name: 'IdleAssets',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    AuditForm,
    Detail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      visible: true,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '名称',
          dataIndex: 'name',
          checked: true,
          width: 100
        },
        {
          title: '审核人',
          dataIndex: 'verifyUserName',
          checked: true,
          width: 130
        },
        {
          title: '申请时间',
          dataIndex: 'createdTime',
          checked: true,
          width: 130
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: '100px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
            sortBy: 'desc',
            sortName: 'created_time'
          }
        }
        return getSbUnusedPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys
        }
      },
      optionAlertShow: false,
      // 下拉框map
      statusMap: {}

    }
  },
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_UNUSED_STATUS)
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },

    handleEnter () {
      this.$refs.table.refresh(true)
    },
    handleAudit (record) {
      this.$refs.auditForm.base(record)
    },
    handleEdit (record) {

    },
    handleView (record) {
      getUnusedInfo({ id: record.id }).then(res => {
        this.$refs.detail.base(res.data)
        this.visible = false
      })
    }
  }
}
</script>
