<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" />
      <Button  type="primary" style="margin-bottom: 12px"  @click="_allocateByUser">调整检测人</Button>
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        :icon-msg="iconMsg"
        show-check-box
        @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>
      </element-table>
      </Col>
    </Row>
    <assign-person ref="assignPersonModal" is-change @on-result-change="_userResult" />
  </div>
</template>
<script>
import { drugItem } from '../../../api'
import assignPerson from '../../../components/user-info-single/assignPerson'
export default {
  components: {
    assignPerson
  },
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '按人分配' }],
      pageColumns: [
        { title: '检测项目', key: 'name', width: 150 },
        { title: '样本编号', key: 'sampleCode', width: 150 },
        { title: '检测人', key: 'testers', width: 150 },
        { title: '业务类型', key: 'category', width: 150 },
        { title: '检测依据', key: 'code', width: 150 },
        { title: '检测依据名称', key: 'standardName' },
        { title: '检测科室', key: 'groupName' },
        { title: '比较符', key: 'compareSymbol' },
        { title: '限值', key: 'limitValue' },
        { title: '单位', key: 'unit' },
        { title: '是否系统判定', key: 'judged' },
        { title: '备注', key: 'remark' }
      ],
      getPage: {},
      sampleIds: [],
      selectIds: [], // 检测项目id
      iconMsg: [
        { type: 'pt-a-edit', 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') - 60
      } else {
        return this.$tableHeight('noSearch') - 60
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      if (this.sampleIds.length !== 0 || this.getPage.records === []) {
        this.$refs.pageTable._pageChange(1)
      }
    },
    _init() {
      this.$refs.pageTable._initTable()
    },
    _allocateByUser() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        // const user = Global.getUserInfo('userInfo')
        this.$nextTick(function() {
          this.$refs.assignPersonModal._open('itemTree', '组织列表')
        })
      }
    },
    _userResult: async function(data) {
      const tempData = {}
      tempData.userId = data.userId
      tempData.userName = data.realname
      const result = await drugItem.adjustTester({
        ids: this.selectIds.join(','),
        obj: tempData
      })
      if (result) {
        this.$Message.success('调整成功！')
        this._page()
        this.$emit('on-result-change')
      }
    },
    _page: async function() {
      this.getPage.records = []
      if (this.sampleIds.length === 0) {
        return false
      }
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugItem.pageDispatch({
        sampleIds: this.sampleIds,
        obj: 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 '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
        case 'selectIds':
          this.selectIds = data
          break
      }
    },

    _open(data) {
      if (undefined !== data && data.length > 0) {
        this.sampleIds = data
        this._page()
      } else {
        this.sampleIds = []
        this.getPage.records = []
      }
    },

    _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 drugItem.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await drugItem.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
