<template>
  <view style="width: 100vw;height: 100vh; overflow-x: hidden;background: #fff;">
    <u-navbar title="工作经历"></u-navbar>
    <!-- 公司名称 -->
    <view class="item-line">
      <text class="line-label must"> 公司名称</text>
      <u-input :custom-style="inputStyle" type="text" input-align="right" :border="false" maxlength="20"
        v-model="formData.corporateName" placeholder="请输入" />
    </view>
    <!-- 入职时间 -->
    <view class="item-line item-date" @click="dateStartShowFn">
      <text class="line-label must"> 入职时间</text>
      <u-input :custom-style="inputStyle" type="text" input-align="right" :border="false" v-model="formData.startTime"
        @click.native="dateStartShowFn" placeholder="请选择" disabled />
      <view class="img">
        <image style="width: 15rpx; height: 25rpx; margin-left: 10rpx" src="/static/common/img/row_right.png" />
      </view>
      <!-- 点击盒子 -->
      <view class="input-mask"></view>
    </view>
    <!-- 离职时间 -->
    <view class="item-line item-date" @click="dateEndShowFn">
      <text class="line-label must"> 离职时间</text>
      <u-input :custom-style="inputStyle" type="text" input-align="right" :border="false" v-model="formData.endTime"
        @click.native="dateEndShowFn" placeholder="请选择" disabled />
      <view class="img">
        <image style="width: 15rpx; height: 25rpx; margin-left: 10rpx" src="/static/common/img/row_right.png" />
      </view>
      <!-- 点击盒子 -->
      <view class="input-mask"></view>
    </view>
    <!-- 公司职位 -->
    <view class="item-line">
      <text class="line-label must"> 公司职位</text>
      <u-input :custom-style="inputStyle" type="text" input-align="right" :border="false" maxlength="20"
        v-model="formData.corporatePosition" placeholder="请输入" />
    </view>
    <!-- 工作内容 -->
    <view class="item-text">
      <u-input type="textarea" height="200" maxlength="500" :border="false" v-model="formData.wordDesc"
        placeholder="工作责任描述：请输入内容,字数限制500字以内" />
    </view>
    <!-- 按钮 -->
    <view class="btn-box">
      <u-button style="width: 80%" shape="circle" type="primary" @click="addFn"
        v-if="queryParams.type === 'add'">保存</u-button>
      <template v-if="queryParams.type === 'edit'">
        <u-button style="width: 40%" shape="square" type="error" @click="deleteFn">
          删除
        </u-button>
        <u-button style="width: 40%" shape="square" type="primary" @click="editFn">
          保存
        </u-button>
      </template>
    </view>
    <!-- 开始日期 -->
    <u-picker v-model="dateStartShow" mode="time" :params="{
      year: true,
      month: true,
    }" @confirm="startChangeFn"></u-picker>
    <!-- 离职日期 -->
    <u-picker v-model="dateEndShow" mode="time" :params="{
      year: true,
      month: true,
    }" @confirm="endChangeFn"></u-picker>
    <u-top-tips ref="uTips" :navbar-height="50"></u-top-tips>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        corporateName: "",
        startTime: "", //2025-01
        endTime: "", //2025-02
        corporatePosition: "", //职位
        wordDesc: "", //工作内容
        id: "",
      },
      inputStyle: {
        lineHeight: "120rpx",
        height: "120rpx",
        width: "400rpx",
      },
      dateStartShow: false,
      dateEndShow: false,
      queryParams: {
        type: "add",
      },
    };
  },
  onLoad(e) {
    Object.assign(this.queryParams, e);
    if (e.type === "edit") {
      let currentRow = JSON.parse(localStorage.getItem("workRow"));
      console.log(currentRow, "currentRow");
      Object.assign(this.formData, currentRow);
      this.formData.startTime = currentRow.startTime.slice(0, 7);
      this.formData.endTime = currentRow.endTime.slice(0, 7);
    }
  },
  methods: {
    // 打开起始日期
    dateStartShowFn() {
      this.dateStartShow = true;
    },
    // 起始日期确认结果
    startChangeFn(val) {
      console.log(val, "start");
      this.formData.startTime = val.year + "-" + val.month;
    },
    // 打开离职日期
    dateEndShowFn() {
      this.dateEndShow = true;
    },
    // 离职日期确认结果
    endChangeFn(val) {
      console.log(val, "end");
      this.formData.endTime = val.year + "-" + val.month;
    },
    // 删除
    async deleteFn() {
      uni.showModal({
        // title: "提示",
        content: `确定删除此工作经历？`,
        success: async (res) => {
          if (res.confirm) {
            const req = {
              id: this.formData.id,
            };
            const res = await this.$u.apiList.DelWorkInfoApi(req);
            console.log(res, "res--");
            /* uni.showToast({
              title: "删除成功",
              duration: 2000,
            }); */
            this.$refs.uToast.show({
              title: "删除成功",
              type: "success",
            });
            setTimeout(() => {
              /* uni.redirectTo({
                url: "/pages/my/personalResume/online?type=1",
              }); */
              uni.navigateBack({
                delta: 1, // 返回上一级页面
              });
            }, 1000);
          }
          if (res.cancel) {
            // console.log('用户点击取消');
          }
        },
      });
    },
    validate() {
      return new Promise((res, rej) => {
        if (!this.formData.corporateName) {
          // this.$tips("公司名称不能为空", "error");
          this.$refs.uToast.show({
            title: "公司名称不能为空",
            type: "error",
          });
          rej(false);
          return;
        }
        if (!this.formData.startTime) {
          // this.$tips("入职时间不能为空", "error");
          this.$refs.uToast.show({
            title: "入职时间不能为空",
            type: "error",
          });
          rej(false);
          return;
        }
        if (!this.formData.endTime) {
          // this.$tips("离职时间不能为空", "error");
          this.$refs.uToast.show({
            title: "离职时间不能为空",
            type: "error",
          });
          rej(false);
          return;
        }
        if (!this.formData.corporatePosition) {
          // this.$tips("公司职位不能为空", "error");
          this.$refs.uToast.show({
            title: "公司职位不能为空",
            type: "error",
          });
          rej(false);
          return;
        }
        // if (!this.formData.wordDesc) {
        //   this.$tips("工作内容不能为空", "error");
        //   rej(false);
        //   return;
        // }
        // 离职时间应该大于入职时间
        if (new Date(this.formData.startTime) > new Date(this.formData.endTime)) {
          // this.$tips("入职时间不应大于离职时间", "error");
          this.$refs.uToast.show({
            title: "入职时间不应大于离职时间",
            type: "error",
          });
          rej(false);
          return;
        }
        res(true);
      });
    },
    // 增加
    async addFn() {
      console.log("%c%s", "color:red", "发起请求");
      const result = await this.validate().catch((e) => e);
      console.log(result, "result--");
      if (result === false) {
        return;
      }
      const req = {
        corporateName: this.formData.corporateName,
        wordDesc: this.formData.wordDesc,
        corporatePosition: this.formData.corporatePosition,
        startTime: this.formData.startTime,
        endTime: this.formData.endTime,
        userId: this.vuex_user.id,
      };
      const res = await this.$u.apiList.AddWordInfoApi(req);
      console.log(res, "res--");
      /* uni.showToast({
        title: "提交成功",
        duration: 2000,
      }); */
      this.$refs.uToast.show({
        title: "提交成功",
        type: "success",
      });
      setTimeout(() => {
        /* uni.redirectTo({
          url: "/pages/my/personalResume/online?type=1",
        }); */
        uni.navigateBack({
          delta: 1, // 返回上一级页面
        });
      }, 1000);
    },
    // 保存
    async editFn() {
      console.log("%c%s", "color:red", "发起请求");
      const result = await this.validate().catch((e) => e);
      console.log(result, "result--");
      if (result === false) {
        return;
      }
      const req = {
        corporateName: this.formData.corporateName,
        wordDesc: this.formData.wordDesc,
        startTime: this.formData.startTime,
        endTime: this.formData.endTime,
        corporatePosition: this.formData.corporatePosition,
        id: this.formData.id,
      };
      const res = await this.$u.apiList.UpdateWorkInfoApi(req);
      console.log(res, "res--");
      /* uni.showToast({
        title: "保存成功",
        duration: 2000,
      }); */
      this.$refs.uToast.show({
        title: "保存成功",
        type: "success",
      });
      setTimeout(() => {
        /* uni.redirectTo({
          url: "/pages/my/personalResume/online?type=1",
        }); */
        uni.navigateBack({
          delta: 1, // 返回上一级页面
        });
      }, 1000);
    },
  },
};
</script>

<style lang="scss" scoped>
// 必填的样式
.must {
  position: relative;

  &::before {
    content: "*";
    color: red;
    position: absolute;
    top: -10rpx;
    right: -17rpx;
    transform: scale(0.8);
    /* 相对于父元素的左上角定位 */
  }
}

// 单行样式
.item-line {
  text-align: left;
  padding-left: 30rpx;
  padding-right: 30rpx;
  font-size: 30rpx;
  line-height: 120rpx;
  height: 120rpx;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #f1f2f6;
  background-color: white;

  .line-label {
    // font-size: 30rpx;
    color: black;
  }

  .line-value {
    // font-weight: 700;
    color: #969696;
  }
}

// 返校日期
.item-date {
  padding-left: 30rpx;
  padding-right: 30rpx;
  font-size: 30rpx;
  line-height: 120rpx;
  height: 120rpx;
  display: flex;
  position: relative;
  background-color: white;

  .input-mask {
    position: absolute;
    width: 100%;
    height: 120rpx;
    // background-color: red;
  }

  .img {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 15rpx;
    padding-top: 4rpx;
    margin-left: 5rpx;
    // padding-right: 30rpx;
  }
}

.item-text {
  background-color: white;
  padding: 30rpx;
}

// 按钮
.btn-box {
  position: fixed;
  width: 100%;
  bottom: 0;
  height: 150rpx;
  border-top: 1px solid #f1f2f6;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
}
</style>
