<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.batchNum" placeholder="请输入生产批号" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
          <Button type="success" @click="_add">
            添加
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <!--按钮-->
      <Col span="24" />

      <!--      <Button type="primary" style="margin-bottom: 12px" @click="_backCheck">-->
      <!--        退回-->
      <!--      </Button>-->
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        :icon-msg="iconMsg"
        show-check-box
        @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>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>
            <span v-else-if="item.isNot" class="blue-color">
              {{ scope.row[item.key] === 0 ? '否': '是' }}
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <assign-person ref="assignPersonModal" is-change @on-result-change="_userResult" />
    <!--首次入库-->
    <LmsChemicalStockAdd ref="addModal" :chemical="leftData" @on-result-change="_page" />
    <!-- 耗材入库 -->
    <LmsChemicalStockIn ref="inStockModal" @on-result-change="_page"></LmsChemicalStockIn>
  </div>
</template>
<script>
import { consumeStock } from '../../../../api'
import assignPerson from '../../../../components/user-info-single/assignPerson'
import LmsChemicalStockAdd from './LmsChemicalStockAdd'
import LmsChemicalStockIn from './LmsChemicalStockIn'
export default {
  components: {
    assignPerson,
    LmsChemicalStockAdd,
    LmsChemicalStockIn
  },
  data() {
    return {
      formObj: {
        batchNum: '',
        spec: '',
        chemicalId: ''
      },
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '按人分配' }],
      pageColumns: [
        { title: '库存数量', key: 'stock', width: 100 },
        { title: '有效期', key: 'validDate', width: 110, date: true },
        { title: '生产批号', key: 'batchNum', width: 120 },
        { title: '商品编码', key: 'productCode', width: 120 },
        { title: 'CAS编号', key: 'casNum', width: 120 },
        { title: '规格', key: 'spec', width: 150 },
        { title: '供应商', key: 'supplier', width: 150 },
        { title: '价格', key: 'unitPrice', width: 150 },
        { title: '制造厂商', key: 'manufacturer', width: 150 },
        { title: '备注', key: 'remark', width: 220 }
      ],
      getPage: {},
      sampleIds: [],
      leftData: {},
      orderId: '',
      chemicalId: '',
      selectIds: [], // 检测项目id
      iconMsg: [
        // { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-submit', 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') - 60
      } else {
        return this.$tableHeight('noSearch') - 60
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      if (this.sampleIds.length !== 0 || this.getPage.records === []) {
        this.$refs.pageTable._pageChange(1)
      }
    },
    _init() {
      this.getPage = []
      this.$refs.pageTable._initTable()
    },
    _submitCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定提交该数据？',
          loading: true,
          onOk: () => {
            this._submitOk(this.selectIds)
          }
        })
      }
    },
    _submitOk: async function(id) {
      if (!id.length) return false
      const result = await consumeStock.submitCheck(id)
      if (result) {
        this.$Modal.remove()
        this.$Message.success('提交成功!')
        this.$emit('on-result-change')
        await this._page()
      }
    },
    _backCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定退回 ' + this.selectIds.length + ' 条记录？',
          onOk: () => {
            this.$refs.reasonModal._open('退回原因')
          }
        })
      }
    },
    _userResult: async function(data) {
      const tempData = {}
      tempData.userId = data.userId
      tempData.userName = data.realname
      const result = await consumeStock.allocateItem({
        ids: this.selectIds.join(','),
        obj: tempData
      })
      if (result) {
        this.$Message.success('分配成功！')
        this._page()
      }
    },
    _page: async function() {
      this.getPage.records = []
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await consumeStock.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 '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
        case 'selectIds':
          this.selectIds = data
          break
      }
    },

    _open(data, purchaseId) {
      this.leftData = data
      this.formObj = {}
      this.orderId = purchaseId
      this.chemicalId = data.chemicalId
      this.formObj.chemicalId = data.chemicalId
      this.formObj.spec = data.spec
      console.log('-------------------aaa', this.formObj)
      if (undefined !== data.chemicalId && undefined !== purchaseId) {
        this._page()
      } else {
        this.sampleIds = []
        this.getPage.records = []
      }
    },

    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '入库':
          this._inStock(data)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },

    _inStock(data) {
      // 入库
      this.$refs.inStockModal._open(data, this.leftData, this.orderId)
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await consumeStock.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await consumeStock.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _reasonResult: async function(data) {
      const result = await consumeStock.backCheck({
        remark: data,
        ids: this.selectIds
      })
      if (result) {
        this.$Message.success('退回成功')
        this.$emit('on-result-change')
        this._page()
      }
    },
    // 首次入库
    _add() {
      if (this.formObj.chemicalId === '') {
        this.$Message.warning('请选择一条耗材数据！')
      } else {
        this.$refs.addModal._open(this.formObj.chemicalId)
      }
    }
  }
}
</script>
