<template>
  <div>
    <xy-dialog
      :z-index="zIndex"
      ref="dialog"
      :is-show.sync="isShow"
      type="form"
      :title="type === 'add' ? '新增绩效指标' : '编辑绩效指标'"
      :form="form"
      :rules="rules"
      @submit="submit"
    >
      <template v-slot:year>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            年份 ：
          </div>
          <div class="xy-table-item-content">
            <el-date-picker
              v-model="form.year"
              type="year"
              disabled
              style="width: 300px"
              placeholder="选择年">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template v-slot:target_type_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            一级指标 ：
          </div>
          <div class="xy-table-item-content">
            <el-select
              :popper-append-to-body="false"
              value-key="id"
              :value="typeFormat"
              clearable
              placeholder="请选择一级指标"
              style="width: 300px"
              @change="e => {
                form.target_type_id = e.id;
                target_type2s = e.children;
              }"
            >
              <el-option
                v-for="item in target_types"
                :key="item.id"
                :label="item.name"
                :value="item"
              ></el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:target_type2_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            二级指标 ：
          </div>
          <div class="xy-table-item-content">
            <el-select
              :popper-append-to-body="false"
              v-model="form.target_type2_id"
              clearable
              placeholder="请选择二级指标"
              style="width: 300px"
            >
              <el-option
                v-for="item in target_type2s"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
      </template>

      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            名称 ：
          </div>
          <div class="xy-table-item-content">
            <el-input
              v-model="form.name"
              clearable
              placeholder="请输入名称"
              style="width: 300px"
            ></el-input>
          </div>
        </div>
      </template>
      <template v-slot:unit_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            单位 ：
          </div>
          <div class="xy-table-item-content">
            <el-select
              :popper-append-to-body="false"
              v-model="form.unit_id"
              clearable
              placeholder="请选择单位"
              style="width: 300px"
            >
              <el-option
                v-for="item in unit_ids"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:symbol_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            计算符号 ：
          </div>
          <div class="xy-table-item-content">
            <el-select
              :popper-append-to-body="false"
              ref="symbolSelect"
              v-model="form.symbol_id"
              clearable
              placeholder="请选择计算符号"
              style="width: 300px"
            >
              <el-option
                v-for="item in symbol_ids"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template #is_fixed>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            是否固定指标项目 ：
          </div>
          <div class="xy-table-item-content">
            <el-switch style="width: 300px" v-model="form.is_fixed" :active-value="1" :inactive-value="0"></el-switch>
          </div>
        </div>
      </template>
      <template #evaluation_way>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            建议上传佐证材料 ：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width: 300px" v-model="form.evaluation_way" type="textarea" :autosize="{ minRows: 2 }"></el-input>
          </div>
        </div>
      </template>
      <!-- <template #score>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
            >*</span
            >
            分值 ：
          </div>
          <div class="xy-table-item-content">
            <el-input-number :precision="2" style="width: 300px" v-model="form.score" :controls="false"></el-input-number>
          </div>
        </div>
      </template> -->
      <template v-slot:half_target>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            半年（程）指标值 ：
          </div>
          <div class="xy-table-item-content">
            <template v-if="isDingxing">
              <el-input
                v-model="form.half_target"
                clearable
                placeholder="请输入半年（程）指标值"
                style="width: 300px"
              ></el-input>
            </template>
            <template v-else>
              <el-input-number
                v-model="form.half_target"
                clearable
                placeholder="请输入半年（程）指标值"
                style="width: 300px"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </template>
          </div>
        </div>
      </template>
      <template v-slot:year_target>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red; font-weight: 600; padding-right: 4px"
              >*</span
            >
            全年（程）指标值 ：
          </div>
          <div class="xy-table-item-content">
            <template v-if="isDingxing">
              <el-input
                v-model="form.year_target"
                clearable
                placeholder="请输入全年（程）指标值"
                style="width: 300px"
              ></el-input>
            </template>
            <template v-else>
              <el-input-number
                v-model="form.year_target"
                clearable
                placeholder="请输入全年（程）指标值"
                style="width: 300px"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </template>
          </div>
        </div>
      </template>
    </xy-dialog>
  </div>
</template>

<script>
import { save as planTargetSave } from "@/api/budget/planTarget";
import { show, save } from "@/api/achievements/points";
import { PopupManager } from "element-ui/src/utils/popup"
export default {
  props: {
    target_types: {
      type: Array,
      default: () => [],
    },
    unit_ids: {
      type: Array,
      default: () => [],
    },
    symbol_ids: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      zIndex: PopupManager.nextZIndex(),
      isDingxing: false,
      isShow: false,
      id: "",
      type: "",
      target_type2s: [],

      form: {
        target_type_id: "",
        target_type2_id: "",
        year:'',
        name: "",
        symbol_id: "",
        unit_id: "",
        score: 0.00,
        half_target: "",
        year_target: "",
        is_fixed: 1,
        evaluation_way: "",
      },
      rules: {
        target_type_id: [
          {
            required: true,
            message: "请填写一级指标",
          },
        ],
        target_type2_id: [
          {
            required: true,
            message: "请填写二级指标",
          },
        ],
        year: [
          {
            required: true,
            message: "请填写年份",
          },
        ],
        name: [
          {
            required: true,
            message: "请填写名称",
          },
        ],
        unit_id: [
          {
            required: true,
            message: "请填写单位",
          },
        ],
        symbol_id: [
          {
            required: true,
            message: "请填写计算符号",
          },
        ],
        half_target: [
          {
            required: true,
            message: "请填写半年（程）指标值",
          },
        ],
        year_target: [
          {
            required: true,
            message: "请填写全年（程）指标值",
          },
        ],
        // score: [
        //   {
        //     required: true,
        //     message: "请填写分值",
        //   },
        // ]
      },
    };
  },
  methods: {
    setYear(year){
      this.form.year = year
    },
    setId (id) {
      this.id = id
    },
    setType (type) {
      this.type = type
    },

    show() {
      this.isShow = true;
    },
    hidden() {
      this.isShow = false;
    },
    init() {
      this.form = {
        target_type_id: "",
        target_type2_id: "",
        year:'',
        name: "",
        symbol_id: "",
        unit_id: "",
        score: 0.00,
        half_target: "",
        year_target: "",
        is_fixed: 1,
        evaluation_way: "",
      };
    },
    setForm(key = [], value = []) {
      if (key instanceof Array) {
        key.forEach((key, index) => {
          this.form[key] = value[index] ?? "";
        });
      }
      if (typeof key === "string") {
        this.form[key] = value;
      }
      if (!key) {
        this.init();
      }
    },

    async getDetail() {
      const res = await show({ id: this.id });
      this.$integrateData(this.form, res);

      this.target_type2s = this.target_types?.filter(i => i.id === this.form.target_type_id)[0]?.children
      this.form.year = res.year?res.year+'':''
    },

    submit() {
      if (this.type === "add") {
        if (this.form.hasOwnProperty("id")) {
          delete this.form.id;
        }
      }
      if (this.type === "editor") {
        Object.defineProperty(this.form, "id", {
          value: this.id,
          enumerable: true,
          configurable: true,
          writable: true,
        });
      }
      save(this.form).then((res) => {
        if (/\/budgetList/g.test(this.$route.path) && this.type === 'add') {
          this.$emit('savePlanTarget',res)
        }
        this.$message({
          type: "success",
          message:
            this.type === "add" ? "新增绩效指标" : "编辑绩效指标" + "成功",
        });
        this.isShow = false;
        this.$emit("refresh");
      });
    },
  },
  computed:{
    typeFormat() {
      return this.target_types.filter(item => item.id === this.form.target_type_id)[0]?.name || "";
    },
  },
  watch: {
    isShow(val) {
      if (val) {
        this.zIndex = PopupManager.nextZIndex()
        if (this.type === "editor") {
          this.getDetail();
        }
      } else {
        this.id = "";
        this.type = "";
        this.init();
        this.$refs["dialog"].clearValidate();
        delete this.form.id;
      }
    },
    "form.symbol_id": {
      handler:function(val) {
        this.$nextTick(() => {
          this.isDingxing = this.$refs['symbolSelect']?.selectedLabel === '定性';
        })
      },
      immediate: true
    }
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-input__inner {
  text-align: left;
}

.xy-table-item-label{
  width: 200px;
}
</style>
