<template>
<el-dialog
  title="审批"
  :visible.sync="dialogVisible"
  width="50%"
  :before-close="handleClose">
  <div class="modal">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="75px" class="demo-ruleForm">
          <el-form-item label="请选择" prop="resource">
              <el-radio-group v-model="ruleForm.resource">
              <el-radio label="审批通过"></el-radio>
              <el-radio label="审批不通过"></el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="原因" prop="desc">
              <el-input type="textarea" v-model="ruleForm.desc"></el-input>
          </el-form-item>
      </el-form>
  </div>

  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleConfirm">确 定</el-button>
  </span>
</el-dialog>
</template>
<script>
import { ApprovedOrgBtnApi } from "@/api/staffApi.js";

export default {
  data() {
    let checkDesc = (rule, value, callback) => {
        if(this.ruleForm.resource === '审批不通过' && !value){
            return callback(new Error('请输入'))
        }
        return callback();
    }
    return {
        dialogVisible: false,
        row: {},
        ruleForm: {},
        rules: {
            resource: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          desc: [
            { validator: checkDesc, trigger: 'blur' }
          ],
        }
    }
  },
  methods: {
    handleClose() {
        this.dialogVisible = false;
    },
    handleConfirm() {
        this.$refs.ruleForm.validate((valid) => {
            if (valid) {
                const {resource, desc} = this.ruleForm
                ApprovedOrgBtnApi(this.row.id, {
                    approved: resource === '审批通过' ? true : false,
                    note: desc
                }).then(() => {
                    this.dialogVisible = false;
                    this.ruleForm = {};
                    this.$emit('refresh')
                });
            } else {
                console.log('error submit!!');
                return false;
            }
        });
    },
    showModal(row) {
        this.row = row
        this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.modal{
    padding: 10px 16px 0;
}
::v-deep .el-form-item__label {
    padding-right: 12px;
}

</style>