<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-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" />-->
      <el-button type="success" @click="_add">
        添加
      </el-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>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>
            <div v-else-if="item.status">
              <div v-if="scope.row.status===0" style="color: #F8BB2C;">草稿</div>
              <div v-else-if="scope.row.status===1" style="color: #00a0e9;">待审批</div>
              <div v-else-if="scope.row.status===2" style="color: #6FBA2C;">审批通过</div>
              <div v-else-if="scope.row.status===3" style="color: red;">审批驳回</div>
              <div v-else-if="scope.row.status===5" style="color: red;">审批未通过</div>
            </div>
            <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? iconMsg:iconMsg1"
              :row-data="scope.row"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <LmsEquipMaintainEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { equipRepair } from '../../../api'
import LmsEquipMaintainEdit from './create/LmsEquipMaintainEdit'
export default {
  components: {
    LmsEquipMaintainEdit
  },
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      btn: [],
      pageColumns: [
        { title: '计划编号', key: 'num', width: 120, fixed: 'left' },
        {
          title: '计划名称',
          key: 'name',
          width: 120,
          fixed: 'left',
          detail: true
        },
        {
          title: '状态',
          key: 'status',
          width: 120,
          fixed: 'left',
          status: true
        },
        {
          title: '涉及仪器',
          key: 'involveNames',
          width: 160
        },
        { title: '创建人', key: 'registerName', width: 90 },
        {
          title: '创建时间',
          key: 'registerTime',
          width: 140,
          date: true
        },
        { title: '最后修改人', key: 'applyName', width: 150 },
        {
          title: '最后修改时间',
          key: 'applyTime',
          width: 140,
          date: true
        },
        {
          title: '备注',
          key: 'remark',
          width: 120
        },
        { title: '审批意见', key: 'firstStepAdvice', width: 280 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' },
        { type: 'pt-a-edit', id: '', name: '复制' }
      ],
      iconMsg1: [{ type: 'pt-a-edit', 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 equipRepair.pageRepair(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams() {
      return this.$refs.pageTable._searchParams()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open('', 1)
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },

    _add() {
      this.$refs.editModal._open('', 1)
    },
    _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._deleteById(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await equipRepair.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await equipRepair.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
