
<template>
  <div class="container-fluid">
    <Navbar
      :title="$t('lang.assetBasicSetting.assetConfig')"
      :is-go-back="false"
    />

    <div class="row" style="margin-bottom: 15px">
      <div class="col-md-12">
        <ul class="nav nav-tabs">
          <li
            role="presentation"
            :class="{ active: functionType == 1 }"
            @click="selectPage(1)"
          >
            <a href="javascript:void(0)">{{
              $t("lang.assetBasicSetting.baseInformation")
            }}</a>
          </li>
          <li
            role="presentation"
            :class="{ active: functionType == 2 }"
            @click="selectPage(2)"
          >
            <a href="javascript:void(0)">{{
              $t("lang.assetBasicSetting.depreciationInformation")
            }}</a>
          </li>
          <li
            role="presentation"
            :class="{ active: functionType == 3 }"
            @click="selectPage(3)"
          >
            <a href="javascript:void(0)">{{
              $t("lang.assetBasicSetting.encodingMethod")
            }}</a>
          </li>
          <li
            role="presentation"
            :class="{ active: functionType == 4 }"
            @click="selectPage(4)"
          >
            <a href="javascript:void(0)">{{
              $t("lang.assetBasicSetting.systemIntegration")
            }}</a>
          </li>
          <li
            role="presentation"
            :class="{ active: functionType == 5 }"
            @click="selectPage(5)"
          >
            <a href="javascript:void(0)">{{
              $t("lang.assetBasicSetting.picture")
            }}</a>
          </li>
        </ul>
      </div>
    </div>
    <div v-show="functionType == 1">
      <div>
        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.setNo") }}</label>
          <input
            autocomplete="off"
            type="text"
            :value="loginContextDto.accountId"
            disabled="true"
            class="form-control"
          />
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.setName") }}</label>
          <input
            autocomplete="off"
            type="text"
            :value="loginContextDto.accountName"
            disabled="true"
            class="form-control"
          />
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.userClient") }}</label>
          <input
            autocomplete="off"
            type="text"
            :value="loginContextDto.clientName"
            disabled="true"
            class="form-control"
          />
        </div>

        <div
          v-if="assetConfigDto != null && assetConfigDto.id != null"
          class="form-group"
        >
          <label>{{
            $t("lang.assetBasicSetting.printPictureOfAssetCard")
          }}</label>
          <div class="input-group">
            <ImageEditor
              :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
              :record-id="assetConfigDto.id"
              :field-name="'imageName'"
              :readonly="isdisable"
              :image-name="imageName"
            />
          </div>
        </div>

        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.enableDepreciation"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />{{ $t("lang.assetBasicSetting.depreciationOfThisSet") }}
          </label>
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.companyPrefix") }}</label>
          <input
            v-model="assetConfigDto.clientPrefix"
            autocomplete="off"
            type="text"
            :disabled="isdisable"
            class="form-control"
          />
        </div>

        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.enableAssetCheck"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />{{
              $t("lang.assetBasicSetting.enableAssetAcceptanceAndWarehousing")
            }}
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.showUnrecognizedAssets"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />{{
              $t("lang.assetBasicSetting.assetCountShowsUnrecognizedAsset")
            }}
          </label>
        </div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.minimumTimeForAssetCollectionAndReturn")
          }}</label>
          <input
            v-model="assetConfigDto.assetInstanceStockInAndStockOutMinTime"
            autocomplete="off"
            type="number"
            :disabled="isdisable"
            class="form-control"
          />
        </div>
        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.allowOrganizationBatchChange"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />{{
              $t(
                "lang.assetBasicSetting.batchGenerationOfDepartmentChangeDocuments"
              )
            }}
          </label>
        </div>
      </div>
    </div>
    <div v-show="functionType == 2">
      <div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.mainDepreciationMethods")
          }}</label>
          <select
            v-model="assetConfigDto.depreciationMethod"
            class="form-control"
            :disabled="isdisable"
          >
            <option v-for="depreciationMethodDto in depreciationMethodDtos" :key="depreciationMethodDto.id">
              {{ depreciationMethodDto.text }}
            </option>
          </select>
        </div>

        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.depreciationSummaryAllocationPeriod")
          }}</label>
          <select
            v-model="assetConfigDto.depreciationDistributionPeriod"
            :disabled="isdisable"
            class="form-control"
          >
            <option>{{ $t("lang.assetBasicSetting.one") }}</option>
            <option>{{ $t("lang.assetBasicSetting.two") }}</option>
            <option>{{ $t("lang.assetBasicSetting.three") }}</option>
            <option>{{ $t("lang.assetBasicSetting.four") }}</option>
            <option>{{ $t("lang.assetBasicSetting.six") }}</option>
            <option>{{ $t("lang.assetBasicSetting.twelve") }}</option>
          </select>
        </div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.nextDepreciationAllocationMonth")
          }}</label>
          <Date
            v-model="assetConfigDto.nextDeprecitonDistributionDate"
            :readonly="isdisable"
          />
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.monthOfOpeningASet") }}</label>
          <Date
            v-model="assetConfigDto.depreciationStartDate"
            :readonly="isdisable"
          />
        </div>

        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.orginalValueChangeActiveCurrentMonth"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />
            {{
              $t(
                "lang.assetBasicSetting.changesInOriginalValueTakeEffectInTheCurrentPeriod"
              )
            }}
          </label>
        </div>

        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.netSalvageChangeActiveCurrentMonth"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />
            {{ $t("lang.assetBasicSetting.theAdjustment") }}
          </label>
        </div>

        <div class="checkbox">
          <label>
            <input
              v-model="
                assetConfigDto.accumulateDepreciationChangeActiveCurrentMonth
              "
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />
            {{ $t("lang.assetBasicSetting.depreciationEffectiveAdjustment") }}
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input
              v-model="assetConfigDto.reservedValueDepreciateInLastMonth"
              autocomplete="off"
              type="checkbox"
              :disabled="isdisable"
            />{{ $t("lang.assetBasicSetting.When") }}
          </label>
        </div>
      </div>
    </div>
    <div v-show="functionType == 3">
      <div>
        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.assetCategoryCodingMethod") }}({{
            $t("lang.assetBasicSetting.totalLength")
          }}:{{ getTotalNum(assetConfigDto.assetCategoryNoEncode) }})</label>
          <input
            v-model="assetConfigDto.assetCategoryNoEncode"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>

        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.assetNumberCodingMethod")
          }}</label>
          <div class="radio">
            <label>
              <input
                v-model="assetConfigDto.assetNoAutoEncode"
                autocomplete="off"
                type="radio"
                name="codeType"
                value="false"
                :checked="!assetConfigDto.assetNoAutoEncode"
                :disabled="isdisable"
              />{{ $t("lang.assetBasicSetting.manualEncoding") }}
            </label>
          </div>
          <div class="radio">
            <label>
              <input
                v-model="assetConfigDto.assetNoAutoEncode"
                autocomplete="off"
                type="radio"
                name="codeType"
                value="true"
                :checked="assetConfigDto.assetNoAutoEncode"
                :disabled="isdisable"
              />{{ $t("lang.assetBasicSetting.automaticCoding") }}
            </label>
          </div>
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.assetCodeCategory") }}</label>
          <select
            v-model="assetConfigDto.assetCodeType"
            class="form-control"
            :disabled="isdisable"
          >
            <option value="CATEGORYNO_SERIALNO">
              {{ $t("lang.assetBasicSetting.categoryNumberAndSerialNumber") }}
            </option>
            <option value="ORGNIZATIONNO_SERIALNO">
              {{ $t("lang.assetBasicSetting.departmentNumberAndSerialNumber") }}
            </option>
            <option value="CLIENTNO_MANUAL">
              {{ $t("lang.assetBasicSetting.companyNumberAndManualInput") }}
            </option>
          </select>
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.assetCodeLength") }}</label>
          <input
            v-model="assetConfigDto.assetNoLength"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.assetSerialNumber") }}</label>
          <input
            v-model="assetConfigDto.assetNoSerialNo"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>

        <div class="form-group">
          <label>{{ $t("lang.assetBasicSetting.assetGroupCodingMethod") }}({{
            $t("lang.assetBasicSetting.totalLength")
          }}:{{ getTotalNum(assetConfigDto.assetGroupNoEncode) }})</label>
          <input
            v-model="assetConfigDto.assetGroupNoEncode"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>

        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.assetCardNumberCodingMethod")
          }}</label>
          <div class="radio">
            <label>
              <input
                v-model="assetConfigDto.assetInstanceBarCodeAutoEncode"
                autocomplete="off"
                type="radio"
                name="assetInstanceBarCodeAutoEncode"
                value="false"
                :checked="!assetConfigDto.assetInstanceBarCodeAutoEncode"
                :disabled="isdisable"
              />{{ $t("lang.assetBasicSetting.manualEncoding") }}
            </label>
          </div>
          <div class="radio">
            <label>
              <input
                v-model="assetConfigDto.assetInstanceBarCodeAutoEncode"
                autocomplete="off"
                type="radio"
                name="assetInstanceBarCodeAutoEncode"
                value="true"
                :checked="assetConfigDto.assetInstanceBarCodeAutoEncode"
                :disabled="isdisable"
              />{{ $t("lang.assetBasicSetting.automaticCoding") }}
            </label>
          </div>
        </div>

        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.assetCardCodingMethod")
          }}</label>
          <select
            v-model="assetConfigDto.assetInstanceCodeType"
            class="form-control"
            :disabled="isdisable"
          >
            <option value="ASSET_NO_SERIALNO">
              {{ $t("lang.assetBasicSetting.assetNumberAndSequenceNumber") }}
            </option>
            <option value="CLIENT_ASSET_NO_SERIALNO">
              {{
                $t(
                  "lang.assetBasicSetting.companyPrefixAndAssetNumberAndSequenceNumber"
                )
              }}
            </option>
            <option value="CODE_DEFINITION">
              {{
                $t("lang.assetBasicSetting.calculateAccordingToCodeDefinition")
              }}
            </option>
          </select>
        </div>
      </div>
    </div>

    <div v-show="functionType == 4">
      <div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.u8DatabaseLinkString")
          }}(jdbc:sqlserver://ip:port;databaseName=db_name;user=name;password=password)</label>
          <input
            v-model="assetConfigDto.u8DbUrl"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.imageDatabaseLinkString")
          }}(jdbc:sqlserver://ip:port;databaseName=db_name;user=name;password=password)</label>
          <input
            v-model="assetConfigDto.imageJdbc"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>
        <div class="form-group">
          <label>{{
            $t("lang.assetBasicSetting.imageServerRootDirectory")
          }}</label>
          <input
            v-model="assetConfigDto.imageServerBaseUrl"
            autocomplete="off"
            type="text"
            class="form-control"
            :disabled="isdisable"
          />
        </div>
      </div>
    </div>

    <!-- 新增图片页签 -->
    <div v-show="functionType == 5">
      <div
        v-if="assetConfigDto != null && assetConfigDto.id != null"
        class="form-group"
      >
        <label>{{
          $t("lang.assetBasicSetting.printPictureOfAssetCard")
        }}</label>
        <div class="input-group">
          <ImageEditor
            :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
            :record-id="assetConfigDto.id"
            :field-name="'imageName'"
            :readonly="isdisable"
            :image-name="imageName"
          />
        </div>
      </div>
      <!-- 资产卡片打印图片2 -->
      <div
        v-if="assetConfigDto != null && assetConfigDto.id != null"
        class="form-group"
      >
        <label>{{
          $t("lang.assetBasicSetting.printPictureOfAssetCard2")
        }}</label>
        <div class="input-group">
          <ImageEditor
            :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
            :record-id="assetConfigDto.id"
            :field-name="'imageName2'"
            :readonly="isdisable"
            :image-name="imageName2"
          />
        </div>
      </div>
      <!-- 资产卡片打印图片3 -->
      <div
        v-if="assetConfigDto != null && assetConfigDto.id != null"
        class="form-group"
      >
        <label>{{
          $t("lang.assetBasicSetting.printPictureOfAssetCard3")
        }}</label>
        <div class="input-group">
          <ImageEditor
            :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
            :record-id="assetConfigDto.id"
            :field-name="'imageName3'"
            :readonly="isdisable"
            :image-name="imageName3"
          />
        </div>
      </div>
      <!-- 资产卡片打印图片4 -->
      <div
        v-if="assetConfigDto != null && assetConfigDto.id != null"
        class="form-group"
      >
        <label>{{
          $t("lang.assetBasicSetting.printPictureOfAssetCard4")
        }}</label>
        <div class="input-group">
          <ImageEditor
            :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
            :record-id="assetConfigDto.id"
            :field-name="'imageName4'"
            :readonly="isdisable"
            :image-name="imageName4"
          />
        </div>
      </div>
      <!-- 资产卡片打印图片5 -->
      <div
        v-if="assetConfigDto != null && assetConfigDto.id != null"
        class="form-group"
      >
        <label>{{
          $t("lang.assetBasicSetting.printPictureOfAssetCard5")
        }}</label>
        <div class="input-group">
          <ImageEditor
            :class-name="'com.leanwo.prodog.asset.model.AssetConfig'"
            :record-id="assetConfigDto.id"
            :field-name="'imageName5'"
            :readonly="isdisable"
            :image-name="imageName5"
          />
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col-md-12">
        <button class="btn btn-default" @click="changeIsdisabled()">
          {{
            isdisable
              ? $t("lang.assetBasicSetting.edit")
              : $t("lang.assetBasicSetting.cancelEdit")
          }}
        </button>
        <button
          :disabled="isdisable"
          class="btn btn-default"
          @click="saveAssetConfig()"
        >
          {{ $t("lang.assetBasicSetting.determine") }}
        </button>
        <button
          v-show="functionType == 1"
          class="btn btn-default"
          @click="regenerateThumbnails()"
        >
          {{ $t("lang.assetBasicSetting.regenerateThumbnail") }}
        </button>
        <button
          v-show="functionType == 1"
          class="btn btn-default"
          @click="generateAsset()"
        >
          {{ $t("lang.assetBasicSetting.generateAsset") }}
        </button>
      </div>
    </div>
    <Loading v-if="loading" />
  </div>
</template>
<script>
import Common from '../common/Common.js';





import ImageEditor from '../widget/ImageEditor.vue';

import LoginContextResource from '../api/base/LoginContextResource.js';
import AssetConfigResource from '../api/asset/AssetConfigResource.js';
import AssetInstanceResource from '../api/asset/AssetInstanceResource.js';
import DepreciationMethodResource from '../api/asset/DepreciationMethodResource.js';
import { Notify } from 'pc-component-v3';




export default {

  components: {
    
    
    
    
    ImageEditor,
  },
  data: function () {
    return {
      pageNo: '1',
      functionType: 1,
      loginContextDto: {}, //登录账套dto
      depreciationMethodDtos: [], //所有的折旧方法dto
      isdisable: true, //是否禁止更改
      CATEGORYNO_SERIALNO: '', //编码类型
      assetConfigDto: {}, //固定资产系统设置dto
      imageName: undefined,
      imageName2: undefined,
      imageName3: undefined,
      imageName4: undefined,
      imageName5: undefined,
      loading: false,
    };
  },

  mounted: function () {
    this.queryLoginContext();
    this.getDepreciationMethods();
    this.queryAssetConfig();
  },

  methods: {
    /**
     * @param  {选择页面事件}
     * @return {[type]}
     */
    selectPage: function (pageNo) {
      var _self = this;
      _self.pageNo = pageNo;
      _self.functionType = pageNo;
    },

    /**
     * 查找账套相关信息
     * @return {[type]} [description]
     */
    queryLoginContext: function () {
      var _self = this;
      LoginContextResource.queryLoginContext().then(
        successData => {
          if(successData.errorCode == 0) {
            _self.loginContextDto = successData.data;
          } else {
            Notify.error('提示',successData.errorMessage, false);
          }
        },
        errorData => {
          Common.processException(errorData);
        },
      );
      // $.ajax({
      //     url: Common.getApiURL('loginContextResource/queryLoginContext'),
      //     type: "get",
      //     dataType: "json",
      //     beforeSend: function (request) {
      //         Common.addTokenToRequest(request);
      //     },

      //     success: function (data) {
      //         _self.loginContextDto = data;
      //     },

      //     error: function (XMLHttpRequest, textStatus, errorThrown) {
      //         Common.processException(XMLHttpRequest, textStatus, errorThrown);
      //     }
      // });
    },

    /**
     * 查询AssetConfigDto
     * @return {[type]} [description]
     */
    queryAssetConfig: function () {
      var _self = this;
      AssetConfigResource.queryAssetConfig().then(
        successData => {
          if(successData.errorCode == 0) {
            _self.assetConfigDto = successData.data;
          } else {
            Notify.error('提示',successData.errorMessage, false);
          }
        },
        errorData => {
          Common.processException(errorData);
        },
      );
      //   $.ajax({
      //     url: Common.getApiURL("assetConfigResource/queryAssetConfig"),
      //     type: "get",
      //     dataType: "json",
      //     beforeSend: function (request) {
      //       Common.addTokenToRequest(request);
      //     },

      //     success: function (data) {
      //       _self.assetConfigDto = data;
      //     },
      //     error: function (XMLHttpRequest, textStatus, errorThrown) {
      //       Common.processException(XMLHttpRequest, textStatus, errorThrown);
      //     },
      //   });
    },

    /**
     * 查找所有的折旧方法名称
     * @return {[type]} [description]
     */
    getDepreciationMethods: function () {
      var _self = this;
      DepreciationMethodResource.getDepreciationMethods().then(
        successData => {
          _self.depreciationMethodDtos = successData.datas;
        },
        errorData => {
          Common.processException(errorData);
        },
      );
      // $.ajax({
      //     url: Common.getApiURL(
      //     "depreciationMethodResource/getDepreciationMethods"
      //     ),
      //     type: "get",
      //     dataType: "json",
      //     beforeSend: function (request) {
      //     Common.addTokenToRequest(request);
      //     },

      //     success: function (data) {
      //     _self.depreciationMethodDtos = data;
      //     },
      //     error: function (XMLHttpRequest, textStatus, errorThrown) {
      //     Common.processException(XMLHttpRequest, textStatus, errorThrown);
      //     },
      // });
    },

    /**
     * 点击保存资产设置事件
     * @return {[type]} [description]
     */
    saveAssetConfig: function () {
      var _self = this;
      AssetConfigResource.saveAssetConfig(_self.assetConfigDto).then(
        successData => {
          if(successData.errorCode != 0) {
            Notify.error('提示','更新资产配置失败。', false);
            return;
          }
          Notify.success('提示', '更新资产配置成功!', 1500);
        },
        errorData => {
          Common.processException(errorData);
        },
      );
      //   $.ajax({
      //     url: Common.getApiURL("assetConfigResource/saveAssetConfig"),
      //     type: "post",
      //     contentType: "application/json",
      //     data: JSON.stringify(_self.assetConfigDto),
      //     beforeSend: function (request) {
      //       Common.addTokenToRequest(request);
      //     },

      //     success: function (data) {
      //       Notify.success("提示", "更新资产配置成功!", 1500);
      //     },
      //     error: function (XMLHttpRequest, textStatus, errorThrown) {
      //       Common.processException(XMLHttpRequest, textStatus, errorThrown);
      //     },
      //   });
    },

    /**
     * 点击重新生成缩略图
     *
     */
    regenerateThumbnails: function () {
      var _self = this;
      _self.loading=true;
      AssetConfigResource.regenerateThumbnails(_self.assetConfigDto).then(
        successData => {
          if(successData.errorCode != 0) {
            Notify.error('提示','重新更新缩略图失败。', false);
            return;
          }
          Notify.success('提示', '重新生成缩略图成功。', false);
          _self.loading=false;
        },
        errorData => {
          Common.processException(errorData);
          _self.loading=false;
        },
      );
    },

    /**
     * 生成资产
     */
    generateAsset: function () {
      var _self = this;
      _self.loading=true;
      AssetInstanceResource.generateAssetByAssetInstance().then(
        successData => {
          if(successData.errorCode == 0) {
            Notify.success('提示', '共计生成' + successData.data + '个资产', false);
            _self.loading=false;
          } else {
            Notify.error('提示',successData.errorMessage, false);
          }
        },
        errorData => {
          Common.processException(errorData);
          _self.loading=false;
        },
      );
    },

    changeIsdisabled: function () {
      this.isdisable = !this.isdisable;
    },
    
    getTotalNum: function (str) {
      if (str == undefined || str == '') {
        return;
      }
      var arr = str.split('-');
      var len = arr.length;
      if (len == 0) {
        return;
      }
      var totalNum = 0;
      for (var i = 0; i < len; i++) {
        totalNum += Number(arr[i]);
      }
      return totalNum;
    },
  },
};
</script>
<style scoped>
</style>