<template>
  <van-nav-bar
    fixed
    title="信息上报中心"
    left-text=""
    left-arrow
    @click-left="onClickLeft"
  />
  <van-loading size="16px" v-show="isLoading">加载中...</van-loading>
  <div class="main" v-show="!isLoading">
    <p class="title">场所活动上报</p>
    <div class="formArea">
      <p class="miniTitle">基础信息</p>
      <van-cell-group>
        <van-field
          v-model="placeActivity.activityName"
          center
          label="活动名称:"
          placeholder="请填写活动名称"
          input-align="right"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.siteName.name"
          readonly
          required
          label="场所名称:"
          placeholder="请选择"
          input-align="right"
          right-icon="arrow-down"
          @click="showPlace = true"
        />
        <van-popup v-model:show="showPlace" round position="bottom">
          <van-picker
            title="活动场所"
            :columns="places"
            :columns-field-names="customFieldName"
            @cancel="showPlace = false"
            @confirm="getPlace"
          />
        </van-popup>
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.activityTime"
          center
          readonly
          label="活动时间:"
          placeholder="请选择时间"
          input-align="right"
          right-icon="arrow-down"
          @click="showAct = true"
        />
        <van-popup v-model:show="showAct" round position="bottom">
          <van-picker-group
            title="活动时间"
            :tabs="['选择日期', '选择时间']"
            next-step-text="下一步"
            @confirm="getTime"
            @cancel="showAct = false"
          >
            <van-date-picker v-model="currentDate" />
            <van-time-picker
              v-model="currentTime"
              :columns-type="columnsType"
            />
          </van-picker-group>
        </van-popup>
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.participantsNum"
          center
          label="参会人数:"
          placeholder="请填写参会人数"
          input-align="right"
          type="digit"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.knownList.name"
          center
          required
          label="已知名单:"
          placeholder="请选择参会已知名单"
          input-align="right"
          right-icon="arrow-down"
          @click="showPerson = true"
        />
        <van-dialog
          v-model:show="showPerson"
          title="选择人员名单"
          show-cancel-button
          @confirm="reselected"
        >
          <person-list @selected="selected" :type="1"></person-list>
        </van-dialog>
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.reporter.name"
          center
          label="报告人:"
          placeholder="请选择报告人"
          input-align="right"
          right-icon="arrow-down"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.participation"
          center
          label="参加范围:"
          placeholder="请填写参加范围"
          input-align="right"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.place"
          center
          readonly
          label="活动地点:"
          placeholder="请填写参加地点"
          input-align="right"
          right-icon="arrow-down"
          @click="showArea = true"
        />
        <van-popup v-model:show="showArea" round position="bottom">
          <van-cascader
            v-model="cascaderValue"
            title="请选择所在地区"
            :options="options"
            @close="showArea = false"
            @finish="onFinish"
            :field-names="fieldNames"
          />
        </van-popup>
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.placeDel"
          center
          label="详细地址:"
          placeholder="请填写活参加地点"
          input-align="right"
          right-icon="location"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.sourceFunds"
          center
          label="经费来源:"
          placeholder="请选择"
          input-align="right"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.activityPerson.name"
          center
          label="活动负责人:"
          placeholder="请选择活动负责人"
          input-align="right"
          right-icon="arrow-down"
          @click="showReport = true"
        />
        <van-dialog
          v-model:show="showReport"
          title="选择活动负责人"
          show-cancel-button
          @confirm="reselected"
        >
          <person-list @selected="selected" :type="0"></person-list>
        </van-dialog>
      </van-cell-group>
      <van-cell-group>
        <van-field
          v-model="placeActivity.phone"
          center
          type="tel"
          label="联系方式:"
          placeholder="请填写联系方式"
          input-align="right"
        />
      </van-cell-group>
      <van-cell-group>
        <van-field name="uploader" label="安全预案:">
          <template #input>
            <van-uploader
              :after-read="afterRead"
              v-model="fileList"
              multiple
              :max-count="1"
              accept=""
            >
              <van-button icon="plus">上传文件</van-button>
            </van-uploader>
          </template>
        </van-field>
      </van-cell-group>
      <van-cell-group>
        <van-field name="content" label="主题内容:" label-align="top">
          <template #input>
            <wang-editor
              ref="subjectContentEditor"
              v-model="placeActivity.subjectContent"
            />
          </template>
        </van-field>
      </van-cell-group>
    </div>
    <div class="subbtn">
      <van-button type="primary" @click="submit">提交</van-button>
      <van-button type="default" hairline>取消</van-button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import personList from "../personList.vue";
import placeActivityServer from "@/api/placeActivity/placeActivityServer";
import tools from "@/api/sys/tools";
import UserManage from "@/api/user/UserManage";
import { useRoute } from "vue-router";
// 富文本编辑器
import WangEditor from "@/components/editor/WangEditor";
export default {
  components: { personList, WangEditor },
  setup() {
    // 加载
    let isLoading = ref(true);
    // 返回
    const onClickLeft = () => {
      history.back();
    };
    // 活动信息
    let placeActivity = ref({
      id: "",
      activityName: "",
      siteName: {
        id: "",
        name: "",
      },
      activityTime: "",
      participantsNum: "",
      knownList: {
        id: "",
        name: "",
      },
      subjectContent: "",
      reporter: {
        id: "",
        name: "",
      },
      participation: "",
      place: "",
      placeDel: "",
      sourceFunds: "",
      safetyPlan: "",
      activityPerson: {
        id: "",
        name: "",
      },
      phone: "",
      state: "0",
      assessment: "0",
    });
    const subjectContentEditor = ref(null);
    // 根据路由初始化
    let route = useRoute();
    onMounted(() => {
      if (route.query.id) {
        new placeActivityServer().queryById(route.query.id).then((data) => {
          placeActivity.value = data;
          let ids = data.knownList.id.split(",");
          placeActivity.value.knownList.name = "";
          ids.forEach((item) => {
            new UserManage().queryById(item).then((data) => {
              placeActivity.value.knownList.name += data.name + ",";
            });
          });
          subjectContentEditor.value.init(placeActivity.value.subjectContent);
          isLoading.value = false;
        });
      } else {
        subjectContentEditor.value.init("");
        isLoading.value = false;
      }
    });
    // 场所名称
    let places = ref([]);
    const customFieldName = {
      text: "name",
      value: "id",
    };
    new tools()
      .placeList({
        current: 1,
        size: 10000,
      })
      .then(({ records }) => {
        places.value.push(...records);
      });
    let showPlace = ref(false);
    const getPlace = ({ selectedOptions }) => {
      showPlace.value = false;
      placeActivity.value.siteName = selectedOptions[0];
    };
    // 获取活动时间
    let showAct = ref(false);
    let currentDate = ref(["" + new Date().getFullYear(), "01", "01"]);
    let currentTime = ref(["00", "00", "00"]);
    const columnsType = ["hour", "minute", "second"];
    const getTime = () => {
      showAct.value = false;
      placeActivity.value.activityTime = `${currentDate.value.join(
        "-"
      )} ${currentTime.value.join(":")}`;
    };
    // 已知名单
    let showPerson = ref(false);
    // 活动负责人
    let showReport = ref(false);
    let list = {
      value: [],
      type: "",
    };
    const selected = (val, type) => {
      list.value = val;
      list.type = type;
    };
    const reselected = () => {
      let ids = [];
      let names = [];
      if (list.type == 1) {
        list.value.forEach((item) => {
          ids.push(item.id);
          names.push(item.name);
        });
        placeActivity.value.knownList.id = ids.join(",");
        placeActivity.value.knownList.name = names.join(",");
      } else {
        placeActivity.value.activityPerson.id = list.value.id;
        placeActivity.value.activityPerson.name = list.value.name;
      }
    };
    // 地区选择
    let showArea = ref(false);
    const cascaderValue = ref("");
    const fieldNames = {
      text: "name",
      value: "code",
      children: "children",
    };
    // 选项列表，children 代表子选项，支持多级嵌套
    let options = ref([]);
    new tools().treeData().then((res) => {
      options.value.push(res[0]);
    });
    // 全部选项选择完毕后，会触发 finish 事件
    const onFinish = ({ selectedOptions }) => {
      showArea.value = false;
      placeActivity.value.place = selectedOptions
        .map((option) => option.name)
        .join("/");
    };
    // 文件上传
    let fileList = ref([]);
    const afterRead = (file) => {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      new tools()
        .uploadFile(file, `reporting/reportingActivities`)
        .then(({ data }) => {
          fileList.value.push(data);
          placeActivity.value.safetyPlan = data.url;
        });
    };
    // 提交数据
    const submit = () => {
      new placeActivityServer().save(placeActivity.value).then((res) => {
        console.log("提交", res);
      });
    };
    return {
      isLoading,
      placeActivity,
      // 活动场所
      customFieldName,
      showPlace,
      places,
      getPlace,
      // 活动时间
      showAct,
      columnsType,
      currentDate,
      currentTime,
      getTime,
      // 返回
      onClickLeft,
      // 人员选择
      showPerson,
      showReport,
      selected,
      reselected,
      // 地区选择
      showArea,
      fieldNames,
      options,
      onFinish,
      cascaderValue,
      // 文件上传
      afterRead,
      fileList,
      submit,
      subjectContentEditor,
    };
  },
};
</script>

<style scoped>
.main {
  background: #fff;
  position: relative;
  top: 40px;
}
.miniTitle,
.title {
  height: 40px;
  line-height: 40px;
  color: #36a7f3;
  border-bottom: 1px solid #eee;
}
.title {
  font-size: 20px;
  font-weight: 700;
  padding-left: 20px;
}
.miniTitle {
  font-size: 16px;
}
.formArea {
  padding: 0px 20px;
}
.formArea .van-cell-group .van-field__label {
  height: 40px;
}
/* .formArea .van-cell-group .van-cell {
  line-height: 40px;
} */
.van-cell__value .van-field__right-icon .van-icon-location {
  color: #36a7f3 !important;
}
.subbtn {
  margin: 20px;
  text-align: center;
}
.subbtn .van-button {
  width: 40%;
  margin: 5px;
}
.van-uploader .van-button {
  border: none;
  color: #36a7f3;
  top: -4px;
}
.van-dialog {
  width: 80%;
  top: 50%;
}
.van-loading {
  text-align: center;
  margin-top: 80px;
}
#editor {
  width: 98%;
}
</style>