<template>
  <div>
    <Row>
      <Form
        v-show="searchOpen"
        id="sample-manage-form"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="样本编号:">
          <Input name="sampleNum" placeholder="请输入样本编号" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="物料名称:">
          <Input name="name" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item label="检测类型:" class="search-item" prop="type">
          <Input name="category" placeholder="请输入物料名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="供应商:">
          <Input name="supplier" placeholder="请输入供应商" clearable @on-enter="_formSearch" />
        </Form-item>
        <!--<Form-item label="存储期限:" class="search-item">-->
        <!--<Date-picker-->
        <!--ref="saveDate"-->
        <!--type="daterange"-->
        <!--placeholder="存储期限"-->
        <!--style="width: 100%;"-->
        <!--:editable="false"-->
        <!--@on-change="_saveTimeChange"-->
        <!--/>-->
        <!--<input type="hidden" name="preparationTimeBegin">-->
        <!--<input type="hidden" name="preparationTimeEnd">-->
        <!--</Form-item>-->
        <Form-item class="search-item" label="存储位置:">
          <Input name="storageLocation" placeholder="请输入存储位置" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      <Col span="24" />
<!--        <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
          <el-button style="margin: 2px" type="primary" @click="_saveEdit">填写留样信息</el-button>
          <el-button style="margin: 2px" type="primary" @click="_preDeal">处理</el-button>
      </Col>
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        show-check-box
        warn-key
        @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"
          :align="item.align"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope" @click.stop="_handleIndex(scope)">
            <span v-if="item.key==='storageLocation'" @click.stop="_handleIndex(scope)">
              <span v-if="editIndex!==scope.$index">
                {{ scope.row.storageLocation }}
              </span>
              <span v-else>
                <el-input v-model="scope.row.storageLocation" placeholder="位置" size="small" />
              </span>
            </span>
            <span v-else-if="item.key==='storagePeriod'">
              <span v-if="editIndex!==scope.$index">
                {{ scope.row.storagePeriod }}
              </span>
              <span v-else>
                <el-input v-model="scope.row.storagePeriod" placeholder="期限" size="small" />
              </span>
            </span>
            <span v-else-if="item.key === 'status'">
              {{ scope.row[item.key]?scope.row[item.key].display:'' }}
            </span>
            <span v-else-if="item.datetime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </span>
            <span v-else-if="item.key === 'ctime'">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
            </span>
            <span v-else-if="item.key === 'sampleNum'" :class="scope.row.warning !== undefined?'font-jude':''">
              {{ scope.row[item.key]? scope.row[item.key]:'' }}
            </span>
            <span v-else>
              {{ scope.row[item.key] }}
            </span>
          </template>
        </el-table-column>
        <!--        <el-table-column-->
        <!--          slot="col"-->
        <!--          label="操作"-->
        <!--          align="center"-->
        <!--          :width="90"-->
        <!--          fixed="right"-->
        <!--        >-->
        <!--          <template slot-scope="scope">-->
        <!--            <icon-list-->
        <!--              :msg="editIndex !== scope.$index?iconMsg:iconMsgSave"-->
        <!--              :row-data="scope.row"-->
        <!--              :row-index="scope.$index"-->
        <!--              @on-result-change="_iconClick"-->
        <!--            />-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </element-table>
      </Col>
    </Row>
    <!--填写存放信息-->
    <preparationManageEdit ref="preModal" @on-result-change="_formSearch" />
    <!--处理样品-->
    <preparationManageDeal ref="dealModal" @on-result-change="_formSearch" />
  </div>
</template>

<script>
import preparationManageEdit from '../preparationManageEdit'
import preparationManageDeal from '../preparationManageDeal'
import { drugSampleKeep } from '../../../api'
export default {
  name: 'PreparationManage',
  components: {
    preparationManageEdit,
    preparationManageDeal
  },
  data() {
    return {
      pageColumns: [
        {
          title: '样本编号',
          key: 'sampleCode',
          width: 120,
          fixed: 'left',
          align: 'center'
        },
        { title: '牌号', key: 'name', width: 120, align: 'center' },
        { title: '价类', key: 'category', width: 120, align: 'center' },
        {
          title: '卷制标准版本号',
          key: 'standard',
          width: 160,
          align: 'center'
        },
        { title: '机台', key: 'samplePoint', width: 110, align: 'center' },
        {
          title: '留样数量',
          key: 'quantity',
          width: 120,
          align: 'center'
        },
        {
          title: '单位',
          key: 'unit',
          width: 160,
          align: 'center'
        },
        {
          title: '开始留样时间',
          key: 'ctime',
          width: 120,
          data: true,
          align: 'center'
        },
        {
          title: '存储期限(天)',
          key: 'timeLimit',
          width: 160,
          align: 'center'
        },
        {
          title: '存储位置',
          key: 'position',
          align: 'center'
        }
      ],
      getPage: {
        records: []
      },
      btn: [
        { id: '', type: 'primary', name: '填写存放信息' },
        { id: '', type: 'primary', name: '处理' }
      ],
      iconMsg: [{ id: '', type: 'pt-a-edit', name: '编辑' }],
      iconMsgSave: [{ id: '', type: 'pt-a-submit', name: '确认' }],
      searchOpen: false,
      currentIndex: '',
      selectIds: [],
      editIndex: -1
    }
  },
  computed: {
    tableHeight() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearchTwo')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    // 接口要异步
    async _page() {
      const data = this.$serialize('staff-select-form')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await drugSampleKeep.pageSampleKeep(data)
      if (result) {
        this.getPage = result
      }
      if (process.browser) {
        this.$refs.pageTable._hideLoading()
      }
    },
    _clearPage() {
      $('#staff-select-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
      // this.$refs.saveDate.handleClear()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '填写存放信息':
          this._saveEdit()
          break
        case '处理':
          this._preDeal()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },

    _saveEdit() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this.$refs.preModal._open(this.selectIds)
      }
    },
    _preDeal() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this.$refs.dealModal._open(this.selectIds)
      }
    },
    dealByIds(ids) {
      // const result = await sampleLibrary.deleteById(id)
      // if (result) {
      //   this.$Message.success('删除成功')
      //   this.selectIds = []
      //   this._formSearch()
      // }
    },
    _handleIndex(data) {
      this.currentIndex = data.$index
    },
    // 选择时间
    _endDateChange(data) {
      this.getPage.records[this.currentIndex].saveDate = data
    },
    _iconClick(res, data, index) {
      this.currentIndex = index
      switch (res) {
        case '编辑':
          this._edit()
          break
        case '确认':
          this._ok(data)
          break
      }
    },
    _edit() {
      this.editIndex = this.currentIndex
    },
    _ok(data) {
      this._endDateChange(this.$dateformat(data.saveDate, 'yyyy-mm-dd'))
      this.$nextTick(function() {
        const data = {
          storageLocation: this.getPage.records[this.currentIndex]
            .storageLocation,
          storagePeriod: this.getPage.records[this.currentIndex].storagePeriod
        }
        this._save(this.getPage.records[this.currentIndex].id, data)
      })
      this.editIndex = -1
    },
    async _save(id, data) {
      const result = await drugSampleKeep.preEdit({ id: id, obj: data })
      if (result) {
        this._resultChange('保存成功!')
      }
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._formSearch()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        default:
          this._page()
      }
    },
    // 日期和时间
    _saveTimeChange(data) {
      $('input[name="preparationTimeBegin"]').val(data[0])
      $('input[name="preparationTimeEnd"]').val(data[1])
      console.log(data)
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>

<style scoped>
.font-jude {
  color: red;
}
</style>
