<template>
  <a-card :bordered="false">
    <a-row :gutter="8">
      <a-col :span="4">
        <a-tree
          @expand="onExpand"
          :expandedKeys="expandedKeys"
          :autoExpandParent="true"
          @select="onSelect"
          :selectedKeys="selectedKeys"
          :treeData="treeData"
        />
      </a-col>
      <a-col :span="20">
        <div v-show="visible">
          <div class="table-page-search-wrapper" >
            <a-form layout="inline">
              <a-row :gutter="48">



              </a-row>
            </a-form>
          </div>

          <div class="table-operator">

          </div>

          <a-table
            :data-source="data"
            :columns="columns"
            tableLayout="auto"
            rowKey="id">
              <span slot="action" slot-scope="record">
                <template>
                  <a v-if="$auth('sb-infos-edit')" @click="handleView(record)">修改</a>
                  <a-divider type="vertical" />
                  <a-popconfirm v-if="$auth('sb-infos-del')" title="是否要删除该条数据？" @confirm="batchDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </template>
              </span>
          </a-table>
        </div>
      </a-col>
    </a-row>
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal" @ok="handleOk"/>
    <download-modal ref="downloadModal"/>
    <preview-modal ref="previewModal"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseForm from './modules/BaseForm'
import Detail from './modules/Detail'
import DownloadModal from '@/views/download/DownloadModal'
import PreviewModal from '@/views/preview/PreviewModal'
import { getSbInfoPage, deleteSbInfos, fetchSbInfo, exportSbInfo, getSbInfoTree } from '@/api/sb/model'
import { selectSpareInfoListByModelId } from '@/api/sb/modelbom'
import { fetchSpareTypeAndSbModelTree } from '@/api/sqarepartmanage/sparetype'

import BaseTool from '../../../utils/tool'

export default {
  name: 'SbModelList',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    Detail,
    DownloadModal,
    PreviewModal
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      depreciationTypeMap: {},
      levelMap: {},
      statusMap: {},
      unitMap: {},
      treeData: [],
      data: [],
      expandedKeys: [],
      selectedKeys: [],
      sourceTypeMap: {},
      visible: true,
      // 表头
      columns: [
         {
          title: '备件编号',
          dataIndex: 'spareId'
        },
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '编码',
          dataIndex: 'no'
        },
        {
          title: '自编号',
          dataIndex: 'zbh'
        },
        {
          title: '规格型号',
          dataIndex: 'ggxh'
        },
        {
          title: '数量',
          dataIndex: 'num'
        },
        {
          title: '助记码',
          dataIndex: 'zjm'
        },
        //{
         // title: '操作',
         // key: 'action',
         // width: '200px',
         // checked: true,
        //  align: 'center',
          // fixed: 'right',
         // scopedSlots: { width: 200, customRender: 'action' }
        //}
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return selectSpareInfoListByModelId(Object.assign(parameter, this.queryParam))
          .then(res => {
            this.data = res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    this.tableOption()
    this.depreciationTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_DEPRECIATIONTYPE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_LEVEL)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_STATUS)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.sourceTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBTYPE_SOURCETYPE)
    fetchSpareTypeAndSbModelTree().then(res => {
      this.treeData = res.data
    })
  },
  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
      }
    },
    batchDelete (id) {
      let ids = []
      if (this.BaseTool.String.isBlank(id)) {
        const length = this.selectedRows.length
        if (length === 0) {
          this.$message.info('请选择要删除的记录')
          return
        }
        ids = this.selectedRows.map(item => item.id)
      } else {
        ids = [id]
      }
      deleteSbInfos(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    },
    handleEdit (record) {
      selectSpareInfoListByModelId({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        const data = {data:res.data,modelId:record.id}
        modal.base(data)
      })
    },
    handleView (record) {
      this.visible = false
      //selectSpareInfoListByModelId({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        modal.base(record)
      //})
    },
    handleCopy (record) {
      fetchSbInfo({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        res.data.id = null
        modal.base(res.data)
      })
    },
    handleOk () {
      this.visible = true

    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }

    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportSbInfo(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    onSelect: function (selectedKeys, info) {
      this.selectedKeys = selectedKeys
      this.queryParam.id = selectedKeys.length > 0 ? selectedKeys[0] : ''
      selectSpareInfoListByModelId({ id: this.queryParam.id})
      .then(res => {
        this.data = res.data
      })
    },
    onExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },

  }
}
</script>
