<template>
  <div class="tjyj-box">
    <TopNav navTitle="添加意见" />
    <div class="info_box">
      <div class="yjlx-box info_box item_box">
        <div class="title">意见类型</div>
        <van-radio-group v-model="isActive" direction="horizontal" @change="chooseRadio">
          <van-radio :disabled="status" v-for="(item, index) in radioList" :key="index" :name="item.val">{{ item.name
                      }}</van-radio>
        </van-radio-group>
      </div>

      <div class="item_box">
        <van-field v-model="seleatDate" rows="2" :disabled="status" label="系统来源" label-width="60px" input-align="right"
          right-icon="arrow-down" @click="isShowType = true" />
        <van-action-sheet v-model:show="isShowType">
          <van-picker :columns="xtlyList" @confirm="groupChange" @cancel="isShowType = false" />
        </van-action-sheet>
      </div>

      <div class="item_box">
        <van-field :disabled="status" v-model="sugesstName" rows="2" label="意见名称" label-width="60px"
          input-align="right" />
      </div>

      <div class="item_box">
        <van-field v-model="fknrCnt" :disabled="status" rows="2" label="意见描述" autosize type="textarea" label-width="60px"
          :count="!status ? true : false" maxlength="200" placeholder="请输入内容" show-word-limit />
      </div>

      <div class="info_box item_box">
        <div class="title">文件上传</div>
        <van-uploader :after-read="afterRead" :disabled="status" v-model="fileList" />
      </div>
    </div>
    <div class="submit_but" v-if="status">
      <van-button round @click="status = 0" block type="primary">编辑</van-button>
      <van-button round :loading="loading" block @click="handleDelete" type="danger">删除</van-button>
    </div>
    <div class="submit_but" v-else>
      <van-button :loading="loading" round @click="submit" block type="primary">提交</van-button>
    </div>
  </div>
</template>

<script setup>
import TopNav from "../../../components/topNav.vue";
import {
  editYyzxApi,
  deleteYyzxApi,
  addYyzxApi,
  uploadImg,
} from "../../../api/yyzxApi.js";
import { baseUrl2 } from "../../../utils/request";
import { ref, onMounted, reactive } from "vue";
import { useRouter } from "vue-router";
import { Toast } from "vant";
import { hintToast } from "../../../utils/tools";
let userInfo = JSON.parse(getStorage("userInfo")); //用户信息
const router = useRouter();
const editInfo = ref({});
const yjlxValue = ref(1);
const isActive = ref(1);
const loading = ref(false);
const seleatDate = ref("请选择");
const selectDm = ref("");
const slectType = ref("系统问题");
const sugesstName = ref("系统问题-");
const radioList = ref([
  {
    name: "系统问题",
    val: 1,
  },
  {
    name: "功能改进",
    val: 2,
  },
  {
    name: "新需求",
    val: 3,
  },
]);
const fknrCnt = ref("");
const radioValue = ref("系统问题");
const isShowType = ref(false);
const XTFK_LIST = ref([
  {
    zdmc: "智慧云览",
    zddm: "01",
  },
  {
    zdmc: "智慧云踪",
    zddm: "02",
  },
  {
    zdmc: "智慧云搜",
    zddm: "03",
  },
  {
    zdmc: "智慧云基",
    zddm: "04",
  },
  {
    zdmc: "智慧云侦",
    zddm: "05",
  },
  {
    zdmc: "智慧云法",
    zddm: "06",
  },
  {
    zdmc: "智慧云指",
    zddm: "07",
  },
  {
    zdmc: "智慧云眸",
    zddm: "08",
  },
  {
    zdmc: "智慧云控",
    zddm: "09",
  },
  {
    zdmc: "智慧云防",
    zddm: "10",
  },
  {
    zdmc: "智慧云管",
    zddm: "11",
  },
  {
    zdmc: "智慧云舆",
    zddm: "12",
  },
]);
const xtlyList = ref([
  "智慧云览",
  "智慧云踪",
  "智慧云搜",
  "智慧云基",
  "智慧云侦",
  "智慧云法",
  "智慧云指",
  "智慧云眸",
  "智慧云控",
  "智慧云防",
  "智慧云管",
  "智慧云舆",
]);
const fileList = ref([]);
const status = ref(0);
onMounted(() => {
  if (router.currentRoute.value.query.info) {
    let obj = JSON.parse(router.currentRoute.value.query.info);
    editInfo.value = obj;
    status.value = 1;
    fknrCnt.value = obj.fknr;
    let arr = obj.fkfj ? obj.fkfj.split(",") : [];
    let brr = [];
    arr.forEach((item) => {
      let urlImg = baseUrl2 + "/mosty-api/mosty-base/minio/image/download/" + item;
      let obj = {
        url: urlImg,
        codeId: item,
        isImage: true,
        status: "done",
        message: "上传成功",
      };
      brr.push(obj);
    });
    fileList.value = brr;
    radioList.value.forEach((item) => {
      if (obj.fklx == item.val) {
        isActive.value = item.val;
        slectType.value = item.name;
        sugesstName.value = item.name + "-" + seleatDate.value;
      }
    });
    XTFK_LIST.value.forEach((item) => {
      if (obj.fkxt == item.zddm) {
        seleatDate.value = item.zdmc;
        selectDm.value = item.zddm;
        sugesstName.value = slectType.value + "-" + seleatDate.value;
      }
    });
  } else {
    status.value = 0;
  }
});

function afterRead(file) {
  const data = new FormData();
  data.append("file", file.file);
  file.status = "uploading";
  file.message = "上传中...";
  uploadImg(data)
    .then((res) => {
      file.status = "done";
      file.message = "上传成功";
      file.codeId = res;
    })
    .catch(() => {
      file.status = "failed";
      file.message = "上传失败";
    });
}

function chooseRadio(val) {
  if (!status.value) {
    radioList.value.forEach((item) => {
      if (item.val == val) {
        yjlxValue.value = val;
        slectType.value = item.name;
        sugesstName.value = item.name + "-" + seleatDate.value;
      }
    });
  }
}

// 系统来源提交
function groupChange(val) {
  XTFK_LIST.value.forEach((item) => {
    if (val == item.zdmc) {
      seleatDate.value = item.zdmc;
      selectDm.value = item.zddm;
      sugesstName.value = slectType.value + "-" + seleatDate.value;
    }
  });
  isShowType.value = false;
}

// 删除
function handleDelete() {
  loading.value = true;
  // deleteYyzxApi('http://80.35.14.110:8011/mosty-rzzx/tbRzXtfk/delEntity?id=' + editInfo.value.id).then(res => {
  deleteYyzxApi("/mosty-rzzx/tbRzXtfk/delEntity?id=" + editInfo.value.id)
    .then((res) => {
      Toast.success("删除成功");
      router.push("/yjsj");
      loading.value = false;
    })
    .catch(() => {
      Toast.fail("删除失败");
      loading.value = false;
    });
}
// 提交
function submit() {
  let imgArr = [];
  fileList.value.forEach((item) => {
    imgArr.push(item.codeId);
  });
  loading.value = true;
  if (editInfo.value.id) {
    editInfo.value.fklx = yjlxValue.value;
    editInfo.value.fkxt = selectDm.value;
    editInfo.value.fknr = fknrCnt.value;
    editInfo.value.fkfj = imgArr.join(",");
    let params = { ...editInfo.value };
    editYyzxApi("/mosty-rzzx//tbRzXtfk/updateById", params)
      .then((res) => {
        loading.value = false
        if (res) {
          hintToast("提交成功");
          setTimeout(()=>{ router.go(-1); },2e3)
        } else {
          hintToast("提交失败");
        }
      })
      .catch(() => {
        loading.value = false
        hintToast("提交失败");
      });
  } else {
    let prams = {
      fklx: yjlxValue.value, //1-系统问题，2-功能改进建议，3-新需求
      fkxt: selectDm.value,
      fknr: fknrCnt.value,
      fkfj: imgArr.join(","),
      ssbmdm: userInfo.deptList[0].deptId,
    };
    addYyzxApi("/mosty-rzzx/tbRzXtfk/addEntity", prams).then((res) => {
        loading.value = false
        if (res) {
          hintToast("提交成功");
          setTimeout(()=>{
            router.go(-1);
          },2e3)
        } else {
          hintToast("提交失败");
        }
      })
      .catch(() => {
        loading.value = false
        hintToast("提交失败");
      });
  }
}


function suceessInfo(info) {
  this.$refs.uToast.show({
    type: "success",
    title: "",
    icon: false,
    message: info,
  });
  this.loading = false;
}
</script>

<style lang="scss" scoped>
@import "../../../assets/styles/mixin.scss";

.tjyj-box {
  padding-top: 13vw;
  @include font_color($font-color-theme);
  @include font_size($font_medium_s);
}

.info_box {
  padding: 4vw;
}

.item_box {
  margin-bottom: 4vw;
}

.title {
  margin-bottom: 4vw;
}

::v-deep #van-field-3-input {
  height: 200px !important;
}

.submit_but {
  padding: 2vw 4vw;
  display: flex;
}
</style>
