<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="22">
          <el-form-item label="姓名：">
            <p v-if="form.view == '查看'">{{ form.staffName }}</p>
            <el-select
              v-model="form.staffName"
              filterable
              @change="staffChange"
              style="width: 100%;"
              v-else
            >
              <el-option
                v-for="item in staffArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option
            ></el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="所属部门：">
            <p v-if="form.view == '查看'">{{ form.deptName }}</p>

            <el-select
              v-else
              v-model="form.deptId"
              style="width: 100%;"
              disabled
            >
              <el-option
                v-for="item in deptArr"
                :key="item.id"
                :label="item.deptName"
                :value="item.id"
              >
              </el-option
            ></el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="窗口编号：">
            <p v-if="form.view == '查看'">{{ form.windowNum }}</p>

            <el-input
              v-else
              v-model="form.windowNum"
              placeholder="请输入窗口编号"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="违规类型：">
            <p v-if="form.view == '查看'">
              {{
                form.irregularType == 1
                  ? "脱岗"
                  : form.irregularType == 2
                  ? "离岗"
                  : form.irregularType == 3
                  ? "玩手机"
                  : form.irregularType == 4
                  ? "睡觉"
                  : "--"
              }}
            </p>

            <el-select
              v-model="form.irregularType"
              v-else
              placeholder="请选择违规类型"
              style="width: 100%;"
            >
              <el-option
                v-for="item in sourceArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option
            ></el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="22">
          <el-form-item label="发生时间：">
            <p v-if="form.view == '查看'">{{ form.happenTime }}</p>

            <el-date-picker
              v-else
              v-model="form.happenTime"
              type="datetime"
              placeholder="选择日期时间"
              style="width: 100%;"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="时长：">
            <p v-if="form.view == '查看'">{{ form.duration }}</p>

            <el-input
              v-else
              v-model="form.duration"
              placeholder="请输入时长（xx分xx秒）"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="报警时间：">
            <p v-if="form.view == '查看'">{{ form.alarmTime }}</p>

            <el-date-picker
              v-else
              v-model="form.alarmTime"
              type="datetime"
              placeholder="选择日期时间"
              style="width: 100%;"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="图片凭证：">
            <div v-if="form.view == '查看'">
              {{ form.snapPath ? picMatter(form.snapPath) : "--" }}
            </div>
            <ImageUpload
              v-else
              :value="form.snapPath"
              @input="replaceImage"
            ></ImageUpload>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="22">
          <el-form-item label="绩效规则：">
            <p v-if="form.view == '查看'">{{ form.ruleNme }}</p>
            <div v-else>
              <el-col :span="22">
                <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-col>
              <el-col :span="22">
                <el-select
                  v-model="form.ruleId"
                  style="width: 100%;margin-top: 10px;"
                  @change="ruleChange"
                >
                  <el-option
                    v-for="item in ruleArr"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option></el-select
              ></el-col>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="加分/扣分分值：">
            <p>{{ form.score }}</p>
          </el-form-item>
        </el-col>
      </el-row>

      <form-buttons
        @submit="submitForm"
        v-show="form.view != '查看'"
        v-if="pageInfo.type != 'view'"
        noCancelBtn
      />
    </el-form>
    <!-- <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <Field
          label="员工ID"
          prop="staffId"
          v-model="form.staffId"
          placeholder="请输入员工ID"
        />
        <Field
          label="员工姓名"
          prop="staffName"
          v-model="form.staffName"
          placeholder="请输入员工姓名"
        />
        <Field
          label="工号"
          prop="workNum"
          v-model="form.workNum"
          placeholder="请输入工号"
        />
        <Field
          label="窗口编号"
          prop="windowNum"
          v-model="form.windowNum"
          placeholder="请输入窗口编号"
        />
        <Field
          label="所属部门"
          prop="deptId"
          v-model="form.deptId"
          placeholder="请输入所属部门"
        />
        <Field
          label="所属部门名称"
          prop="deptName"
          v-model="form.deptName"
          placeholder="请输入所属部门名称"
        />
        <Field
          label="违规类型"
          prop="irregularType"
          v-model="form.irregularType"
          type="select"
          :enumData="dict.irregularType"
          placeholder="请选择违规类型"
        />
        <Field
          label="发生时间"
          prop="happenTime"
          v-model="form.happenTime"
          type="date"
        />
        <Field
          label="持续时间，秒"
          prop="duration"
          v-model="form.duration"
          placeholder="请输入持续时间，秒"
        />
        <Field
          label="报警时间"
          prop="alarmTime"
          v-model="form.alarmTime"
          type="date"
        />
        <Field
          label="图片凭证地址"
          prop="snapPath"
          v-model="form.snapPath"
          type="textarea"
          placeholder="请输入图片凭证地址"
        />
        <Field
          label="绩效规则id"
          prop="ruleId"
          v-model="form.ruleId"
          placeholder="请输入绩效规则id"
        />
        <Field
          label="规则名称"
          prop="ruleName"
          v-model="form.ruleName"
          placeholder="请输入规则名称"
        />
        <Field
          label="加分/扣分方式"
          prop="subMethod"
          v-model="form.subMethod"
          type="select"
          :enumData="dict.subMethod"
          placeholder="请选择扣分方式"
        />
        <Field
          label="加分/扣分人员"
          prop="deductPerson"
          v-model="form.deductPerson"
          placeholder="请输入扣分人员"
        />
        <Field
          label="加分/扣分时间"
          prop="deductTime"
          v-model="form.deductTime"
          type="date"
        />
        <Field
          label="扣分或增加分值"
          prop="score"
          v-model="form.score"
          placeholder="请输入扣分或增加分值"
        />
        <Field
          label="处理状态"
          prop="processStatus"
          v-model="form.processStatus"
          type="select"
          :enumData="dict.processStatus"
          placeholder="请选择处理状态"
        />
        <Field
          label="备注"
          prop="remark"
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
        />
      </el-row>

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

<script>
import form from "@/assets/mixins/formdialog";
import { timestampToTime } from "@/assets/utils/dateFormat.js";

export default {
  name: "PerformEffectRecordDetail",
  mixins: [form],
  components: {},
  created() {
    this.changePath("perform/effect/record");
    // 获取工作人员列表
    this.$post("/staff/list", { page: 1, size: -1 }).then((res) => {
      if (res.code == 1) {
        this.staffArr = res.data.data;
      } else {
        this.staffArr = [];
      }
    });
    // 获取部门列表
    this.$post("/dept/list", { page: 1, size: -1 }).then((res) => {
      if (res.code == 1) {
        this.deptArr = res.data.data;
      } else {
        this.deptArr = [];
      }
    });

    // 获取规则分类
    this.$post("/perform/rules/category/list", { page: 1, size: -1 }).then(
      (res) => {
        if (res.code == 1) {
          this.kaoqinCateArr = res.data.data;
        } else {
          this.kaoqinCateArr = [];
        }
      }
    );
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "效能绩效记录信息",
      // 是否显示弹出层
      open: false,
      direction: "rtl",
      toString: ["irregularType", "subMethod", "processStatus"],
      toDate: ["happenTime", "alarmTime", "deductTime"],
      // 表单校验
      rules: {
        staffName: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
          { max: 64, message: "最多只能录入64个字符", trigger: "blur" },
        ],
        createTime: [{ required: true, message: "请选择创建时间" }],
      },
      staffArr: [
        {
          label: "员工1",
          value: "1",
        },
      ],
      staffArr: [],
      deptArr: [],
      kaoqinArr: [],
      fileList: [],
      kaoqinCateArr: [],
      ruleArr: [],
      sourceArr: [
        {
          label: "脱岗",
          value: "1",
        },
        {
          label: "离岗",
          value: "2",
        },
        {
          label: "玩手机",
          value: "3",
        },
        {
          label: "睡觉",
          value: "4",
        },
      ],
      form: {},
      baseUrl: process.env.VUE_APP_API_BASE_URL + "/",
      dateValue: "",
      timeValue: "",
    };
  },

  methods: {
    picMatter(val) {
      if (val) {
        return (
          <el-image
            src={val.indexOf("http") == -1 ? this.baseUrl + val : val}
            preview-src-list={
              val.indexOf("http") == -1 ? this.baseUrl + val : val
            }
            style="width: 100px"
          ></el-image>
        );
      } else {
        return val;
      }
    },
    ruleChange(val) {
      let arr = this.ruleArr.filter((v) => v.id == val);
      this.form.score = arr && arr[0].score;
      this.form.checkStatus == 1 ? (this.form.checkStatus = 2) : "";
    },
    cateChange() {
      this.$post("/perform/rules/list", {
        page: 1,
        size: -1,
        categoryId: this.form.categoryId,
      }).then((res) => {
        this.ruleArr = res.data.data;
        this.form.ruleId = "";
      });
    },
    replaceImage(val) {
      this.form.snapPath = val;
    },
    staffChange(val) {
      console.log(val);
      let arr = this.staffArr.filter((v) => v.id == val);
      this.form.deptId = arr && arr.length > 0 ? arr[0].deptId : "";
    },
    /** 编辑 */
    edit(row) {
      this.reset();
      this.query = { id: row.id };
      // this.urls.currUrl = "perform/effect/record/edit";
      // this.getData();
      this.$get("/perform/effect/record/info", { id: row.id }).then((res) => {
        if (res.code == 1) {
          this.form = res.data;
        } else {
          this.form = row;
        }
        for (let key in this.form) {
          key == "actualAttendTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "attendanceDate"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "createTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "errorTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "updateTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "checkTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "deductTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "goOffTimeStr"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "happenTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "alarmTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : "";
        }
        this.form.processStatus = 2;
        this.form.view = "核查";
        this.staffChange(this.form.staffId);
        this.$post("/perform/rules/list", {
          page: 1,
          size: -1,
          categoryId: this.form.categoryId,
        }).then((res) => {
          this.ruleArr = res.data.data;
        });

        this.open = true;
      });
      this.pageInfo.type = "edit";
      this.title = "修改效能绩效记录信息";
    },
    /** 新增 */
    add(row) {
      this.reset();
      // this.urls.currUrl = "perform/effect/record/add";
      // this.getData();
      this.open = true;
      this.pageInfo.type = "add";
      this.title = "新增效能绩效记录信息";
    },
    /** 查看*/
    view(row) {
      this.reset();
      this.query = { id: row.id };
      // this.urls.currUrl = "perform/effect/record/view";
      // this.getData();
      this.$get("/perform/effect/record/info", { id: row.id }).then((res) => {
        if (res.code == 1) {
          this.form = res.data;
        } else {
          this.form = row;
        }
        for (let key in this.form) {
          key == "actualAttendTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "attendanceDate"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "createTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "errorTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "updateTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "checkTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "deductTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "goOffTimeStr"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "happenTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : key == "alarmTime"
            ? (this.form[key] = timestampToTime(this.form[key], 6))
            : "";
        }
        this.form.processStatus = 1;
        this.form.view = "查看";
        this.open = true;
      });
      this.pageInfo.type = "view";
      this.title = "效能绩效记录信息详细";
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
    },
    /**获取数据后弹框 */
    afterRender(data) {
      this.open = true;
    },

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

    // 表单重置
    reset() {
      this.form = {
        staffId: null,
        staffName: "",
        workNum: "",
        windowNum: "",
        deptId: null,
        deptName: "",
        irregularType: null,
        happenTime: null,
        duration: 0,
        alarmTime: null,
        snapPath: "",
        ruleId: null,
        ruleName: "",
        subMethod: "",
        deductPerson: "",
        deductTime: null,
        score: "",
        processStatus: 1,
        remark: "",
        categoryId: "",
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
