<template>
  <!-- <div class="subsidyForm">subsidyForm</div> -->
  <view class="subsidyForm">
    <view>
      <uni-forms
        ref="form"
        :model="form"
        :rules="rules"
        class="form"
        label-position="top"
        label-width="100%"
        validate-trigger="bind"
      >
        <uni-forms-item label="记账时间" name="time">
          <uni-datetime-picker
            type="date"
            v-model="form.time"
            placeholder="请选择记账时间"
          />
        </uni-forms-item>
        <uni-forms-item label="补助类型" name="typeId">
          <uni-data-select
            v-model="form.typeId"
            placeholder="请选择补助类型"
            :localdata="dictLists['subsidy_type']"
            width="100%"
          />
        </uni-forms-item>
        <uni-forms-item label="金额 / 元" name="amoun">
          <uni-easyinput v-model="form.amoun" placeholder="请输入金额" />
        </uni-forms-item>
      </uni-forms>
    </view>
    <view style="height: 44px">
      <button v-if="false" @click="getForm(form)">
        <text>{{ "测 试" }}</text>
      </button>
    </view>

    <view v-if="true" class="foot">
      <button @click="cencel" :plain="false" class="cencel">
        <text>{{ "取 消" }}</text>
      </button>
      <button @click="showConfirm" type="primary" class="confirm">
        <text>{{ "确 认" }}</text>
      </button>
    </view>
  </view>
</template>

<script>
import {
  listSubsidy,
  getSubsidy,
  delSubsidy,
  addSubsidy,
  updateSubsidy,
  getSubsidyStas,
} from "@/api/lef/subsidy";
export default {
  name: "subsidyForm",
  components: {},
  props: {},
  data() {
    return {
      options: {},
      form: {},
      dictLists: {},
      rules: {
        time: {
          rules: [
            {
              required: true,
              errorMessage: "记账时间不能为空",
            },
          ],
        },
        typeId: {
          rules: [
            {
              required: true,
              errorMessage: "补助类型不能为空",
            },
          ],
        },
        amoun: {
          rules: [
            {
              required: true,
              errorMessage: "金额不能为空",
            },
          ],
        },
      },
    };
  },
  computed: {},
  methods: {
    async getRouter(options) {
      this.options = options;
    },
    naviBack(isNew) {
      let pages = getCurrentPages(); //关于获取页面的官方文档https://uniapp.dcloud.io/api/window/window
      let prevPage = pages[pages.length - 2];
      uni.navigateBack({
        delta: 1,
        success: () => {
          if (isNew !== undefined) {
            setTimeout(function () {
              prevPage.$vm.getMsg(isNew);
            }, 200);
          }
        },
      });
    },
    getSubsidyById() {
      this.$modal.loading("加载中，请耐心等待...");
      getSubsidy(this.options.id).then((res) => {
        this.form = res.data;
        this.$modal.closeLoading();
      });
    },
    cencel() {
      this.naviBack();
    },
    submitForm() {
      let isNew =
        this.form.subsidyId === null ||
        this.form.subsidyId === undefined ||
        this.form.subsidyId === "";
      if (isNew) {
        addSubsidy(this.form).then((response) => {
          this.naviBack(isNew);
        });
      } else {
        updateSubsidy(this.form).then((response) => {
          this.naviBack(isNew);
        });
      }
    },
    async initializeDict() {
      this.dictByType("subsidy_type");
    },
    dictByType(type) {
      this.getDicts(type).then((response) => {
        let dicts = [];
        for (let dict of response.data) {
          let element = {};
          element["value"] = dict.dictValue;
          element["text"] = dict.dictLabel;
          element["label"] = dict.dictLabel;
          dicts.push(element);
        }
        this.$set(this.dictLists, type, dicts);
      });
    },
    showConfirm() {
      this.$refs["form"].validate().then((res) => {
        this.$cus_modal.confirm().then((conf_res) => {
          if (conf_res.confirm === true) {
            this.submitForm();
          }
        });
      });
    },
  },
  watch: {},

  // 页面周期函数--监听页面加载
  onLoad(options) {
    this.initializeDict();
    this.getRouter(options).then(() => {
      if (options.id != undefined) {
        this.getSubsidyById();
      }
    });
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示(not-nvue)
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {
    const that = this
    return that.$listenOB.goBack()
  },
  // 页面处理函数--监听用户下拉动作
  // onPullDownRefresh() { uni.stopPullDownRefresh(); },
  // 页面处理函数--监听用户上拉触底
  // onReachBottom() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  // onPageScroll(event) {},
  // 页面处理函数--用户点击右上角分享
  // onShareAppMessage(options) {},
};
</script>

<style lang="scss" scoped>
page {
  background: white;
}

.subsidyForm {
  background-color: white;
  height: calc(100%);

  .foot {
    width: 100vw;
    height: 44px;
    bottom: 0px;
    display: flex;
    position: fixed;
    z-index: 2;

    .cencel {
      border-radius: 0px;
      border: 0px;
      width: 50%;
      background-color: white;
    }

    .confirm {
      border-radius: 0px;
      border: 0px;
      width: 50%;
      color: white;
    }
  }
}
.form {
  text-align: center;
  margin: auto;
  width: 80%;

  /deep/ .flex-radio {
    uni-radio-group {
      width: 100%;

      .checklist-group {
        display: flex;
        justify-content: space-evenly;
      }
    }
  }

  /deep/ .uni-forms-item {
    .uni-forms-item__content {
      .uni-file-picker {
        .uni-file-picker__header {
          .file-title {
            span {
              color: #ff0000a6;
            }
          }
        }
      }

      .uni-mt-5 {
        .uni-icons {
          background: #007aff21;
          line-height: 35px;
          height: 35px;
        }
      }

      .uni-select-cy {
        z-index: 1;

        .uni-select-cy-select {
          z-index: -500;
        }
      }
    }
  }
}
</style>