<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="_add">
        添加
      </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>
            <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" />

    <!--添加-->
    <RightEdit ref="addModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { internalAuditManage } from '../../../api'
import reasonEdit from '../../../components/base/Reason'
import assignPerson from '../../../components/user-info-single/assignPerson'
import RightEdit from './RightEdit'
export default {
  components: {
    assignPerson,
    RightEdit,
    reasonEdit
  },
  data() {
    return {
      formObj: {
        name: undefined,
        checkPlanId: ''
      },
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '按人分配' }],
      pageColumns: [
        { title: '内审员', key: 'checkMemberName', width: 120 },
        {
          title: '内审结束日期',
          key: 'checkManageTime',
          width: 120,
          date: true
        },
        {
          title: '质量管理体系',
          key: 'checkManageItem',
          width: 280,
          detail: true
        },
        { title: '检查内容', key: 'checkManageContent', width: 150 },
        { title: '检查结果', key: 'checkManageResult', width: 120 },
        { title: '审核类型', key: 'checkType', width: 120 },
        { title: '备注', key: 'remark', width: 200 },
        { title: '登记人', key: 'uname', width: 120 },
        { title: '登记日期', key: 'ctime', width: 120, date: true }
      ],
      getPage: {},
      checkPlanId: '',
      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 internalAuditManage.submitAudit(id)
      if (result) {
        this.$Modal.remove()
        this.$Message.success('提交成功!')
        this.$emit('on-result-change')
        this._page()
      }
    },

    _add() {
      if (this.formObj.checkPlanId === '' || this.checkPlanId === '') {
        this.$Message.warning('请选择左边一条数据！')
        return false
      }
      this.$refs.addModal._open(this.checkPlanId, '')
    },

    _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 internalAuditManage.allocateItem({
        ids: this.selectIds.join(','),
        obj: tempData
      })
      if (result) {
        this.$Message.success('分配成功！')
        this._page()
      }
    },
    _page: async function() {
      if (this.checkPlanId === '') {
        return false
      }
      this.getPage.records = []
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await internalAuditManage.page(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) {
        this.checkPlanId = data
        this.formObj.checkPlanId = data
        this._page()
      } else {
        this.checkPlanId = ''
        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 internalAuditManage.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await internalAuditManage.getById(id)
      if (result) {
        this.$refs.addModal._open(this.checkPlanId, result)
      }
    },
    _reasonResult: async function(data) {
      const result = await internalAuditManage.backAudit({
        remark: data,
        ids: this.selectIds
      })
      if (result) {
        this.$Message.success('退回成功')
        this.$emit('on-result-change')
        this._page()
      }
    }
  }
}
</script>
