<template>
  <div>
    <Modal
      v-model="showModal"
      v-drag
      :mask-closable="false"
      title="计划详情"
      :width="1200"
      footer-hide
    >
      <Row>
        <Col span="24" />
        <Form v-model="formObj" inline onsubmit="return false" :label-width="90">
          <Form-item class="search-item" label="名称:" style="margin-left: -25px">
            <Input v-model="formObj.name" placeholder="请输入名称" clearable @on-enter="_formSearch" />
            <input v-model="formObj.mainPlanId" type="hidden">
          </Form-item>
          <Form-item class="search-btn">
            <Button type="primary" @click="_formSearch">
              搜索
            </Button>
          </Form-item>
        </Form>
        </Col>
        <!--正常界面-->
        <Col span="24" />
        <Button type="success" style="margin-bottom: 5px" @click="_add">
          添加
        </Button>
        <Button type="success" style="margin-bottom: 5px" @click="_delete">
          删除
        </Button>
        </Col>
        <!-- 表格 -->
        <Col span="24" />
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          select-data
          :table-height="350"
          :get-page="getPage"
          :icon-msg="iconMsg"
          :opt-col-width="80"
          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"
          >
            <template slot-scope="scope">
              <span v-if="item.key==='judged'">
                {{ scope.row[item.key]===1?'是':'否' }}
                {{ scope.row[item.key]===1?'是':'否' }}
              </span>
              <div v-else-if="item.date">
                {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
              </div>
              <span
                v-else-if="item.key==='orderBy'"
                @click.stop="_handleRow(scope)"
              >
                <el-input-number
                  v-model="scope.row.orderBy"
                  size="small"
                  placeholder="排序"
                  @change="_handleOrderValueEdit(scope.row.id,scope.row.orderBy)"
                />
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>
            </template>
          </el-table-column>
        </element-table>
        </Col>
      </Row>
    </Modal>
    <PlanEdit ref="planEditModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { qcSubPlan } from '../../../api'
import PlanEdit from './PlanEdit'
export default {
  components: {
    PlanEdit
  },
  data() {
    return {
      formObj: {
        mainPlanId: '',
        name: '',
        code: '',
        standardName: ''
      },
      showModal: false,
      searchOpen: false,
      btn: [],
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      pageColumns: [
        { title: '监控方法', key: 'method', width: 130 },
        { title: '监控项目名称及参数', key: 'name', detail: true, width: 200 },
        { title: '频次', key: 'frequency', width: 130 },
        { title: '时间', key: 'time', width: 130, date: true },
        { title: '监控单位 ', key: 'monitorUnit', width: 130 },
        { title: '责任人 ', key: 'monitorMan', width: 130 },
        { title: '参加人员/部门 ', key: 'department', width: 130 },
        { title: '备注', key: 'remark', width: 130 },
        { title: '监控结果 ', key: 'monitorResult', width: 130 },
        { title: '实施人员 ', key: 'implementer', width: 130 },
        { title: '完成日期 ', key: 'completion', width: 130, date: true }
      ],
      mainPlanId: '',
      getPage: {},
      selectData: []
    }
  },
  methods: {
    _open(mainPlanId, label) {
      this.formObj = this.$resetFields(this.formObj)
      this.showModal = true
      this.mainPlanId = mainPlanId
      this.formObj.mainPlanId = mainPlanId
      this.getPage.records = []
      this.$refs.pageTable._hideLoading()
      if (mainPlanId) {
        this._page()
      }
    },

    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },

    _add() {
      this.$refs.planEditModal._open(this.mainPlanId, '')
    },
    _delete() {
      if (this.selectData.length === 0) {
        this.$Message.warning('请选择要删除的数据')
        return false
      }
      const selectIds = []
      for (let i = 0; i < this.selectData.length; i++) {
        selectIds.push(this.selectData[i].id)
      }
      this._deleteById(selectIds)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await qcSubPlan.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导入检测项目':
          this.$refs.relItemModal._open(this.mainPlanId)
          break
      }
    },
    _importItem() {
      this.$refs.relItemModal._open(this.mainPlanId)
    },
    // 操作列操作
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
        case '编辑':
          this._editById(data.id)
          break
      }
    },

    _editById: async function(id) {
      const result = await qcSubPlan.getById(id)
      if (result) {
        this.$refs.planEditModal._open(this.mainPlanId, result)
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },

    _handleOrderValueEdit(id, value) {
      this._edit({ id: id, obj: { orderBy: value } })
    },

    _edit: async function(data) {
      const result = await qcSubPlan.edit(data)
      if (result) {
        this._page()
      }
    },

    _deleteOk: async function(id) {
      const result = await qcSubPlan.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    // table结果  返回整行
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectData = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
