<template>
  <a-card :bordered="false">
    <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" :sm="24">
            <a-form-item label="上层设备类型">
              <a-tree-select
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :treeData="treeData"
                :treeNodeFilterProp="'title'"
                :showSearch="true"
                v-model="queryParam.parentId"
                placeholder="请选择"
              >
              </a-tree-select>
            </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">
      <a-button style="margin-left: 8px" v-if="$auth('sb-infos-export')" type="primary" icon="download" @click="doExport">导出</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :scroll="{y: BaseTool.Constant.scrollY }"
      showPagination="auto"
    >
      <span slot="action" slot-scope="record">
        <template>
          <a @click="partInfoListView(record)">部件列表</a>
        </template>
      </span>
      <span slot="status" slot-scope="text">
        <badge
          :status="DictCache.COLOR.SB_INFO_STATUS[text]"
          :text="statusMap[text]" />
      </span>
      <span slot="wbFile" slot-scope="record">
        <template>
          <a @click="handleRepairFilePreview(record)">预览</a>
          <a-divider type="vertical" />
          <a @click="handleRepairFileDownload(record)">下载</a>
        </template>
      </span>
      <span slot="useFile" slot-scope="record">
        <template>
          <a @click="handleUseFilePreview(record)">预览</a>
          <a-divider type="vertical" />
          <a @click="handleUseFileDownload(record)">下载</a>
        </template>
      </span>
      <span slot="checkFile" slot-scope="record">
        <template>
          <a @click="handleCheckFilePreview(record)">预览</a>
          <a-divider type="vertical" />
          <a @click="handleCheckFileDownload(record)">下载</a>
        </template>
      </span>
    </s-table>
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal"/>
    <download-modal ref="downloadModal"/>
    <preview-modal ref="previewModal"/>
    <part-info-list ref="partInfoList" />
  </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, exportSbInfo } from '@/api/sb/info'
import PartInfoList from '@/views/part/info/modules/PartInfoList'

// 以设备为维度操作部件
export default {
  name: 'SbInfoForPartInfo',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    PartInfoList,
    Detail,
    DownloadModal,
    PreviewModal
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      depreciationTypeMap: {},
      levelMap: {},
      statusMap: {},
      unitMap: {},
      sourceTypeMap: {},
      treeData: [],
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          checked: true,
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '设备编号',
          checked: true,
          dataIndex: 'no',
          width: 200
        },
        {
          title: '财务编码',
          checked: true,
          dataIndex: 'financingNo',
          width: 200
        },
        {
          title: '设备名称',
          checked: true,
          dataIndex: 'name',
          width: 200
        },
        {
          title: '设备类型',
          checked: true,
          dataIndex: 'type',
          width: 200,
          customRender: (text, record, index) => {
            return record.typeName
          }
        },
        {
          title: '设备等级',
          checked: true,
          dataIndex: 'level',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.levelMap, text)
          }
        },
        {
          title: '设备原值',
          dataIndex: 'initialValue',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '当前价值',
          dataIndex: 'currentValue',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已折旧价值',
          dataIndex: 'cutValue',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '生产商',
          dataIndex: 'producerId',
          width: 200,
          customRender: (text, record, index) => {
            return record.producerName
          }
        },
        {
          title: '设备来源方式',
          dataIndex: 'sourceType',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.sourceTypeMap, text)
          }
        },
        {
          title: '来源设备id',
          dataIndex: 'sourceSbId',
          width: 200,
          customRender: (text, record, index) => {
            return record.sourceSbName
          }
        },
        {
          title: '上级设备',
          dataIndex: 'parentId',
          width: 200,
          customRender: (text, record, index) => {
            return record.parentName
          }
        },
        {
          title: '计量单位',
          dataIndex: 'unit',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.unitMap, text)
          }
        },
        {
          title: '所属车间',
          dataIndex: 'positionId',
          width: 200,
          customRender: (text, record, index) => {
            return record.positionName
          }
        },
        {
          title: '规格型号',
          dataIndex: 'guigeId',
          width: 200,
          customRender: (text, record, index) => {
            return record.guigeName
          }
        },
        {
          title: '保管人',
          dataIndex: 'saveUser',
          width: 200
        },
        {
          title: '购置日期',
          dataIndex: 'buyDate',
          width: 200
        },
        {
          title: '投用日期',
          dataIndex: 'startDate',
          width: 200
        },
        {
          title: '下次检定日期',
          dataIndex: 'nextCheckDate',
          width: 200
        },
        {
          title: '保修截至日期',
          dataIndex: 'guaranteeDate',
          width: 200
        },
        {
          title: '预计工作年限',
          dataIndex: 'workYear',
          width: 200
        },
        {
          title: '预计报废日期',
          dataIndex: 'retirementDate',
          width: 200
        },
        {
          title: '折旧方式',
          dataIndex: 'depreciationType',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.depreciationTypeMap, text)
          }
        },
        {
          title: '维保手册',
          dataIndex: 'wbFile',
          width: 200,
          scopedSlots: { customRender: 'wbFile' }
        },
        {
          title: '使用手册',
          dataIndex: 'useFile',
          width: 200,
          scopedSlots: { customRender: 'useFile' }
        },
        {
          title: '验收文件',
          dataIndex: 'checkFile',
          width: 200,
          scopedSlots: { customRender: 'checkFile' }
        },
        {
          title: '状态',
          checked: true,
          dataIndex: 'status',
          width: 200,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 200
        },
        {
          title: '创建日期',
          dataIndex: 'createdTime',
          width: 200
        },
        {
          title: '操作',
          key: 'action',
          checked: true,
          width: '200px',
          align: 'center',
          fixed: 'right',
          scopedSlots: { width: 200, customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getSbInfoPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return 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)
  },
  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
      }
    },
    partInfoListView (record) {
      const modal = this.$refs.partInfoList
      modal.base({ sbId: record.id }, { sbId: record.id })
    },
    handleOk () {
      this.setTree()
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportSbInfo(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleRepairFilePreview (record) {
      console.log(record, 87878)
      this.$refs.previewModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_REPAIRFILE)
    },
    handleRepairFileDownload (record) {
      // this.$refs.downloadModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_REPAIRFILE, getDownloadUrl(record.id))
    },
    handleUseFilePreview (record) {
      this.$refs.previewModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_USEFILE)
    },
    handleUseFileDownload (record) {
      // this.$refs.downloadModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_USEFILE, getDownloadUrl(record.id))
    },
    handleCheckFilePreview (record) {
      this.$refs.previewModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_RECHECKFILE)
    },
    handleCheckFileDownload (record) {
      // this.$refs.downloadModal.base(record.id, this.DictCache.VALUE.FILE_MODULE.SBINFO_RECHECKFILE, getDownloadUrl(record.id))
    }
  }
}
</script>
