<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="_submitCheck">
        提交
      </Button>
      <Button type="primary" style="margin-bottom: 12px" @click="_backCheck">
        退回
      </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>
            <span v-else-if="item.isNot" class="blue-color">
              {{ scope.row[item.key] === 0 ? '否': '是' }}
            </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" />

    <!--退回原因-->
    <reasonEdit ref="reasonModal" @on-result-change="_reasonResult" />
  </div>
</template>
<script>
import { drugItem } from '../../../api'
import assignPerson from '../../../components/user-info-single/assignPerson'
import reasonEdit from '../../../components/base/Reason'
export default {
  components: {
    assignPerson,
    reasonEdit
  },
  data() {
    return {
      formObj: {
        name: ''
      },
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '按人分配' }],
      pageColumns: [
        { title: '检测项目', key: 'name', width: 120 },
        { title: '样本编号', key: 'sampleCode', width: 120 },
        { title: '检测值', key: 'testValue', width: 120 },
        { title: '缺陷', key: 'defects', width: 120 },
        { title: '业务类型', key: 'category', width: 120 },
        { title: '检测依据', key: 'code', width: 120 },
        { title: '检测依据名称', key: 'standardName' },
        { title: '检测科室', key: 'groupName', width: 120 },
        { title: '比较符', key: 'compareSymbol', width: 120 },
        { title: '限值', key: 'limitValue', width: 120 },
        { title: '单位', key: 'unit', width: 120 },
        { title: '是否系统判定', key: 'judged', isNot: true },
        { 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()
    },
    _submitCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定提交该数据？',
          loading: true,
          onOk: () => {
            this._submitOk(this.selectIds)
          }
        })
      }
    },
    _submitOk: async function(id) {
      if (!id.length) return false
      const result = await drugItem.submitCheck(id)
      if (result) {
        this.$Modal.remove()
        this.$Message.success('提交成功!')
        this.$emit('on-result-change')
        this._page()
      }
    },
    _backCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定退回 ' + this.selectIds.length + ' 条记录？',
          onOk: () => {
            this.$refs.reasonModal._open('退回原因')
          }
        })
      }
    },
    _userResult: async function(data) {
      const tempData = {}
      tempData.userId = data.userId
      tempData.userName = data.realname
      const result = await drugItem.allocateItem({
        ids: this.selectIds.join(','),
        obj: tempData
      })
      if (result) {
        this.$Message.success('分配成功！')
        this._page()
      }
    },
    _page: async function() {
      if (this.sampleIds.length === 0) {
        return false
      }
      this.getPage.records = []
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await drugItem.pageItemCheck({
        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)
      }
    },
    _reasonResult: async function(data) {
      const result = await drugItem.backCheck({
        remark: data,
        ids: this.selectIds
      })
      if (result) {
        this.$Message.success('退回成功')
        this.$emit('on-result-change')
        this._page()
      }
    }
  }
}
</script>
