<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>
            <div v-else-if="item.date">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
            </div>
            <sapn v-else-if="item.sex">{{scope.row.sex == 2 ? '女' :scope.row.sex == 1? '男': ''}}</sapn>
            <sapn v-else-if="item.probatioStartDate">
              {{scope.row.probatioStartDate?$dateformat(scope.row.probatioStartDate,'yyyy-mm-dd~')+$dateformat(scope.row.probatioEndDate,'yyyy-mm-dd'):''}}
            </sapn>
            <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="170"
          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>
    <!--添加编辑-->
    <typeEdit ref="editModal" @on-result-change="_page" />

    <CertificateList ref="certificateModal" @on-result-change="_page" />

    <WorkExperienceList ref="workExperienceModal" @on-result-change="_page" />

    <CanUseEquipList ref="canUseEquipModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { staffInfo } from '../../../api'
import typeEdit from './StaffInfoEdit'
import CertificateList from './certificate/CertificateList'
import WorkExperienceList from './work-experience/WorkExperienceList'
import CanUseEquipList from './can-use-equip/CanUseEquipList'
export default {
  components: {
    typeEdit,
    CertificateList,
    WorkExperienceList,
    CanUseEquipList
  },
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      // btn: [{ type: 'success', id: 'staff-info-add', name: '添加' }],
      pageColumns: [
        { title: '姓名', key: 'name', width: 100, detail: true },
        { title: '工号', key: 'number', width: 100 },
        { title: '性别', key: 'sex', width: 100, sex: true },
        { title: '所属部门', key: 'orgNames', width: 100 },
        { title: '员工类型', key: 'levelName', width: 100 },
        { title: '专业', key: 'profession', width: 100 },
        { title: '最新职称', key: 'profess', width: 100 },
        { title: '年龄', key: 'age', width: 100 },
        { title: '入职时间', key: 'entryTime', width: 140, date: true },
        { title: '参加工作时间', key: 'workTime', width: 140, date: true },
        {
          title: '试用期时间',
          key: 'probatioStartDate',
          width: 200,
          probatioStartDate: true
        },
        { title: '入职转正时间', key: 'becomeDate', width: 140, date: true },
        { title: '合同到期时间', key: 'expireDate', width: 140, date: true },
        { title: '学历', key: 'education', width: 100 },
        { title: '毕业时间', key: 'graduationTime', width: 140, date: true },
        { title: '毕业院校', key: 'graduatedSchool', width: 100 },
        { title: '岗位', key: 'post', width: 100 },
        { title: '状态', key: 'status', width: 100, status: true },
        { title: '是否内审员', key: 'ifAuditor', width: 120, ifAuditor: true },
        { title: '备注', key: 'remark', width: 100 }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-chemistry', id: '', name: '可用仪器' },
        { type: 'pt-a-man', id: '', name: '职称与证书' },
        { type: 'pt-a-data', id: '', name: '工作经历' },
        { type: 'pt-a-trash', 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 staffInfo.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },

    _add() {
      this.$refs.editModal._open()
    },

    _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._certificateModal(data.id)
          break
        case '可用仪器':
          this._canUseEquipModal(data.id)
          break
        case '工作经历':
          this._workModal(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 staffInfo.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await staffInfo.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _certificateModal(id) {
      this.$refs.certificateModal._open(id)
    },
    _canUseEquipModal(id) {
      this.$refs.canUseEquipModal._open(id)
    },
    _workModal(id) {
      this.$refs.workExperienceModal._open(id)
    }
  }
}
</script>
