<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="1000px"
      @close="close"
      @keyup.enter.native="doSubmit"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="100px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="21">
            <el-form-item label="职级名称" prop="rankName"
                          :rules="[{required: true, message: '职级名称不能为空', trigger: 'blur'}
                 ]">
              <el-input disabled v-model="inputForm.rankName" placeholder="请填写职级名称"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="状态" prop="useFlag"
                          :rules="[{required: true, message: '状态不能为空', trigger: 'blur'}
                 ]">
              <el-radio v-model="inputForm.useFlag" label="1" size="mini" style="margin-right: 0px" border>启用</el-radio>
              <el-radio v-model="inputForm.useFlag" label="2" size="mini" border>禁用</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="序列" prop="sequenceName"
                          :rules="[{required: true, message: '序列不能为空', trigger: 'blur'}
                 ]">
              <el-input disabled v-model="inputForm.sequenceName" placeholder="请填写序列名称"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21" v-if="isShowC === true">
            <el-form-item label="子序列" prop="sequenceChildName"
                          :rules="[{required: true, message: '子序列不能为空', trigger: 'blur'}
                 ]">
              <el-input disabled v-model="inputForm.sequenceChildName" placeholder="请填写子序列名称"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="职层" prop="levelName"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.rankRstLevelDTO.id" style="width:100%" placeholder="请选择职层">
                <el-option
                  v-for="rankLevel in rankLevelList"
                  :key="rankLevel.id"
                  :label="rankLevel.name"
                  :value="rankLevel.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="适应职务" prop="rankRstPositionDTOList"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.rankRstPositionDTOIdList" style="width:100%" multiple placeholder="请选择适应职务">
                <el-option
                  v-for="rankRstPositionDTO in rankRstPositionDTOList"
                  :key="rankRstPositionDTO.id"
                  :label="rankRstPositionDTO.name"
                  :value="rankRstPositionDTO.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="适应岗位" prop="rankRstPostDTOList"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.rankRstPostDTOIdList" style="width:100%" multiple placeholder="请选择适应岗位">
                <el-option
                  v-for="rankRstPostDTO in rankRstPostDTOList"
                  :key="rankRstPostDTO.id"
                  :label="rankRstPostDTO.name"
                  :value="rankRstPostDTO.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import RankSystemTableService from '@/api/rank/RankSystemTableService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          rankId: '',
          rankName: '',
          useFlag: '',
          sequenceName: '',
          sequenceChildName: '',
          levelId: '',
          levelName: '',
          rankRstPositionDTOIdList: [],
          rankRstPostDTOIdList: [],
          rcId: '',
          rsId: '',
          rankRstLevelDTO: {
            id: ''
          }
        },
        isShowC: false,
        rankLevelList: [],
        rankRstPositionDTOList: [],
        rankRstPostDTOList: []
      }
    },
    rankSystemTableService: null,
    created () {
      this.rankSystemTableService = new RankSystemTableService()
    },
    methods: {
      init (method, rankId, rcId, rsId, isShowC) {
        this.isShowC = isShowC
        this.method = method
        this.inputForm = {
          id: '',
          rankId: '',
          rankName: '',
          useFlag: '',
          sequenceName: '',
          sequenceChildName: '',
          levelId: '',
          levelName: '',
          rankRstPositionDTOIdList: [],
          rankRstPostDTOIdList: [],
          rcId: '',
          rsId: '',
          rankRstLevelDTO: {
            id: ''
          }
        }
        this.inputForm.rankId = rankId
        this.inputForm.rcId = rcId
        this.inputForm.rsId = rsId
        if (method === 'add') {
          this.title = `新建体系表`
        } else if (method === 'edit') {
          this.title = '修改体系表'
        } else if (method === 'view') {
          this.title = '查看体系表'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.rankSystemTableService.queryById(this.inputForm.rankId, this.inputForm.rcId, this.inputForm.rsId).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.inputForm.rcId = rcId
              this.inputForm.rsId = rsId
              console.log('inputForm', this.inputForm)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.rankSystemTableService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      queryAllRankLevel () {
        this.rankSystemTableService.queryAllRankLevel().then(({data}) => {
          this.rankLevelList = JSON.parse(JSON.stringify(data))
        })
      },
      queryAllPosition () {
        this.rankSystemTableService.queryAllPosition().then(({data}) => {
          this.rankRstPositionDTOList = JSON.parse(JSON.stringify(data))
        })
      },
      queryAllPost () {
        this.rankSystemTableService.queryAllPost().then(({data}) => {
          this.rankRstPostDTOList = JSON.parse(JSON.stringify(data))
        })
      }
    }
  }
</script>
