<template>
  <div>
    <treePage title="仪器类别列表" @on-result-change="_refresh">
      <template slot="tree">
        <relResourceTree ref="treeModal" @on-result-change="_treeData" />
      </template>
      <template slot="cont">
        <el-row>
          <!--查询-->
          <el-col :span="24">
            <Form
              v-show="searchOpen"
              onsubmit="return false"
              :label-width="60"
              inline
            >
              <label class="label-sign" />
              <Form-item class="search-item" label="名称:">
                <el-input v-model="formObj.name" placeholder="请输入名称" clearable @keyup.enter.native="_formSearch" />
              </Form-item>
              <Form-item class="search-btn">
                <el-button type="primary" @click="_formSearch">
                  搜索
                </el-button>
              </Form-item>
            </Form>
          </el-col>
          <!--按钮-->
          <el-col :span="24">
            <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
          </el-col>
          <!--table-->
          <el-col :span="24">
            <element-table
              ref="pageTable"
              :page-columns="pageColumns"
              :table-height="tableHeight"
              :get-page="getPage"
              :icon-msg="iconMsg"
              show-check-box
              :opt-col-width="70"
              @on-result-change="_tableResultChange"
            >
              <el-table-column
                v-for="item in pageColumns"
                :key="item.key"
                show-overflow-tooltip
                sortable
                :prop="item.key"
                :label="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
              >
                <template slot-scope="scope">
                  <a v-if="item.detail" @click.stop="_detailModal(scope.row.id)">
                    {{ scope.row[item.key] }}
                  </a>
                  <div v-else-if="item.key==='icon'">
                    <i class="iconfont" :class="scope.row[item.key]" /> {{ scope.row[item.key] }}
                  </div>
                  <div v-else-if="item.key==='type'">
                    <div v-if="scope.row[item.key]===0" class="green-color">
                      PC菜单
                    </div>
                    <div v-else-if="scope.row[item.key]===2" class="blue-color">
                      APP菜单
                    </div>
                    <div v-else class="warning-color">
                      按钮
                    </div>
                  </div>
                  <div v-else-if="item.key==='status'">
                    <div v-if="scope.row[item.key]===0" class="red-color">
                      禁用
                    </div>
                    <div v-else class="blue-color">
                      正常
                    </div>
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </template>
              </el-table-column>
            </element-table>
          </el-col>
        </el-row>
      </template>
    </treePage>
    <!--添加编辑-->
    <resourceEdit ref="editModal" @on-result-change="_search" />
    <!-- 查看详情 -->
    <LmsEquipInfoDetail ref="detailModal"></LmsEquipInfoDetail>
    <!--生成出库单-->
    <LmsEquipOutOrder ref="outOrderModal" @on-result-change="_search" />
  </div>
</template>
<script>
import { equipInfo, equipRelDelivery } from '../../../api'
import treePage from '../../../components/base/treePage'
import relResourceTree from './tree/relResourceTree'
import resourceEdit from './lmsEquipEdit'
import LmsEquipOutOrder from './out-apply-order/LmsEquipOutOrder'
import LmsEquipInfoDetail from './LmsEquipInfoDetail.vue'
export default {
  components: {
    treePage,
    relResourceTree,
    LmsEquipInfoDetail,
    LmsEquipOutOrder,
    resourceEdit
  },
  data() {
    return {
      searchOpen: false,
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'success', id: '', name: '生成出库单' }
      ],
      iconMsg: [{ type: 'pt-a-view', id: '', name: '详情' }],
      pageColumns: [
        {
          title: '仪器名称',
          key: 'name',
          width: 120,
          detail: true,
          fixed: true
        },
        { title: '仪器类别', key: 'className', width: 120 },
        { title: '仪器编号', key: 'labNum', width: 160, fixed: 'left' },
        { title: '规格型号', key: 'spec', width: 160 },
        { title: '仪器品牌', key: 'brand', width: 160 },
        { title: '出厂编号', key: 'equipNum', width: 160 },
        { title: '仪器类别', key: 'className', width: 160 },
        { title: '制造厂商', key: 'productPlace', width: 160 },
        { title: '供应商名称', key: 'supplierName', width: 160 },
        { title: '仪器价格', key: 'price', width: 160 },
        { title: '计量类型', key: 'meterType', width: 160 },
        { title: '精确度等级', key: 'uncertainty', width: 160 },
        { title: '分辨力', key: 'accuracy', width: 160 },
        { title: '使用范围', key: 'range', width: 160 },
        { title: '设备管理人', key: 'equipAdminName', width: 160 },
        { title: '使用科别', key: 'departmentName', width: 160 },
        { title: '证书编号', key: 'ceNum', width: 160 },
        { title: '仪器位置', key: 'locationName', width: 160 },
        {
          title: '购买日期',
          key: 'buyDate',
          width: 100,
          date: true
        },
        {
          title: '接收日期',
          key: 'receiveDate',
          width: 100,
          date: true
        },
        {
          title: '启用日期',
          key: 'startUseDate',
          width: 100,
          date: true
        },
        {
          title: '有效日期',
          key: 'effectDate',
          width: 100,
          date: true
        },
        { title: '期间核查周期（月）', key: 'periodCheckCycle', width: 160 },
        { title: '仪器来源', key: 'source', width: 100 },
        { title: '备注', key: 'remark' }
      ],
      selectIds: [],
      formObj: {
        id: '',
        name: '',
        status: '',
        systemId: ''
      },
      getPage: {},
      statusList: [
        {
          value: '0',
          name: '禁用'
        },
        {
          value: '1',
          name: '正常'
        }
      ],
      treeObj: {}
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._search()
  },
  methods: {
    // 选择系统返回值
    _systemResult(data) {
      this.formObj.systemId = data
    },
    // 刷新
    _refresh() {
      this.formObj.id = ''
      this.treeObj = {}
      this._formSearch()
    },
    _search() {
      this._formSearch()
      this.$refs.treeModal._open()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await equipInfo.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this._add()
          break
        case '生成出库单':
          this._createOutOrder()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },

    _add() {
      const ids = this.selectIds
      if (ids === '' || undefined === ids || ids.length === 0) {
        this.$Message.warning('请选中要申请的设备！')
      } else {
        this._addPurIds(ids)
      }
    },
    _createOutOrder() {
      this.$refs.outOrderModal._open()
    },

    _addPurIds: async function(ids) {
      const result = await equipRelDelivery.addPros(ids)
      if (result) {
        this.$Message.success('成功加入申请单！')
        this.selectIds = []
      }
      // this.$store.dispatch('LmsEquipOutStorage/addPros', ids).then(() => {
      //   if (this.$store.state.LmsEquipOutStorage.success) {
      //     this.$Message.success('成功加入申请单！');
      //     // this.$refs.selection.selectAll(false);
      //     this.selectIds = [];
      //   }
      // });
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '详情':
          this._editModal(data.id)
          break
      }
    },
    _editModal: async function(data) {
      const result = await equipInfo.getById(data)
      this.$refs.detailModal._open(result)
    },
    // 详情
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    },
    _treeData(data) {
      this.formObj.id = data.id
      this.treeObj = data
      this._formSearch()
    }
  }
}
</script>
