<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="物料类型:">
          <Input v-model="formObj.name" placeholder="请输入物料类型" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        @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">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-else-if="item.chemicalStatus">
              <div class="green-color" v-if="scope.row[item.key]===0">入库</div>
              <div class="warning-color" v-else-if="scope.row[item.key]===1">出库</div>
              <div class="blue-color" v-else-if="scope.row[item.key]===2">归还</div>
              <div class="blue-color" v-else-if="scope.row[item.key]===3">校正</div>
            </div>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          label="操作"
          align="center"
          :width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <icon-list
              :msg="iconMsg"
              :row-data="scope.row"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
  </div>
</template>
<script>
import { consumeUseRecord } from '../../../api'
export default {
  components: {},
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      btn: [
        { type: 'success', id: '', name: '导出' },
        { type: 'success', id: '', name: '信息校正' }
      ],
      pageColumns: [
        {
          title: '状态',
          key: 'type',
          width: 120,
          fixed: 'left',
          chemicalStatus: true
        },
        { title: '耗材编号', key: 'chemicalNum', width: 100 },
        { title: '耗材名称', key: 'chemicalName', width: 140 },
        { title: '规格', key: 'spec', width: 130 },
        { title: '耗材类别', key: 'chemicalCategory', width: 140 },
        { title: '单价', key: 'price', width: 100 },
        { title: '出/入库量', key: 'stock', width: 100 },
        { title: '出/入库时间', key: 'regdate', width: 160, dateTime: true },
        { title: '库存量', key: 'allstock', width: 100 },
        { title: '计量单位', key: 'unit', width: 100 },
        { title: '操作人', key: 'realname', width: 100 },
        { title: '操作时间', key: 'ctime', width: 160, date: true },
        { title: '申请人', key: 'applyer', width: 100 },
        { title: '领用部门', key: 'orgNames', width: 100 },
        { title: '有效期', key: 'validDate', width: 120, date: true },
        { title: '生产批号/批次', key: 'batchNum', width: 170 },
        { title: '供应商', key: 'supplier', width: 100 },
        { title: '仓库位置', key: 'storehouse', width: 100 },
        { title: '备注', key: 'remark', width: 230 },
        {
          title: '出入库原因',
          key: 'reason',
          width: 130
        }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' }
        // ,
        // { type: 'pt-a-trash', id: '', name: '删除' }
      ],
      iconMsgDis: [
        { type: 'pt-a-edit', id: '', name: '编辑' }
        // ,
        // { type: 'pt-a-trash', id: '', name: '删除', disabled: true }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await consumeUseRecord.page(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          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.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await consumeUseRecord.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await consumeUseRecord.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
