<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-make-sample" :label-width="90" v-show="searchOpen" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="试样编号:">
                <Input @on-enter="_formSearch" v-model="formObj.sampleCode" placeholder="请输入试样编号" clearable/>
              </Form-item>
              <Form-item class="search-btn">
                <Button @click="_formSearch" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" :showSearchBtn="true" @on-result-change="_btnClick"
                     class="contHide">
              <!--切换待认领任务-->
            </btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :tableHeight="tableHeight" @on-result-change="_tableResultChange"
                            :getPage="getPage"  select-data>
              <vxe-table-column
                :field="item.key"
                :title="item.title"
                :min-width="item.width?item.width:200"
                :fixed="item.fixed?item.fixed:undefined"
                v-for="item in pageColumns"
                :key="item.key" sortable>
                <template slot-scope="scope">
                  <div v-if="item.display">{{scope.row[item.key]? scope.row[item.key].display: ''}}</div>
                  <div v-else-if="item.date">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'): ''}}
                  </div>
                  <div v-else-if="item.judge">{{Number(scope.row[item.key])===1?'是':'否'}}
                  </div>
                  <div v-else-if="item.reported">
                    <div v-if="scope.row[item.key]>0" class="green-color">是</div>
                    <div v-else>否</div>
                  </div>
                  <div v-else-if="item.key === 'infoMaintain'">
                    <label v-if="scope.row.infoMaintain === 'no'" class="green-color">信息已维护</label>
                    <label v-else-if="scope.row.infoMaintain === 'yes'" class="red-color">信息待维护</label>
                    <label v-else-if="scope.row.infoMaintain === 'none'">无</label>
                  </div>
                  <div v-else>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
              <vxe-table-column
                :width="140"
                title="操作"
                align="center"
                fixed="right">
                <template slot-scope="scope">
                  <VXEIconList :msg="type === 0?iconMsg1:iconMsg" @on-result-change="_iconClick" :rowData="scope.row"
                               :rowIndex="scope.rowIndex"></VXEIconList>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <SoilSampleItemManage ref="sampleItemManage" @on-result-change="_page"></SoilSampleItemManage>
  </div>
</template>
<script>
import { soilEntrust } from '../../../../api'
import SoilSampleItemManage from '../../../meter-entrust/SoilSampleItemManage'
/**
 * 按委托编制----管理样品
 */
export default {
  components: { SoilSampleItemManage },
  data() {
    return {
      currentComponent: '',
      searchOpen: false,
      btn: [{ type: 'success', id: 'ZBC', name: '生成' }],
      getPage: {},
      detectionDate: '',
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 160, fixed: 'left' },
        { title: '试样深度', key: 'sampleDepth', width: 160 },
        { title: '土质描述', key: 'sampleDescribe', width: 160 },
        { title: '样品包装类型', key: 'samplePack', width: 160 },
        { title: '现场编号', key: 'siteNo', width: 130 },
        { title: '试验项目', key: 'experimentNames', width: 330 }
      ],
      selectIds: [],
      selectData: [],
      formObj: {
        entrustId: undefined,
        contractId: undefined,
        sampleCode: undefined
      },
      dateList: [],
      type: '',
      iconMsg: [
        {
          type: 'ios-beaker',
          id: '',
          name: '管理检测项目'
        }
      ],
      iconMsg1: [
        {
          type: 'cloud',
          id: '',
          name: '样品附件',
          componentName: 'FileManage'
        },
        { type: 'compose', id: '', name: '编辑' }, // food-report-make-sample-edit
        {
          type: 'ios-upload',
          id: '',
          name: '上传',
          componentName: 'UploadReport'
        },
        {
          type: 'document-text',
          id: '',
          name: '项目台账',
          componentName: 'FoodItemEditModal'
        },
        {
          type: 'ios-clock',
          id: '',
          name: '操作记录',
          componentName: 'FoodSampleRecord'
        }
      ],
      dataPushedList: [{ value: 0, name: '否' }, { value: 1, name: '是' }],
      postUrl: 'FoodSample/pageSampleReportMake',
      currentId: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearch')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  mounted() {},
  methods: {
    // 初始化值
    // 当前列表有值，则不会重新调page接口
    _reportDateChange(data) {
      this.formObj.reportDateBegin = data[0]
      this.formObj.reportDateEnd = data[1]
    },
    _modalResult(data, msg) {
      switch (this.currentComponent) {
        case 'CreateReport':
          this.$emit('on-result-change', 'changeTab')
          break
        case 'ProgressByReport':
          // 进度条
          this.$emit('on-result-change', 'changeTab')
          break
        case 'UploadReport':
          // 单个编辑
          this._updateRows()
          break
        default:
          this._page()
      }
    },
    // 更新ids数据
    _updateRows() {
      const obj = {
        url: this.postUrl,
        params: this.$serializeFormSearch(this.formObj)
      }
      obj.params.ids = this.currentId
      this.$refs.pageTable._updateRows(obj)
    },
    _iconClick(res, data, componentName) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '管理检测项目':
            this._itemManage(data.id)
            break
          case '上传':
            this.currentId = data.id
            this.$refs.refModal._open(data.id)
            break
          case '项目台账':
            this.$refs.refModal._open(data)
            break
          case '操作记录':
            this._record(data.id)
            break
          case '编辑':
            this.currentId = data.id
            this._edit(data)
            break
          case '样品附件':
            this._upload(data.id)
            break
        }
      })
    },
    _itemManage(data) {
      // 管理检测项目
      this.$refs.sampleItemManage._open(data)
    },
    _upload(id) {
      // 上传文件
      this.$refs.refModal._open(id, 'sampleId')
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          for (let i = 0; i < data.length; i++) {
            this.selectIds.push(data[i].id)
          }
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _open(entrustId) {
      // 初始化数据
      // this._resetData(contractId, type, personal)
      this.formObj.entrustId = entrustId
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilEntrust.samplePage(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _record(id) {
      // 操作记录
      this.$refs.refModal._open(id)
    },
    _btnClick(msg, componentName) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (msg) {
          case '生成':
            this._createReport()
            break
          case '选择模板生成':
            this._judgeReport('create')
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._page()
      this.selectIds = []
    },
    // 生成报告
    _judgeReport(name) {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        switch (name) {
          case 'create':
            this.$refs.refModal._open(this.selectData)
            break
          case 'create-samples':
            this.$refs.refModal._open(this.selectIds)
            break
        }
      }
    }
  }
}
</script>
