<template>
  <!-- <div class="businessItemsForm">businessItemsForm</div> -->
  <view class="businessItemsForm">
    <view>
      <uni-forms
        ref="form"
        :rules="rules"
        :model="form"
        class="form"
        label-position="top"
        label-width="100%"
        validate-trigger="bind"
      >
        <uni-forms-item label="主营产品名称" name="mainProducts">
          <uni-easyinput
            v-model="form.mainProducts"
            placeholder="请输入主营产品名称"
          />
        </uni-forms-item>
        <uni-forms-item label="计量单位" name="measureUnit">
          <uni-easyinput
            v-model="form.measureUnit"
            placeholder="请输入计量单位"
            :disabled="true"
          />
        </uni-forms-item>
        <uni-forms-item label="数量" name="quantity">
          <uni-easyinput
            v-model="form.quantity"
            placeholder="请输入数量"
            type="number"
          />
        </uni-forms-item>
        <uni-forms-item
          class="flex-radio"
          label="是否注册商标"
          name="isRegistered"
        >
          <uni-radio-group v-model="form.isRegistered">
            <uni-data-checkbox
              v-model="form.isRegistered"
              :localdata="dictLists['sys_yes_no']"
            />
          </uni-radio-group>
        </uni-forms-item>
        <uni-forms-item
          class="flex-radio"
          label="是否获得认证"
          name="isAuthentication"
        >
          <uni-radio-group v-model="form.isAuthentication">
            <uni-data-checkbox
              v-model="form.isAuthentication"
              :localdata="dictLists['sys_yes_no']"
            />
          </uni-radio-group>
        </uni-forms-item>
        <uni-forms-item label="认证证明" name="authenticationUrl">
          <uni-file-picker
            fileMediatype="image"
            limit="1"
            mode="grid"
            title=""
            v-model="authenticationUrl"
            @progress="progress"
            @select="select"
            @succes="authenticationUrlSucces"
          />
        </uni-forms-item>
      </uni-forms>
    </view>

    <view style="height: 44px">
      <button v-if="false">
        <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 {
  listBusinessItems,
  getBusinessItems,
  delBusinessItems,
  addBusinessItems,
  updateBusinessItems,
} from "@/api/enterprise/businessItems";

export default {
  name: "businessItemsForm",
  components: {},
  props: {},
  data() {
    return {
      authenticationUrl: [],
      options: {},
      form: {
        measureUnit: "kg",
      },
      dictLists: {},
      list: [],
      queryParams: {
        baseId: null,
        mainProducts: null,
        measureUnit: null,
        quantity: null,
        isRegistered: null,
        isAuthentication: null,
        authenticationUrl: null,
        addTime: null,
        editTime: null,
        state: "0",
      },
      rules: {
        mainProducts: {
          rules: [
            {
              required: true,
              errorMessage: "请输入主营产品名称",
            },
          ],
        },
        measureUnit: {
          rules: [
            {
              required: true,
              errorMessage: "请输入计量单位",
            },
          ],
        },
        quantity: {
          rules: [
            {
              required: true,
              errorMessage: "请输入数量",
            },
          ],
        },
      },
    };
  },
  computed: {},
  methods: {
    authenticationUrlSucces() {},
    async initializeDict() {
      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);
      });
    },
    getBusinessItemsById() {
      this.$modal.loading("加载中，请耐心等待...");
      getBusinessItems(this.options.id).then((res) => {
        this.form = res.data;
        if (
          this.form.authenticationUrl === "" ||
          this.form.authenticationUrl === null ||
          this.form.authenticationUrl === undefined
        ) {
          this.$set(this.form, ["authenticationUrl"], []);
        } else {
          let authenticationUrl = [];
          authenticationUrl.push(this.form.authenticationUrl);
          this.$set(this.form, ["authenticationUrl"], authenticationUrl);
          this.authenticationUrl = authenticationUrl;
        }
        this.$modal.closeLoading();
      });
    },
    async getRouter(options) {
      this.options = options;
    },
    select(tempFiles) {
      let authenticationUrl = [];
      authenticationUrl.push(tempFiles.tempFilePaths);
      this.form.authenticationUrl = authenticationUrl;
      this.authenticationUrl = authenticationUrl;
    },
    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);
          }
        },
      });
    },
    cencel() {
      this.naviBack();
    },
    submitForm() {
      let form = this.form;
      console.log(form);
      let isNew =
        this.form.id === null ||
        this.form.id === undefined ||
        this.form.id === "";
      if (
        form.authenticationUrl === undefined ||
        form.authenticationUrl === [] ||
        form.authenticationUrl === null ||
        form.authenticationUrl.length === 0
      ) {
        this.form.authenticationUrl = "";
      } else {
        this.form.authenticationUrl = form.authenticationUrl[0].toString(",");
      }
      if (isNew) {
        addBusinessItems(this.form).then((response) => {
          this.naviBack(isNew);
        });
      } else {
        updateBusinessItems(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(options) {
    this.initializeDict();
    this.getRouter(options).then(() => {
      if (options.id != undefined) {
        this.getBusinessItemsById();
      }
    });
  },
  // 页面周期函数--监听页面初次渲染完成
  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;
}

.businessItemsForm {
  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>
