<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"
              :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" />

    <!--库存详情-->
    <LmsChemicalStock ref="stockDetailModal" @on-result-change="_search" />

    <!--归类-->
    <LmsChemicalStockClassify ref="detailClassifyModal" @on-result-change="_searchChemical"></LmsChemicalStockClassify>
  </div>
</template>
<script>
import { consumeLedger } from '../../../api'
import treePage from '../../../components/base/treePage'
import relResourceTree from './tree/relConsumeCategoryTree'
import resourceEdit from './LmsConsumeLedgerEdit'
import LmsChemicalStock from './LmsChemicalStock'
import LmsChemicalStockClassify from './classify/LmsChemicalStockClassify.vue'
export default {
  components: {
    treePage,
    relResourceTree,
    LmsChemicalStock,
    LmsChemicalStockClassify,
    resourceEdit
  },
  data() {
    return {
      searchOpen: false,
      btn: [{ type: 'success', id: '', name: '添加' }],
      iconMsg: [
        { type: 'pt-edit', id: '', name: '编辑' },
        { type: 'pt-edit', id: '', name: '库存详情' },
        { type: 'pt-edit', id: '', name: '归类' }
      ],
      pageColumns: [
        { title: '耗材编号', width: 150, key: 'num', fixed: 'left' },
        {
          title: '耗材名称',
          key: 'name',
          width: 200,
          detail: true,
          fixed: 'left'
        },
        { title: '耗材类别', width: 140, key: 'category' },
        { title: '最低有效期', width: 140, key: 'validDate', date: true },
        { title: '仓库位置', width: 200, key: 'storehouse' },
        { title: '规格', width: 100, key: 'spec' },
        { title: '累计出库量', width: 120, key: 'allOutStock' },
        { title: '库存量', width: 100, key: 'stock', stock: true },
        { title: '计量单位', width: 100, key: 'unit' },
        { title: '使用部门', key: 'useDepartment', width: 100 },
        {
          title: '最低库存量',
          width: 120,
          key: 'lowerStock',
          lowerStock: true
        },
        { title: '等级（纯度）', width: 130, key: 'quality' },
        { title: '保存条件', width: 140, key: 'saveCondition' },
        { title: '保管人', width: 140, key: 'saver' },
        { title: '单价', key: 'unitPrice', width: 100 },
        { title: '制造厂商', width: 200, key: 'manufacturer' },
        { title: '密度', width: 100, key: 'thickness' },
        { title: '精确度等级', width: 150, key: 'unsure' },
        { title: '标准值', width: 100, key: 'standard' },
        { title: '转化率', width: 100, key: 'exchange' },
        { title: '转化库存量', width: 150, key: 'exstock' },
        { title: '转化计量单位', width: 160, key: 'exunit' },
        { title: '备注', width: 140, key: 'remark' }
      ],
      formObj: {
        id: '',
        name: '',
        status: ''
      },
      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: {
    // 刷新
    _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 consumeLedger.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open(this.treeObj, '')
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data)
          break
        case '库存详情':
          this._stockDetailModal(data)
          break
        case '归类':
          this._detaiClassifyModal(data)
          break
      }
    },
    _detaiClassifyModal(data) {
      if (data.exchange === undefined || data.exunit === undefined) {
        this.$Message.warning('请维护转换率和转换计量单位，才能进行归类操作！')
      } else {
        this.$refs.detailClassifyModal._open(data.id, data.exunit)
      }
    },
    _editModal: async function(data) {
      const result = await consumeLedger.getVOById(data.id)
      this.$refs.editModal._open('', result)
    },

    _stockDetailModal(data) {
      this.$refs.stockDetailModal._openStorage(data)
    },
    // 详情
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    },
    _treeData(data) {
      this.formObj.id = data.id
      this.treeObj = data
      this._formSearch()
    }
  }
}
</script>
