<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="true" width="1000px">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form
          id="edit-form-train-record-edit"
          ref="formObj"
          :model="formObj"
          :rules="ruleValidate"
          :label-width="80"
          inline
        >
          <Form-item label="审核目的" prop="checkPlanPurpose" class="width-48">
            <Tooltip content="只能输入中文且长度为0-100" placement="bottom-start">
              <Input
                v-model="formObj.checkPlanPurpose"
                name="checkPlanPurpose"
                placeholder="请输入审核依据"
                style="width: 375px"
                :maxlength="100"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="审核时间" prop="checkPlanTime" class="width-48">
            <Date-picker
              v-model="formObj.checkPlanTime"
              name="checkPlanTime"
              type="date"
              placeholder="请输入审核时间"
              format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </Form-item>
          <Form-item label="审核依据" prop="checkPlanBasis" class="width-48">
            <Tooltip content="只能输入中文且长度为0-100" placement="bottom-start">
              <Input
                v-model="formObj.checkPlanBasis"
                name="checkPlanBasis"
                placeholder="请输入审核依据"
                style="width: 375px"
                :maxlength="100"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="审核方法" prop="checkPlanMethod" class="width-48">
            <Tooltip content="只能输入中文且长度为0-100" placement="bottom-start">
              <Input
                v-model="formObj.checkPlanMethod"
                name="checkPlanMethod"
                placeholder="请输入审核方法"
                :maxlength="100"
                style="width: 375px"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="审核类型" prop="checkPlanType" class="width-48">
            <Select v-model="formObj.checkPlanType" clearable style="width:100%" name="checkPlanType">
              <Option v-for="item in innerAuditList" :key="item.name" :value="item.name">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="审核范围" prop="checkPlanRange" class="width-48">
            <Tooltip content="只能输入中文且长度为0-200" placement="bottom-start">
              <Input
                v-model="formObj.checkPlanRange"
                name="checkPlanRange"
                :maxlength="200"
                placeholder="请输入审核范围"
                style="width: 375px"
              ></Input>
            </Tooltip>
          </Form-item>
          <Form-item label="审核组长" prop="checkGroupLeaderId" class="width-48">
            <Select
              v-model="formObj.checkGroupLeaderId"
              filterable
              name="checkGroupLeaderName"
              :label-in-value="true"
              @on-change="_leaderChange"
            >
              <Option v-for="item in userList" :key="item.id" :value="item.id">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="审核组成员" prop="checkGroupMemberIds" class="width-48">
            <Select
              v-model="checkGroupMemberIds"
              filterable
              multiple
              name="checkGroupMemberIds"
              :label-in-value="true"
              @on-change="_memberChange"
            >
              <Option v-for="item in userList" :key="item.id" :value="item.id">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="编制人" prop="applyName" class="width-48">
            <Input
              v-model="formObj.applyName"
              name="applyName"
              placeholder="请选择编制人"
              icon="plus-circled"
              @on-click="_selectApply"
            ></Input>
            <input v-model="formObj.applyId" name="applyId" type="hidden">
          </Form-item>
          <Form-item label="编制时间" prop="applyTime" class="width-48">
            <Date-picker
              v-model="applyTime"
              name="applyTime"
              type="date"
              placeholder="请输入审核时间"
              format="yyyy-MM-dd"
              style="width: 100%;"
            />
          </Form-item>
          <Form-item label="审核内容" style="width: 97.6%;">
            <!--            <Editor ref="editorModal" v-model="formObj.checkPlanContent" @on-result-change="_editorData" />-->
            <Input
              v-model="formObj.checkPlanContent"
              name="checkPlanContent"
              :maxlength="200"
              placeholder="请输入审核内容"
              style="width: 375px"
            ></Input>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Tooltip content="长度为0-100" placement="bottom-start">
              <Input v-model="formObj.remark" name="remark" :maxlength="100" placeholder="请输入备注" style="width: 860px;" /></Input>
            </Tooltip>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>

    <!--选择人员-->
    <UserInfo ref="userModal" is-change @on-result-change="_userData" />
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { dict, staffInfo, internalAudit } from '../../../api'
import global from '../../../api/config'
import UserInfo from '../../../components/user-info-single/assignPerson'
export default {
  components: { UserInfo },
  data: function() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        checkPlanPurpose: undefined,
        checkPlanTime: undefined,
        checkPlanBasis: undefined,
        checkPlanMethod: undefined,
        checkPlanType: undefined,
        checkPlanRange: undefined,
        checkGroupLeaderId: undefined,
        checkGroupLeaderName: undefined,
        checkGroupMemberIds: undefined,
        checkGroupMemberNames: undefined,
        checkPlanContent: undefined,
        checkPlanContentText: undefined,
        applyId: undefined,
        applyName: undefined,
        applyTime: undefined,
        auditId: undefined,
        auditName: undefined,
        auditTime: undefined,
        remark: undefined,
        lmsFrameFileList: []
      },
      innerAuditList: [],
      userList: [],
      ruleValidate: {
        trainTopic: [
          { required: true, message: '培训主题不能为空', trigger: 'blur' }
        ],
        trainId: [
          { required: true, message: '所属培训计划不能为空', trigger: 'change' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _userData(data, msg) {
      // 选择人员返回数据
      if (msg === 'applyName') {
        // 选择取样人取样人
        this.formObj.applyName = data.realname
        this.formObj.applyId = data.userId
      }
    },
    _getInnerAuditList: async function() {
      const result = await dict.getItem('内审审核类型')
      this.innerAuditList = result
    },
    _selectPlan() {
      this.$refs.trainPlanModal._open()
    },

    _backTrainPlanData(data) {
      this.formObj.trainPlan = data.name
      this.formObj.trainId = data.id
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          this._save(this.formObj)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      const result = await internalAudit.add(data)
      this._resultChange(result, '添加成功')
    },
    _cancel() {
      this.showEditModal = false
    },
    _getUserList: async function() {
      const result = await staffInfo.list({ status: 1 })
      if (result) {
        this.userList = result
      }
    },
    _leaderChange(obj) {
      console.log(obj)
      if (obj) {
        this.formObj.checkGroupLeaderName = obj.label.trim()
      } else {
        this.formObj.checkGroupLeaderName = ''
      }
    },

    _selectApply() {
      this.$refs.userModal._open('applyName')
    },
    _memberChange(obj) {
      let names = ''
      let ids = ''
      obj.forEach(function(item) {
        names = names + item.label.trim() + ','
        ids = ids + item.value + ','
      })
      this.formObj.checkGroupMemberNames = names.substring(0, names.length - 1)
      this.formObj.checkGroupMemberIds = ids.substring(0, ids.length - 1)
    },
    _open(formObj) {
      const user = global.getUserInfo()
      console.log('----user-----', user)
      this.$refs.formObj.resetFields()
      this.formObj.applyId = user.id
      this.formObj.applyName = user.realname
      this.showEditModal = true
      this._hideLoading()
      this._getInnerAuditList()
      this._getUserList()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加内审计划'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑内审计划'
        if (undefined !== formObj.applyTime) {
          this.formObj.applyTime = this.$dateformat(
            new Date(formObj.applyTime),
            'yyyy-mm-dd'
          )
        } else {
          this.formObj.applyTime = ''
        }
        if (undefined !== formObj.checkPlanTime) {
          this.formObj.checkPlanTime = this.$dateformat(
            new Date(formObj.checkPlanTime),
            'yyyy-mm-dd'
          )
        } else {
          this.formObj.checkPlanTime = ''
        }
      }
    }
  }
}
</script>
