<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="状态:">
          <Select v-model="formObj.status" clearable placeholder="请选择状态">
            <Option value="0">
              草稿
            </Option>
            <Option value="1">
              待审批
            </Option>
            <Option value="2">
              审批通过
            </Option>
            <Option value="6">
              驳回
            </Option>
            <Option value="10">
              审批未通过
            </Option>
          </Select>
        </Form-item>
        <Form-item class="search-item" label="申请单号:">
          <Input v-model="formObj.num" placeholder="请输入申请单号" clearable @on-enter="_formSearch" />
        </Form-item>

        <Form-item class="search-item" label="申请原因:">
          <Input v-model="formObj.applyReason" placeholder="请输入申请原因" clearable @on-enter="_formSearch" />
        </Form-item>

        <Form-item class="search-item" label="申请时间:">
          <Date-picker
            type="datetimerange"
            split-panels
            style="width:100%;"
            placeholder="请选择申请时间"
            :editable="false"
            @on-change="_ctimeChange"
          />
          <input v-model="formObj.beginDate" type="hidden" name="pcTimeBegin">
          <input v-model="formObj.endDate" type="hidden" name="pcTimeEnd">
        </Form-item>

        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <!--      <Button type="success" style="margin-top: 5px;margin-bottom: 5px;" @click="_createApplyOrder">-->
      <!--        创建申请单-->
      <!--      </Button>-->
      <!--      <Button type="success" style="margin-top: 5px;margin-bottom: 5px;" @click="_deleteApplyOrder">-->
      <!--        删除-->
      <!--      </Button>-->
      </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.status">
              <span v-if="scope.row[item.key] ===0" class="warning-color">草稿</span>
              <span v-else-if="scope.row[item.key] ===1" class="blue-color">审批中</span>
              <span v-else-if="scope.row[item.key] ===2" class="blue-color">待出库</span>
              <span v-else-if="scope.row[item.key] ===3" class="red-color">审批未通过</span>
              <span v-else-if="scope.row[item.key] ===5" class="red-color">驳回</span>
              <span v-else>已完成</span>
            </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="scope.row.status===0 || scope.row.status===5 ? iconMsgCheck:iconMsg"
              :row-data="scope.row"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!-- 出库单详情 -->
    <LmsEquipOutOrder ref="outOrderDetailCanEditModal" @on-result-change="_page" />


    <!-- 出库详情 已提交-->
    <LmsEquipOutstorageDetails ref="detailsModal" @on-result-change="_page"></LmsEquipOutstorageDetails>
  </div>
</template>
<script>
import { equipDeliveryOrder } from '../../../api'
import LmsEquipOutOrder from './out-order-detail/LmsEquipOutOrder'
// import LmsEquipOutstorageDetails from './LmsEquipOutstorageDetails.vue'
export default {
  components: {
    LmsEquipOutOrder
  },
  data() {
    return {
      formObj: {
        num: '',
        status: '',
        beginDate: '',
        endDate: '',
        applyReason: ''
      },
      searchOpen: true,
      btn: [],
      pageColumns: [
        {
          title: '状态',
          key: 'status',
          width: 100,
          status: true
        },
        {
          title: '申请编号',
          key: 'num',
          width: 220
        },
        {
          title: '申请原因',
          key: 'reason',
          width: 320
        },
        {
          title: '申请时间',
          key: 'ctime',
          width: 210,
          date: true
        },
        { title: '申请人', key: 'applyer', width: 120 },
        { title: '审批意见', key: 'postil', width: 280 }
      ],
      getPage: {},
      iconMsg: [{ type: 'pt-a-view', id: '', name: '出库单详情' }],
      iconMsgCheck: [
        { type: 'pt-a-edit', id: '', name: '出库单详情' },
        { type: 'pt-a-edit', id: '', name: '申请原因' },
        { type: 'pt-a-edit', id: '', name: '提交审批' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ]
    }
  },
  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 equipDeliveryOrder.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._outOrderModal(data)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },

    _outOrderModal(data) {
      if (data.status !== 0 && data.status !== 5) {
        this._outOrderDetailCannotEdit(data)
      } else {
        this._outOrderDetailCanEdit(data)
      }
    },

    _outOrderDetailCanEdit(data) {
      this.$refs.outOrderDetailCanEditModal._open(data)
    },
    _outOrderDetailCannotEdit(data) {
      alert(234)
    },

    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await equipDeliveryOrder.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },

    _ctimeChange(data) {
      this.formObj.beginDate = data[0]
      this.formObj.endDate = data[1]
    },
    _editModal: async function(id) {
      const result = await equipDeliveryOrder.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _createApplyOrder() {
      this.$refs.editModal._open()
    },
    _deleteApplyOrder() {
      alert(3456)
    }
  }
}
</script>
