<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="content">-->
      <!--        <el-input-->
      <!--          v-model="remark.content"-->
      <!--          type="textarea"-->
      <!--          :autosize="{ minRows: 3, maxRows: 6 }"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="责任部门" prop="company" v-if="auditType == '2'">-->
      <!--        <el-tree-select v-model="remark.company" :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
          multiple
        />
      </el-form-item>
      <!--      <el-form-item label="附件" prop="fileUrl">-->
      <!--        <FilesUpload-->
      <!--          v-model:src="remark.fileUrl"-->
      <!--          :generate-pdf="false"-->
      <!--          v-model:file-name-list="fileNameList"-->
      <!--        />-->
      <!--      </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/unqualified/DetailCom.vue";
import {dealFeedback} from "@/api/unqualified/index";
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 auditType = ref("1");
const dutyDept = ref("");
const showDrawer = (row) => {
  row.isEdit = '是';
  auditType.value = row.auditType;
  drawerVisible.value = true;
  dutyDept.value = JSON.parse(row.remark2).user2;
  saleModel.value = row;
  nextTick(() => {
    detailComRef.value && detailComRef.value.refreshView(row);
  });

  getUserInfo();
};

const rules1 = {
  nextRemarkUser: [
    {required: true, message: "请选择处理人", trigger: "change"},
    {min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur"},
  ],
};

defineExpose({showDrawer});

const remarkRef = ref(null);

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

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

  remarkRef.value.resetFields();
};

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

// 用户信息
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() + 1) + "月" + date.getDate() + "日";
  remark.user = dutyDept.value;
  remark.nextRemarkUser = remark.nextRemarkUser.join(",")
  // remark.user = userStore.user.username;
  remarkRef.value.validate((valid) => {
    if (valid) {
      let remark1Copy = {
        content: JSON.parse(saleModel.value.remark1).content,
        user: JSON.parse(saleModel.value.remark1).user,
        time: now,
        state: remark.state,
        user1: remark.user,
      };
      let remark2Copy = {
        content: "",
        user2: remark.nextRemarkUser,
        time: "",
      };
      let p = {
        id: saleModel.value.id,
        remark1: JSON.stringify(remark1Copy),
        remark2: JSON.stringify(remark2Copy),
        currentUserName: remark.nextRemarkUser,
        state: remark.state,
        step: 3, //步骤 1 投书或建议处置意见 2 整改及纠正措施验证 3 处置情况的反馈意见
      };
      console.log(p, "pppppp")
      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>
