<template>
  <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" ref="assignDialog"
             top="5vh" center title="指派"
             class="assign-dialog" :visible.sync="visible">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="100px"
             class="form">
      <el-form-item label="责任人" prop="assignUser"
                    :rules="{
                    required: true, message: '责任人不能为空', trigger: 'change'
                  }">
        <el-select @change="assignUserClick" clearable filterable v-model="ruleForm.assignUser">
          <el-option :key="index" :value="item.id" :label="item.realname"
                     v-for="(item,index) in userList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="assignContact">
        <el-input :disabled="true" v-model="ruleForm.assignContact"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="assignNote">
        <el-input
          rows="4"
          type="textarea"
          placeholder="备注"
          v-model="ruleForm.assignNote"
          maxlength="99"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer no-print">
              <el-button :loading="submitLoading" @click="assignSubmit('ruleForm')" type="primary" size="medium"
                         icon="el-icon-printer">指派</el-button>
                  <el-button size="medium" icon="el-icon-back"
                             @click="$emit('goBack')">返回
                  </el-button>
              </span>
  </el-dialog>

</template>

<script>
  import api from "@/api/question/list";
  import adminApi from '@/api/permission/admin'
  export default {
    name: "assignDialog",
    data() {
      return {
        submitLoading:false,
        userList:[]
      }
    },
    mounted(){
      this.getUserAdminList()
    },
    props:{
      isWater:{
        type:Boolean,
        default(){
          return false
        }
      },
      visible:{
        type:Boolean,
        default(){
          return false
        }
      },
      ruleForm:{
        type:Object,
        default(){
          return {}
        }
      }
    },
    methods:{
      assignUserClick() {
        let ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
        if (ruleForm.assignUser) {
          let child = this.userList.find(item => item.id == ruleForm.assignUser)
          if (child) {
            ruleForm.assignContact = child.mobile
          }
        } else {
          ruleForm.assignContact = ''
        }
        this.ruleForm = Object.assign({}, ruleForm)
      },
      getUserAdminList() {
        adminApi.getUserAdminList().then(res => {
          this.userList = res.data ? res.data : []
        }).catch(err => {
          this.userList = []
        });
      },
      assignSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method='assign'
            if(this.isWater){
              method='waterAssign'
            }
            this.submitLoading = true
            api[method](this.ruleForm).then(res => {
              this.$message({
                type: 'success',
                message: '指派成功！'
              })
              this.submitLoading = false
              this.$emit('submitSuccess')
            }).catch(err => {
              this.submitLoading = false
            });
          }
        })
      },
    }
  }
</script>

<style lang="less">
  .assign-dialog{
    .el-dialog {
      width: 500px !important;
    }
  }
</style>
