<template>
  <div>
    <Modal v-model="showDefectModal" v-drag width="800" class="modal-footer-none zIndex-1100">
      <p slot="header">
        添加缺陷
      </p>
      <div>
        <Row>
          <!--查询-->
          <Form id="test-value-extend-form" inline onsubmit="return false" :label-width="70">
            <label class="label-sign" />
            <Form-item label="缺陷:">
              <Input
                name="name"
                placeholder="请输入缺陷"
                style="width: 200px"
                clearable
                @on-enter="_search"
              />
            </Form-item>
            <Form-item class="search-btn">
              <Button type="primary" @click="_search">
                搜索
              </Button>
            </Form-item>
          </Form>
          <Col span="24" />
          <Button style="margin-bottom: 5px" type="primary" @click="_add">
            添加
          </Button>
          </Col>
          <!-- 表格 -->
          <Col span="24" />
          <element-table
            ref="pageTable"
            :table-height="tableHeight"
            :get-page="getPage"
            show-check-box
            select-data
            @on-result-change="_tableResultChange"
          >
            <el-table-column
              v-for="item in pageColumns"
              :key="item.key"
              show-overflow-tooltip
              :prop="item.key"
              :align="item.align"
              :label="item.title"
              :min-width="item.width"
              :fixed="item.fixed?item.fixed:undefined"
            >
              <template slot-scope="scope">
                <span
                  v-if="item.key==='defect'"
                  @click.stop="_handleRow(scope)"
                >
                  <el-input
                    v-model="scope.row.defect"
                    size="medium"
                    placeholder="请输入缺陷"
                    @blur="_handleEdit(scope.row.id,scope.row.defect,'defect')"
                  />
                </span>

                <span
                  v-else-if="item.key==='type'"
                  @click.stop="_handleRow(scope)"
                >
                  <el-select v-model="scope.row.type" filterable placeholder="请选择类别" @change="_handleEdit(scope.row.id,scope.row.type,'type')">
                    <el-option
                      v-for="item in typeList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </span>


                <span
                  v-else-if="item.key==='code'"
                  @click.stop="_handleRow(scope)"
                >
                  <el-select v-model="scope.row.code" filterable placeholder="请选择代码" @change="_handleEdit(scope.row.id,scope.row.code,'code')">
                    <el-option
                      v-for="item in codeList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </span>



                <span
                  v-else-if="item.key==='quantity'"
                  @click.stop="_handleRow(scope)"
                >
                  <el-input
                    v-model="scope.row.quantity"
                    type="number"
                    size="medium"
                    placeholder="请输入数量"
                    @blur="_handleEdit(scope.row.id,scope.row.quantity,'quantity')"
                  />
                </span>


                <span v-else>{{ scope.row[item.key] }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              :width="100"
              fixed="right"
            >
              <template slot-scope="scope">
                <i @click="_delete(scope.row.id)">
                  删除
                </i>
              </template>
            </el-table-column>
          </element-table>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import { testValueExtend } from '../../../../api'

export default {
  components: {},
  data() {
    return {
      showDefectModal: false,
      getPage: {},
      currentRow: {},
      typeList: [],
      codeList: [],
      pageColumns: [
        { title: '缺陷', key: 'defect', align: 'center', width: 120 },
        { title: '类别', key: 'type', align: 'center', width: 100 },
        { title: '代码', key: 'code', align: 'center', width: 100 },
        { title: '数量', key: 'quantity', align: 'center' }
      ],
      iconMsg: [{ type: 'pt-a-trash', id: '', name: '删除' }],
      footerList: [
        { id: '', type: '', name: '关闭' },
        { id: '', type: 'primary', name: '确认' }
      ],
      itemIds: [],
      itemId: '',
      selectData: []
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal') - 200
    }
  },
  methods: {
    _open(itemId) {
      this.showDefectModal = true
      this.itemId = itemId
      this._hideLoading()
      this.$refs.pageTable.templateRadio = ''
      this._clearPage()
      this._page()
      this._getTypeList()
    },

    _getTypeList() {
      this.typeList.push({
        value: '烟支',
        label: '烟支'
      })
      this.typeList.push({
        value: '卷烟纸',
        label: '卷烟纸'
      })
      this.typeList.push({
        value: '接装纸',
        label: '接装纸'
      })
      this.typeList.push({
        value: '滤嘴',
        label: '滤嘴'
      })
      this.typeList.push({
        value: '烟支标识',
        label: '烟支标识'
      })
      this.typeList.push({
        value: '小盒',
        label: '小盒'
      })

      this.codeList.push({
        value: 'Y01.A.1',
        label: 'Y01.A.1'
      })
      this.codeList.push({
        value: 'Y01.A.2',
        label: 'Y01.A.2'
      })
      this.codeList.push({
        value: 'Y02.A.1',
        label: 'Y02.A.1'
      })
      this.codeList.push({
        value: 'Y02.A.2',
        label: 'Y02.A.2'
      })
    },
    _clearPage() {
      $('#test-value-extend-form')
        .find('input')
        .each(function() {
          $(this).val('')
        })
    },
    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    _handleEdit(id, value, label) {
      const data = {}
      data[label] = value
      data.id = id
      console.log('data----1--', data)
      this._edit(data)
    },
    _edit: async function(data) {
      const result = await testValueExtend.edit(data)
      if (result) {
        await this._page()
      }
    },
    _add() {
      const data = { itemId: this.itemId }
      this._save(data)
    },

    _delete(id) {
      this._deleteByIds(id)
    },

    _deleteByIds: async function(id) {
      const result = await testValueExtend.deleteById(id)
      if (result) {
        await this._page()
      }
    },
    _page: async function() {
      const data = this.$serialize('test-value-extend-form')
      data.itemId = this.itemId
      Object.assign(data, this.formObj)
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await testValueExtend.page(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectData = data
          break
        default:
          this._page()
      }
    },
    _footerResult(msg) {
      switch (msg) {
        case '关闭':
          this.showDefectModal = false
          break
        case '确认':
          this._ok()
          break
      }
    },
    _ok() {
      const data = this.selectData
      if (data.length === 0) {
        this.$Message.warning('请先选择一条数据!')
        this.$refs.footerModal._hideLoading()
      } else {
        const equipData = []
        for (let i = 0; i < data.length; i++) {
          equipData.push({
            brand: data[i].brand,
            equipId: data[i].id,
            equipName: data[i].name,
            labNum: data[i].name,
            equipNum: data[i].labNum,
            acquisitionCommand: data[i].acquisitionCommand,
            spec: data[i].spec,
            collectionType: data[i].acquisitionType,
            ipAddress: data[i].ipAddress,
            collectionAddress: data[i].acquisitionAddress
          })
        }
        const equipDTO = {
          equipList: equipData,
          itemIds: this.itemIds
        }
        this._save(equipDTO)
      }
    },
    async _save(data) {
      const result = await testValueExtend.add(data)
      if (result) {
        await this._page()
      } else {
        this._hideLoading()
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _search() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>
