<template>
  <div>
    <Modal v-model="showInPurchaseOrderModal" title="出库单列表" width="1200">
      <!--内容-->
      <div>
        <Row>
          <!--查询-->
          <Col span="24" />
          <Form id="page_equip_delivery" inline onsubmit="return false" :label-width="70">
            <Form-item label="申请编号:">
              <Input v-model="formObj.num" placeholder="请输入申请编号" style="width:180px" @on-enter="_search" /></Input>
            </Form-item>
            <Form-item label="申请原因:">
              <Input v-model="formObj.reason" placeholder="请输入申请原因" style="width:180px" @on-enter="_search" /></Input>
            </Form-item>
            <Form-item label="申请人:">
              <Input v-model="formObj.applyer" placeholder="请输入申请人" style="width:180px" @on-enter="_search" /></Input>
            </Form-item>
            <Form-item label="申请时间:">
              <Date-picker
                ref="Dates"
                v-model="dateList"
                type="daterange"
                placeholder="操作时间"
                style="width:100%;"
                @on-change="_dateChange"
              />
            </Form-item>
            <Form-item style="margin-left: -70px">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <Col span="24" style="margin-bottom: 10px;" />
          <element-table
            ref="pageTable"
            :page-columns="pageColumns"
            :table-height="tableHeight"
            :get-page="getPage"
            :icon-msg="iconMsg"
            :opt-col-width="70"
            click="true"
            @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.detail">
                  <a @click.stop="_detailModal(scope.row.id)">{{ scope.row[item.key] }}</a>
                </div>
                <div v-else-if="item.date">
                  {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
                </div>
                <div v-else-if="item.dateTime">
                  {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
                </div>
                <div v-else-if="item.status">
                  <span v-if="scope.row.status ===0" class="warning-color">草稿</span>
                  <span v-else-if="scope.row.status ===1" class="blue-color">审批中</span>
                  <span v-else-if="scope.row.status ===2" class="blue-color">待出库</span>
                  <span v-else-if="scope.row.status ===3" class="red-color">审批未通过</span>
                  <span v-else class="green-color">已完成</span>
                </div>
                <div v-else>
                  {{ scope.row[item.key] }}
                </div>
              </template>
            </el-table-column>
          </element-table>
          </Col>
          <Col span="24" style="margin-bottom: 10px" />
           <OutPurchaseOrderOutStock ref="outStock" @on-result-change="_search" />
          </Col>
        </Row>
      </div>
    </Modal>
    <!--一键出库-->
    <LmsEquipDirectOut ref="directOutModal" @on-result-change="_allResultChange" />
  </div>
</template>
<script>
import { equipDeliveryOrder } from '../../../../api'
import LmsEquipDirectOut from './LmsEquipDirectOut.vue'

import OutPurchaseOrderOutStock from './LmsEquipOutInOrderOutStock.vue'

export default {
  components: { OutPurchaseOrderOutStock, LmsEquipDirectOut },
  data() {
    return {
      showInPurchaseOrderModal: false,
      pageColumns: [
        { title: '出库申请单号', key: 'num', width: 200 },
        { title: '状态', key: 'status', width: 120, status: true },
        { title: '申请原因', key: 'reason' },
        { title: '申请人', key: 'applyer', width: 120 },
        { title: '申请时间', key: 'ctime', width: 140, dateTime: true }
      ],
      getPage: {},
      iconMsg: [{ type: 'pt-a-edit', id: '', name: '一键出库' }],
      loading: true,
      formObj: {
        num: '',
        reason: '',
        applyer: '',
        beginDate: '',
        endDate: ''
      },
      dateList: []
    }
  },
  computed: {
    tableHeight: function() {
      return '400'
    }
  },
  methods: {
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'click':
          this._rowChange(data)
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        default:
          this._page()
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '一键出库':
          this._fastOut(data)
          break
      }
    },
    // 一键出库
    _fastOut(data) {
      this.$refs.directOutModal._open(data)
    },
    _openPurchaseOrder() {
      this.showInPurchaseOrderModal = true
      this.formObj = this.$resetFields(this.formObj)
      this.dateList = []
      this._allResultChange()
    },
    _dateChange(data) {
      this.formObj.beginDate = data[0]
      this.formObj.endDate = data[1]
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await equipDeliveryOrder.pageWaitOut(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
        this.$refs.outStock._open()
      }
    },
    _allResultChange() {
      this._page()
      // 清空数据
      this.$refs.outStock._clear()
    },
    _search() {
      this._page()
      this.$emit('on-result-change')
    },
    // 点击行
    _rowChange(currentRow) {
      this.$refs.outStock._open(currentRow)
    }
  }
}
</script>
