<template>
  <el-dialog title="监督" :visible.sync="dialogVisible" width="60%" center @close="close">
    <el-form :model="form" :rules="rules" ref="ruleFormRef" :disabled="disabled" label-width="100px">
      <el-form-item label="监督结果：">
        <div>{{ message }}</div>
      </el-form-item>
      <el-form-item label="审核意见" prop="supervision">
        <el-radio-group v-model="form.supervision" @input="supervisionChange">
          <el-radio :label="true">通过</el-radio>
          <el-radio :label="false">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="审核说明"
        prop="remarks"
        :rules="[
          {
            required: !form.supervision,
            message: '请输入审批说明',
            trigger: 'blur'
          }
        ]"
      >
        <el-input type="textarea" v-model="form.remarks"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button :disabled="disabled" type="primary" @click="submitInfo">提 交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {supervisionCreateApi, supervisionTaskResultApi, supervisionTaskCheckApi} from '@/api/monitor.js';
import {mapGetters} from 'vuex';
export default {
  data() {
    return {
      disabled: false,
      dialogVisible: false,
      message: '',
      supervisionId: '',
      form: {
        pkgId: '',
        supervision: '',
        remarks: ''
      },
      rules: {
        supervision: [{required: true, message: '请选择审核状态'}]
      }
    };
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    async showInfo(row) {
      this.form.pkgId = row.id;
      const res = await supervisionTaskResultApi(row.id);
      if (res.data.code === 0) {
        this.message = res.data.data;
      }
      const roles = this.userInfo.roles;

      const supervisionState = row.supervision ? row.supervision.supervisionState : '';

      const rolesList = [49, 52, 53, 56];
      const flagA = rolesList.some(item => roles.includes(item));

      const rolesList2 = [51, 54, 55, 61];
      const flagB = rolesList2.some(item => roles.includes(item));

      const rolesList3 = [57, 58, 59, 60];
      const flagC = rolesList3.some(item => roles.includes(item));

      const disabledFlagA = flagA && supervisionState;
      const disabledFlagB = flagB && supervisionState !== 'INIT';
      const disabledFlagC = flagC && supervisionState !== 'CKT1';
      const disabledFlagD = rolesList.includes(39) && supervisionState !== 'CKT2';
      const disabledFlagE = supervisionState === 'PASSED' || supervisionState === 'REJECTED';

      if (disabledFlagA || disabledFlagB || disabledFlagC || disabledFlagD || disabledFlagE) {
        this.disabled = true;
        this.form.remarks = row.supervision.remarks;
        this.form.supervision = row.supervision.supervisionResult;
      }

      if (row.supervision && row.supervision.supervisionId) {
        this.supervisionId = row.supervision.supervisionId;
      }
      this.dialogVisible = true;
    },
    supervisionChange(e) {},

    submitInfo() {
      this.$refs.ruleFormRef.validate(async valid => {
        if (valid) {
          let obj = {
            pkgId: this.form.pkgId,
            supervision: this.form.supervision,
            remarks: this.form.remarks,
            resultInfo: this.message
          };
          let res;
          const roles = this.userInfo.roles;
          if (roles.includes(49)) {
            res = await supervisionCreateApi(obj);
          } else {
            obj.supervisionId = this.supervisionId;
            res = await supervisionTaskCheckApi(obj);
          }
          if (res.data.data) {
            this.$message.success('提交成功');
            this.close();
            this.$emit('success');
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.form = {
        pkgId: '',
        supervision: '',
        remarks: ''
      };
      this.disabled = false;
      this.dialogVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped></style>
