<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-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="请输入申请原因" @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="创建时间:">
          <DatePicker
            ref="dateFormSearch"
            type="datetimerange"
            placement="bottom"
            placeholder="请选择创建时间"
            format="yyyy-MM-dd HH:mm"
            style="width: 100%"
            @on-change="_dateChange"
          />
        </Form-item>
        <input v-model="formObj.applyBeginTime" type="hidden" name="applyBeginTime">
        <input v-model="formObj.applyEndTime" type="hidden" name="applyEndTime">
        <Form-item class="search-item" label="创建人:">
          <Input v-model="formObj.registerName" 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>
            <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>
    <!--历史记录-->
    <EquipRepairDoRecord ref="recordModal" />

    <!-- 维修开始 -->
    <LmsEquipRepairManageStart ref="startRepairModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { equipScrap } from '../../../api'
import EquipRepairDoRecord from './EquipScrapDoRecord'
import LmsEquipRepairManageStart from './LmsEquipRepairManageStart.vue'
export default {
  components: { EquipRepairDoRecord, LmsEquipRepairManageStart },
  data() {
    return {
      formObj: {
        name: '',
        registerName: '',
        applyBeginTime: '',
        applyEndTime: '',
        applyReason: ''
      },
      searchOpen: true,
      btn: [],
      pageColumns: [
        { title: '报废申请单编号', key: 'num', width: 230 },
        { title: '报废申请原因', key: 'applyReason', width: 260 },
        { title: '涉及仪器', key: 'equipNames', width: 359 },
        { title: '创建时间', key: 'applyTime', width: 160, datetime: true },
        { title: '创建人', key: 'registerName', width: 160 },
        { title: '备注', key: 'remark', width: 160 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-submit', id: '', name: '实施' },
        { type: 'pt-a-data', 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 equipScrap.pageManage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _dateChange(e) {
      this.formObj.applyBeginTime = e[0]
      this.formObj.applyEndTime = e[1]
    },
    _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._doHis(data.id)
          break
      }
    },

    // 操作日志
    _doHis(id) {
      this.$refs.recordModal._open(id)
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await equipScrap.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal(id) {
      this.$refs.startRepairModal._open(id)
    }
  }
}
</script>
