<template>
  <div>
    <Modal v-model="showModal" width="1100" class="modal-footer-none zIndex-1100">
      <p slot="header">
        出库单详情
      </p>
      <div>
        <Row>
          <!--查询-->
          <Col span="24" />
          <Form inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <Form-item label="耗材编号:">
              <Input
                v-model="formObj.num"
                placeholder="请输入耗材编号"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>
            <Form-item label="耗材名称:">
              <Input
                v-model="formObj.name"
                placeholder="请输入耗材名称"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>

            <Form-item label="耗材类别:">
              <Input
                v-model="formObj.category"
                placeholder="请输入耗材类别"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>

            <Form-item label="规格:">
              <Input
                v-model="formObj.spec"
                placeholder="请输入规格"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!--按钮-->
          <el-col :span="24">
            <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
          </el-col>
          <!-- 表格 -->
          <Col span="24" />
          <element-table
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            show-check-box
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              :prop="item.key"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <span v-if="item.key==='type'">
                  {{ scope.row[item.key] === 0 ? '流转' : scope.row[item.key] === 1 ? '退回': scope.row[item.key] === 2 ?'更新' :
                    scope.row[item.key] === 3 ? '转发':'文件' }}
                </span>
                <span v-else-if="item.key==='ctime'">{{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}</span>

                <div v-else-if="item.quantity" @click.stop="_handleRow(scope)">
                  <div v-if="statusVal===0|| statusVal===5">
                    <InputNumber v-model="scope.row.quantity" :min="1" @on-change="_quantity" />
                  </div>
                  <div v-else>
                    {{ scope.row[item.key] }}
                  </div>
                </div>

                <span v-else>{{ scope.row[item.key] }}</span>
              </template>
            </el-table-column>
            <el-table-column
              slot="col"
              label="操作"
              align="center"
              :width="200"
              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>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import {
  consumeClassify,
  consumeDeliveryOrder,
  consumeRelDelivery
} from '../../../api'
export default {
  components: {},
  data() {
    return {
      showModal: false,
      searchOpen: false,
      getPage: {},
      selectIds: [],
      footerList: [
        { name: '取消', type: '' },
        { name: '提交审批', type: 'primary' }
      ],
      btn: [
        { type: 'success', id: '', name: '移除' }
        // ,
        // { type: 'success', id: '', name: '导出' },
        // { type: 'success', id: '', name: '打印' }
      ],
      iconMsg: [
        {
          type: 'pt-a-submit',
          id: '',
          name: '保存'
        },
        {
          type: 'pt-a-trash',
          id: '',
          name: '移除'
        }
      ],
      pageColumns: [
        {
          title: '耗材编号',
          key: 'chemicalNum',
          width: 100,
          fixed: 'left',
          detail: true
        },
        { title: '耗材名称', key: 'chemicalName', width: 120, ellipsis: true },
        { title: '出库数量', key: 'quantity', width: 100, quantity: true },
        { title: '计量单位', key: 'unit', width: 140 },
        { title: '有效期', key: 'validDate', width: 140, date: true },
        { title: '生产批次', key: 'batchNum', width: 140 },
        { title: '规格', key: 'spec', width: 100 },
        { title: '等级（纯度）', key: 'quality', width: 150 },
        { title: '供应商', key: 'supplier', width: 160 },
        { title: '制造厂商', key: 'manufacturer', width: 160 },
        { title: '备注', key: 'remark', width: 345, statu: true }
      ],
      formObj: {
        num: '',
        category: '',
        spec: '',
        name: '',
        orderId: ''
      },
      orderId: '',
      statusVal: '',
      isFinish: false,
      submitData: {
        // 要提交至归类的数据
        classifyId: '',
        chemicalIds: []
      },
      currentRow: {},
      currentIndex: -1,
      quantity: 1,
      isStatusVal: false
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal') - 120
    }
  },
  methods: {
    _handleRow(scope) {
      this.currentRow = scope.row
      this.currentIndex = scope.$index
    },
    _quantity(data) {
      this.quantity = data
    },
    _open(data) {
      this.orderId = data.id
      this.formObj.orderId = data.id
      this.statusVal = data.status
      if (this.statusVal !== 0 && this.statusVal !== 5) {
        this.isFinish = true
      } else {
        this.isFinish = false
      }

      this.showModal = true
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const database = this.$serializeForm(this.formObj)
      const result = await consumeRelDelivery.page(database)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _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
      }
    },

    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok(false)
          break
        case '提交审批':
          this._ok()
          break
      }
    },

    _iconClick(res, data) {
      switch (res) {
        case '保存':
          this._saveStore(data.id)
          break
        case '移除':
          this._deleteById(data.id)
          break
      }
    },

    _cancel() {
      this.showModal = false
      this.classifyId = ''
      this.submitData = []
      this.formObj = []
      this._hideLoading()
    },

    _saveStore: async function(id) {
      const result = await consumeRelDelivery.editQuantity({
        id: id,
        quantity: this.quantity
      })
      if (result) {
        this.$Message.success('保存成功！')
        this._page()
      }
    },
    _ok() {
      this.$Modal.confirm({
        title: '提示',
        content: '确定提交采购审批？',
        onOk: () => {
          this._flowSubmit()
          // this.$store
          //   .dispatch('LmsChemicalDeliveryOrder/flowSubmit', this.purchaseId)
          //   .then(() => {
          //     if (this.$store.state.LmsChemicalDeliveryOrder.success) {
          //       this.showOutOrderInfoModal = false
          //       this.$Message.success('审批提交成功！')
          //       this.$emit('on-result-change')
          //     }
          //   })
        }
      })
    },

    _flowSubmit: async function() {
      const result = await consumeDeliveryOrder.flowSubmit(this.orderId)
      if (result) {
        this.showModal = false
        this.$Message.success('审批提交成功！')
        this.$emit('on-result-change')
      }
    },

    _save: async function(data) {
      const result = await consumeClassify.saveClassifyConsume(data)
      if (result) {
        this.$Message.success('归类成功！')
        this.showModal = false
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>
