<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="110px" label-position="left">
      <el-form-item label="任务名称" prop="jdd_name">
        <el-input v-model="dialogForm.jdd_name"></el-input>
      </el-form-item>
      <el-form-item label="督导类型" prop="jdd_ddlx">
        <el-select v-model="dialogForm.jdd_ddlx" placeholder="请选择" style="width:100%">
          <el-option v-for="item in selectObj.jdd_ddlx.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="督导对象" prop="jdd_dddx">
        <el-select v-model="dialogForm.jdd_dddx" placeholder="请选择" style="width:100%">
          <el-option v-for="item in selectObj.jdd_dddx.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="附件数" prop="jdd_fjs">
        <el-input v-model="dialogForm.jdd_fjs" type="number" :min="0"></el-input>
      </el-form-item>
      <!-- <el-form-item label="责任部门" prop="jdd_uo_id">
        <el-select v-model="dialogForm.jdd_uo_id" filterable placeholder="请选择" style="width:100%" @change="handleChange">
          <el-option v-for="item in departmentData" :key="item.uo_id" :label="item.uo_name" :value="item.uo_id">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="责任人" prop="jdd_zrr_user_id">
        <el-select v-model="dialogForm.jdd_zrr_user_id" filterable placeholder="请选择" style="width:100%" @change="handleUserData">
          <el-option v-for="item in userData" :key="item.user_id" :label="item.user_realname" :value="item.user_id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务起始时间" prop="jdd_start_date">
        <el-date-picker v-model="time" type="daterange" @change="handleTimeChange" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="任务内容" prop="jdd_content">
        <el-input v-model="dialogForm.jdd_content" type="textarea" rows="5"></el-input>
      </el-form-item>
      <!-- <el-form-item v-if="currentId!=''" label="状态" prop="jdd_status">
        <el-select v-model="dialogForm.jdd_status" placeholder="请选择" style="width:100%">
          <el-option v-for="item in selectObj.jdd_status.option" :key="item.v" :label="item.n" :value="item.v"></el-option>
        </el-select>
      </el-form-item> -->
    </el-form>
  </div>
</template>

<script>
import { ddrwAdd, ddrwEdit, ddrwDetail, orgList, userList } from "./api";
import { dealWithDate } from "@/utils/common-util.js";
import { useUserStore } from "@/stores/user";
const userInfo = useUserStore();
export default {
  data() {
    return {
      departmentData: [],
      userData: [],
      dialogForm: {
        jdd_name: "",
        jdd_ddlx: "",
        jdd_dddx: "",
        jdd_fjs: "",
        jdd_uo_id: userInfo.uo_id,
        jdd_uo_name: userInfo.uo_name,
        jdd_zrr_user_id: "",
        jdd_zrr: "",
        jdd_start_date: "",
        jdd_end_date: "",
        jdd_content: "",
        jdd_status: "1",
      },
      time: [],
      rules_check: {
        jdd_name: [{ required: true, message: '请填写任务名称', trigger: 'blur' }],
        jdd_ddlx: [{ required: true, message: '请选择督导类型', trigger: 'change' }],
        jdd_dddx: [{ required: true, message: '请选择督导对象', trigger: 'change' }],
        jdd_fjs: [{ required: true, message: '请填写附件数', trigger: 'blur' }],
        jdd_uo_id: [{ required: true, message: '请选择责任部门', trigger: 'change' }],
        jdd_zrr_user_id: [{ required: true, message: '请选择责任人', trigger: 'change' }],
        jdd_start_date: [{ required: true, message: '请选择任务起始时间', trigger: 'change' }],
        jdd_content: [{ required: true, message: '请填写任务内容', trigger: 'blur' }],
      },
    }
  },
  props: ["currentId", "selectObj"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    async initDepartmentData() {
      let data = await orgList();
      this.departmentData = data.page_data;
    },
    async initUserData() {
      let obj = {
        page: 1,
        limit: 99999,
        dept_id: this.dialogForm.jdd_uo_id
      }
      let data = await userList(obj);
      this.userData = data.page_data;
    },
    handleChange() {
      this.departmentData.map((item) => {
        if (item.uo_id == this.dialogForm.jdd_uo_id) {
          this.dialogForm.jdd_uo_name = item.uo_name;
        }
      })
      this.userData = [];
      this.dialogForm.jdd_zrr_user_id = "";
      this.dialogForm.jdd_zrr = "";
      this.initUserData();
    },
    handleUserData() {
      this.userData.map((item) => {
        if (item.user_id == this.dialogForm.jdd_zrr_user_id) {
          this.dialogForm.jdd_zrr = item.user_realname;
        }
      })
    },
    handleTimeChange(val) {
      if (val) {
        this.dialogForm.jdd_start_date = dealWithDate(val[0]);
        this.dialogForm.jdd_end_date = dealWithDate(val[1]);
      } else {
        this.dialogForm.jdd_start_date = "";
        this.dialogForm.jdd_end_date = "";
      }
    },
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();
      this.time = [];
    },
    async initDetailInfo(id) {
      let res = await ddrwDetail({ jdd_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
      this.time = [this.dialogForm.jdd_start_date, this.dialogForm.jdd_end_date];
    },
    commitData() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if ((this.dialogForm.jdd_ddlx == 1 || this.dialogForm.jdd_ddlx == 3) && this.dialogForm.jdd_fjs == 0) {
            this.$message.error("无指标的附件数量不能填0！");
            return
          }
          let data = {
            issubmit: '1',
            jdbg_dddx_ddrw: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await ddrwAdd(data);
          } else {
            data = Object.assign({ jdd_id: this.currentId }, data);
            obj = await ddrwEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    },
  },
  created() {
    // this.initDepartmentData();
    this.initUserData();
  }
}
</script>

<style>
</style>