<template>
  <view class="page">
    <view class="content">
      <view class="head">
        <span class="title">写日志</span>
      </view>

      <view class="forms">
        <view class="title2">张三的金科环境项目日志</view>
        <view class="title-sub">日志详情</view>
        <uni-forms label-position="left" class="form" ref="form" :rules="rules">
          <view class="box" v-for="item in formData" :key="item.key">
            <view
              v-show="formData.length > 1"
              class="btn-remove"
              @click="removeFormData(item.key)"
              >-</view
            >
            <uni-forms-item
              required
              label="项目名称"
              name="project_name"
              class="formItem"
            >
              <template v-if="!project_id">
                <picker
                  @change="(e) => selectProject(item.key, e)"
                  :range="projectList"
                  range-key="Name"
                >
                  <view class="select" v-if="item.project_name">
                    {{ item.project_name }}
                  </view>
                  <view class="selectPlaceholder" v-else>请选择项目</view>
                </picker>
              </template>
              <view class="select" v-else>
                {{ getProjectName(project_id) }}
              </view>
            </uni-forms-item>
            <uni-forms-item
              required
              label="日志概述"
              name="title"
              class="formItem"
            >
              <input
                v-model="item.title"
                class="input"
                placeholder="请输入日志概述"
              />
            </uni-forms-item>
            <uni-forms-item
              required
              label="日志详情"
              name="content"
              class="formItem"
            >
              <textarea
                v-model="item.content"
                class="textarea"
                placeholder="请输入日志详情"
              />
            </uni-forms-item>
          </view>
        </uni-forms>
        <view class="btn-row">
          <view class="btn-add" @click="addFormData">+ 新增</view>
          <view class="btn-add" @click="deleteLog" v-if="log_id">删除</view>
        </view>
      </view>
      <view class="button" @click="submit">提 交</view>
    </view>
  </view>
</template>

<script>
import {
  createDaily,
  approvalLogDaily,
  approvalDelLog,
  approvalEditLog,
} from "@/services/daily";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      scrollTop: 0,
      // projectList: [],
      project_id: "",
      log_id: "",
      formData: [
        {
          key: +new Date(),
          project_name: "",
          code_id: "",
          title: "",
          content: "",
        },
      ],
      rules: {
        project_name: {
          rules: [
            {
              required: true,
              errorMessage: "请选择项目名称",
            },
          ],
        },
      },
    };
  },
  onLoad() {
  },
  onLoad(options) {
    this.queryProjectList();
    if (options.id) {
      this.initDate(options.id);
    }
    if (options.project_id) {
      this.project_id = options.project_id;
    }
    this.log_id = options.id;
  },
  computed: {
    ...mapState(["projectList"]),
  },
  methods: {
    ...mapActions(["queryProjectList"]),
    getProjectName(id) {
      const item = this.projectList.find(item => item.ID == id)
      return item ? item.Name : '-'
    },
    selectProject(key, e) {
      const select = this.projectList[e.target.value];
      const idx = this.formData.findIndex((item) => item.key == key);
      const item = this.formData[idx];
      const resultData = JSON.parse(JSON.stringify(this.formData));
      resultData[idx] = {
        ...item,
        code_id: select.ID,
        project_name: select.Name,
      };

      this.formData = resultData;
    },
    addFormData() {
      this.formData.push({
        key: +new Date(),
        project_name: "",
      });
    },
    removeFormData(key) {
      let index = this.formData.findIndex((item) => item.key == key);
      this.formData.splice(index, 1);
    },
    async submit() {
      // 从项目列表页进入的日志界面无须选择项目
      if(this.project_id) {
        this.formData.forEach(item => item.code_id = this.project_id)
      }
      await createDaily(this.formData);
      uni.showToast({
        title: "添加成功",
      });
      setTimeout(() => {
        uni.hideToast();
        uni.navigateBack();
      }, 1800);
    },
    async initDate(id) {
      //测试代码 详情接口有问题
      this.formData = [
        {
          project_name: "11111",
          code_id: "11111",
          title: "11111",
          content: "11111",
        },
        {
          project_name: "3333",
          code_id: "22222",
          title: "22222",
          content: "22222",
        },
        {
          project_name: "45",
          code_id: "111511",
          title: "111161",
          content: "111811",
        },
        {
          project_name: "110111",
          code_id: "111911",
          title: "111119",
          content: "119111",
        },
      ];
      // const res = await approvalLogDaily({ id });
      // console.log(res);
    },
    async deleteLog() {
      await approvalDelLog(this.log_id);
      uni.showToast({
        title: "删除成功",
      });
      setTimeout(() => {
        uni.hideToast();
        uni.navigateBack();
      }, 1800);
    },
    async editLog() {
      await approvalEditLog({ log_id: this.log_id, formData: this.formData });
      uni.showToast({
        title: "编辑成功",
      });
      setTimeout(() => {
        uni.hideToast();
        uni.navigateBack();
      }, 1800);
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  width: calc(100% - 60rpx);
  padding: 40rpx;
  position: fixed;
  background: url("~@/static/app-plus/menu-title-bg.png") no-repeat center;
  background-size: 100% 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1;
  .title {
    font: 18px bold;
    color: #fff;
  }
}
.forms {
  padding: 40rpx 34rpx;
  padding-top: 168rpx;
  .titleContent {
    display: flex;
    justify-content: space-between;
  }
  .title2 {
    font-size: 28rpx;
    font-weight: 400;
    color: #4a90e2;
    line-height: 40rpx;
    padding-bottom: 18rpx;
    border-bottom: 1px solid #e8e8e8;
  }

  .title-sub {
    font-size: 28rpx;
    font-weight: 400;
    color: #4a4a4a;
    line-height: 40rpx;
    margin-top: 20rpx;
    margin-bottom: 20rpx;
  }
  .formItem {
  }
  .input,
  .textarea,
  .selectPlaceholder {
    font-size: 28rpx;
    padding-left: 24rpx;
    border: 1rpx solid #c0c0c0;
  }
  .input,
  .selectPlaceholder {
    line-height: 72rpx;
    height: 72rpx;
  }
  .textarea {
    padding-top: 10rpx;
    width: 400rpx;
    height: 200rpx;
  }
}

.form {
  margin: 0 20px;
}
.button {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100rpx;
  background: url("~@/static/btn-bg.png") no-repeat center;
  background-size: 100% 100%;

  font-size: 38rpx;
  font-weight: 500;
  color: #ffffff;
  line-height: 100rpx;
  text-align: center;
}
.btn-row {
  display: flex;
  justify-content: space-between;
}
.btn-add {
  width: 140rpx;
  height: 50rpx;
  border: 1rpx solid #329bfe;
  color: #329bfe;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 48rpx;
}
.box {
  position: relative;
}
.btn-remove {
  position: absolute;
  right: -10px;
  top: -12px;
  width: 12px;
  height: 12px;
  background: #f54444;
  border-radius: 50%;
  line-height: 20rpx;
  text-align: center;
  color: #fff;
  font-weight: bold;
}
.forms {
  padding-bottom: 140rpx;
}
</style>
