<template>
  <el-drawer v-model="drawerVisible" size="1000">
    <div>
      <DetailCom ref="detailComRef"></DetailCom>
    </div>

    <el-form
      ref="remarkRef"
      :model="remark"
      label-width="150"
      :rules="rules1"
      style="max-width: 600px; margin-left: 80px; margin-top: 20px"
    >
      <!--      <el-form-item label="责任部门" prop="user">
        <el-tree-select
            v-model="remark.user"
            :data="userList"
            filterable
        />
      </el-form-item>-->
      <el-form-item
        label="下一步处理人"
        prop="nextRemarkUser"
        v-if="remark.state == 0"
      >
        <el-tree-select
          v-model="remark.nextRemarkUser"
          :data="userList"
          filterable
        />
      </el-form-item>
      <el-form-item label="是否通过" prop="state">
        <el-radio-group v-model="remark.state">
          <el-radio :value="0">同意</el-radio>
          <el-radio :value="1">不同意</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="drawer-bottom">
        <el-button @click="cancelClick">取消</el-button>
        <el-button type="primary" @click="confirmClick">提交</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup>
import DetailCom from "@/views/statistic/inspectionReport/DetailCom.vue";
import { dealFeedback } from "@/api/statistic/inspectionReport";
import { getUserTree } from "@/api/user/index";
import { useUserStore } from "@/store";
import FilesUpload from "@/components/Upload/FilesUpload.vue";
const drawerVisible = ref(false);
const detailComRef = ref(null);
const saleModel = ref({});
const userStore = useUserStore();
const disabledDate = (time) => {
  //选择今天以及今天之后的日期
  return time.getTime() < Date.now() - 8.64e7; //如果没有后面的-8.64e7就是不可以选择
  //选择今天以及今天之前的日期
  //return time.getTime() > Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的
};
const showDrawer = (row) => {
  drawerVisible.value = true;
  saleModel.value = row;

  nextTick(() => {
    detailComRef.value && detailComRef.value.refreshView(row);
  });

  getUserInfo();
};

const rules1 = {
  content: [
    { required: true, message: "请输入投诉或建议处置意见", trigger: "blur" },
    { min: 2, max: 200, message: "长度在 2 到 200 个字符", trigger: "blur" },
  ],
  user: [
    { required: true, message: "请输入姓名", trigger: "blur" },
    { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" },
  ],
  time: [{ required: true, message: "请选择日期", trigger: "change" }],
  nextRemarkUser: [
    { required: true, message: "请选择处理人", trigger: "change" },
    { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" },
  ],
};

defineExpose({ showDrawer });

const remarkRef = ref(null);

const remark = reactive({
  content: "",
  user: "",
  time: "",
  nextRemarkUser: "",
  state: 0,
});
const fileNameList = ref([]);

const cancelClick = () => {
  drawerVisible.value = false;

  remarkRef.value.resetFields();
};

const feedBackEmit = defineEmits(["finish"]);

// 用户信息
const userInfo = ref({});
const userList = ref([]);
const getUserInfo = async () => {
  getUserTree().then((data) => {
    userList.value = data.data;
  });
};

const confirmClick = () => {
  const date = new Date();
  const now =
    date.getFullYear() + "年" + date.getMonth() + "月" + date.getDate() + "日";
  remark.user = userStore.user.username;
  remarkRef.value.validate((valid) => {
    if (valid) {
      let remark4Copy = {
        content: remark.state == 0 ? "同意" : "",
        user: remark.user,
        time: now,
        state: remark.state,
      };
      let remark5 = {
        content: "",
        user: remark.nextRemarkUser, // qinhaibo和dengyu说是user1234的
        time: "",
      };

      let p = {
        id: saleModel.value.id,
        remark4: JSON.stringify(remark4Copy),
        remark5: JSON.stringify(remark5),
        currentUserName: remark.nextRemarkUser,
        state: remark.state,
        step: 2, //步骤 2 审核人 3 批准人
      };

      dealFeedback(p).then(() => {
        feedBackEmit("finish");
        cancelClick();
      });
    } else {
      console.log("remarkRef invalid");
      return false;
    }
  });
};
</script>

<style scoped lang="scss">
@media print {
  #print {
    position: absolute; /* 或 absolute, fixed, 根据需要调整 */
    top: 20px; /* 调整顶部位置 */
    margin: 0; /* 重置边距 */
    width: 1000px;
  }
}
</style>
