<template>
  <!-- 弹出框表单 -->
  <el-drawer
    :title="title"
    :visible.sync="open"
    :direction="direction"
    size="50%"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="20">
          <el-form-item label="分类：">
            <el-select
              v-model="form.categoryId"
              style="width: 100%;"
              @change="cateChange"
            >
              <el-option
                v-for="item in kaoqinCateArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option
            ></el-select>
          </el-form-item>
        </el-col>
        <Field
          :span="20"
          label="考核内容简称"
          prop="name"
          v-model="form.name"
          placeholder="请输入考核内容简称"
        />
        <Field
          :span="20"
          label="评分标准"
          prop="content"
          v-model="form.content"
          type="textarea"
          placeholder="请输入评分标准"
        />
        <Field
          :span="20"
          label="增减类型"
          prop="subAddType"
          v-model="form.subAddType"
          type="select"
          :enumData="dict.subAddType"
          placeholder="请选择增减类型"
        />
        <Field
          :span="20"
          label="加分/扣分"
          prop="score"
          v-model="form.score"
          placeholder="请输入扣分或增加分值"
        />
        <Field
          :span="20"
          label="关联负责人"
          prop="assoOwner"
          v-model="form.assoOwner"
          type="radio"
          :enumData="dict.assoOwner"
          placeholder="请选择是否关联负责人"
        />
        <Field
          :span="20"
          v-if="form.assoOwner == 1"
          label="负责人扣分"
          prop="ownerScore"
          v-model="form.ownerScore"
          placeholder="请输入负责人扣分或增加分值"
        />
        <Field
          :span="20"
          label="备注"
          prop="remark"
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
        />
      </el-row>

      <form-buttons
        v-if="pageInfo.type != 'view'"
        @submit="submitForm"
        noCancelBtn
      />
    </el-form>
  </el-drawer>
</template>

<script>
import form from "@/assets/mixins/formdialog";
export default {
  name: "PerformRulesDetail",
  mixins: [form],
  components: {},
  created() {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "规则",
      // 是否显示弹出层
      open: false,
      direction: "rtl",
      toString: ["subAddType", "assoOwner", "type", "categoryId"],
      toDate: [],
      // 表单校验
      rules: {},
      urls: {
        currUrl: "/perform/rules/list",
        editUrl: "/perform/rules/edit",
        saveUrl: "/perform/rules/save",
      },
      kaoqinCateArr: [],
    };
  },

  methods: {
    /** 编辑 */
    edit(row, arr, obj) {
      this.reset();
      this.query = { id: row.id };
      // this.urls.currUrl = "perform/rules/edit";
      // this.getData();
      this.$get("/perform/rules/info", { id: row.id }).then((res) => {
        console.log(res);
        if (res.code == 1) {
          this.dict = res.dict;
          this.form = res.data;
        } else {
          this.form = {};
        }
        // this.form.categoryId = obj ? obj.id : "";
        this.form.assoOwner =
          this.form.assoOwner == 0 || this.form.assoOwner == 1
            ? JSON.stringify(this.form.assoOwner)
            : "";
        this.form.subAddType = this.form.subAddType
          ? JSON.stringify(this.form.subAddType)
          : "";
        this.open = true;
        console.log(this.form);
      });
      this.kaoqinCateArr = arr;
      this.pageInfo.type = "edit";
      this.title = "修改规则";
    },
    /** 新增 */
    add(row, obj) {
      this.reset();
      // this.urls.currUrl = "perform/rules/add";
      // this.getData();
      this.form.type = obj.type;
      this.dict = {
        subAddType: {
          "1": "增加",
          "2": "扣除",
        },
        assoOwner: {
          "0": "否",
          "1": "是",
        },
        type: {
          "1": "考勤绩效",
          "2": "评价绩效指标",
          "3": "办件绩效指标",
          "4": "效能绩效指标",
          "5": "其它绩效指标",
        },
        categoryId: {
          "11": "种类2",
          "9": "评价分类",
          "10": "种类1",
        },
      };
      this.form.assoOwner = "";
      this.form.subAddType = "";
      this.kaoqinCateArr = row;
      this.form.categoryId = obj ? obj.id : "";
      this.open = true;
      this.pageInfo.type = "add";
      this.title = "新增规则";
    },
    /** 查看*/
    view(row) {
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "perform/rules/view";
      this.getData();
      this.pageInfo.type = "view";
      this.title = "规则信息详细";
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
    },
    /**获取数据后弹框 */
    afterRender(data) {
      this.form.type = 1;
      this.open = true;
    },

    afterSubmit(data) {
      this.open = false;
      this.$emit("ok");
    },

    // 表单重置
    reset() {
      this.form = {
        categoryId: "",
        categoryName: "",
        name: "",
        content: "",
        subAddType: "",
        score: 0.0,
        assoOwner: "",
        ownerScore: 0.0,
        remark: "",
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
