<template>
  <!-- <div class="managementInfoForm">managementInfoForm</div> -->
  <view class="managementInfoForm">
    <view>
      <uni-forms
        ref="form"
        :model="form"
        class="form"
        label-position="top"
        label-width="100%"
      >
        <uni-forms-item v-if="false" label="经营服务内容" name="serviceContent">
          <uni-data-select
            v-model="form.serviceContent"
            :localdata="dictLists['business_service_content']"
            placeholder="请选择经营服务内容"
          />
        </uni-forms-item>
        <uni-forms-item class="flex-radio" label="主要经营类型">
          <uni-easyinput
            type="textarea"
            v-model="form.managementType"
            placeholder="请输入内容"
          />
        </uni-forms-item>
        <uni-forms-item label="新农直报认证" name="isAuthentication">
          <uni-data-select
            v-model="form.isAuthentication"
            :localdata="dictLists['sys_check_status']"
            placeholder="请选择新农直报认证"
          />
        </uni-forms-item>
        <uni-forms-item
          class="flex-radio"
          label="是否龙头企业(3年有效期内)"
          name="isLeading"
        >
          <uni-radio-group v-model="form.isLeading">
            <uni-data-checkbox
              v-model="form.isLeading"
              :localdata="dictLists['sys_yes_no']"
            />
          </uni-radio-group>
        </uni-forms-item>
        <uni-forms-item label="示范级别" name="level">
          <uni-easyinput v-model="form.level" placeholder="请输入示范级别" />
        </uni-forms-item>
        <uni-forms-item label="正式职工人数" name="workers">
          <uni-easyinput
            v-model="form.workers"
            placeholder="请输入正式职工人数"
            type="number"
          />
        </uni-forms-item>
        <uni-forms-item v-if="false" label="经营流水(万元)" name="managementFee">
          <uni-easyinput
            v-model="form.managementFee"
            placeholder="请输入经营流水(万元)"
            type="number"
          />
        </uni-forms-item>
        <uni-forms-item  v-if="false" label="农业净收入(万元)" name="incomeFee">
          <uni-easyinput
            v-model="form.incomeFee"
            placeholder="请输入农业净收入(万元)"
            type="number"
          />
        </uni-forms-item>
        <uni-forms-item v-if="false" label="经营主体总收益(万元)" name="totalFee">
          <uni-easyinput
            v-model="form.totalFee"
            placeholder="请输入经营主体总收益(万元)"
            type="number"
          />
        </uni-forms-item>
        <uni-forms-item v-if="false" label="经营范围" name="businessScope">
          <uni-easyinput
            v-model="form.businessScope"
            placeholder="请输入内容"
          />
        </uni-forms-item>
      </uni-forms>

      <view style="height: 44px">
        <button v-if="false" @click="getForm(form)">
          <text>{{ "测 试" }}</text>
        </button>
      </view>
    </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 {
  addManagementInfo,
  updateManagementInfo,
  selectManagementInfo,
} from "@/api/enterprise/managementInfo";
import { updateBaseInfo, selectBaseInfo } from "@/api/enterprise/baseInfo";
export default {
  name: "managementInfoForm",
  components: {},
  props: {},
  data() {
    return {
      baseInfo: {},
      form: {},
      dictLists: {},
    };
  },
  computed: {},
  methods: {
    async getManagementInfo() {
      this.$modal.loading("加载中，请耐心等待...");
      selectBaseInfo()
        .then((response) => {
          this.baseInfo = response.data;
        })
        .then(() => {
          selectManagementInfo()
            .then((response) => {
              let form = response.data;
              if (!(form === undefined || form.id === null || form.id === "")) {
                this.form = response.data;
              }
            })
            .catch(() => {});
        })
        .then(() => {
          this.reset();
          this.$modal.closeLoading();
        });
    },
    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);
          }
        },
      });
    },
    async reset() {
      this.form = {
        id: null,
        baseId: null,
        serviceContent: this.baseInfo.serviceContent,
        managementType: null,
        isAuthentication: null,
        isLeading: "0",
        level: null,
        workers: null,
        managementFee: null,
        incomeFee: null,
        totalFee: null,
        businessScope: this.baseInfo.natureBusiness,
      };
    },
    async initializeDict() {
      this.dictByType("business_service_content");
      this.dictByType("sys_check_status");
      this.dictByType("sys_yes_no");
    },
    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);
      });
    },
    cencel() {
      this.naviBack();
    },
    submitForm() {
      this.baseInfo.serviceContent = this.form.serviceContent;
      this.baseInfo.natureBusiness = this.form.businessScope;
      updateBaseInfo(this.baseInfo).then(() => {
        let isNew =
          this.form.id === null ||
          this.form.id === undefined ||
          this.form.id === "";
        if (isNew) {
          addManagementInfo(this.form).then((response) => {
            this.naviBack();
          });
        } else {
          updateManagementInfo(this.form).then((response) => {
            this.naviBack(isNew);
          });
        }
      });
    },
    showConfirm() {
      this.$refs["form"].validate().then((res) => {
        this.$cus_modal.confirm().then((conf_res) => {
          if (conf_res.confirm === true) {
            this.submitForm();
          }
        });
      });
    },
  },
  watch: {},

  // 页面周期函数--监听页面加载
  onLoad() {
    this.reset().then(() => {
      this.getManagementInfo();
    });
    this.initializeDict();
  },
  // 页面周期函数--监听页面初次渲染完成
  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>
.managementInfoForm {
  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>