<template>
  <div class="app-container">
    <el-form
      ref="createTemp"
      :rules="rules"
      :model="createTemp"
      label-position="right"
      label-width="250px"
      style="margin-top: 40px; margin-bottom: 30px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.sfkqqytjh')"
            prop="isGetLastPlan"
          >
            <el-select
              v-model="createTemp.isGetLastPlan"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="是否可取前一天计划"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in planBeforeDayList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.sfkqqytjh1')"
            prop="isGetNextPlan"
          >
            <el-select
              v-model="createTemp.isGetNextPlan"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="是否可取后一天计划"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in planNextDayList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.sfqybl')"
            prop="isEnableSupplyFeed"
          >
            <el-select
              v-model="createTemp.isEnableSupplyFeed"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="是否启用补料"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in isSupplementList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.sfqysl')"
            prop="isEnableRemainFeed"
          >
            <el-select
              v-model="createTemp.isEnableRemainFeed"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="是否启用剩料"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in isLeftoverMaterialList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="日计划报表小数点位数：" prop="reportDigit">
            <el-select
              v-model="createTemp.reportDigit"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="日计划报表小数点位数"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in decimalPlacesList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.qyslsdnts')"
            prop="isLockCount"
          >
            <el-select
              v-model="createTemp.isLockCount"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="启用饲料锁定牛头数"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in isLockBullsList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('defaultParameter.slclxx')" prop="remainOpt">
            <el-select
              v-model="createTemp.remainOpt"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="剩料处理选项"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in disposalList"
                :key="item.id"
                :label="item.fname"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水分调节选项：" prop="waterOpt">
            <el-select
              v-model="createTemp.waterOpt"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="水分调节选项"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in waterConditioningList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="默认撒料超出重量提醒比例："
            prop="overweightWarnRate"
          >
            <el-input
              ref="overweightWarnRate"
              v-model="createTemp.overweightWarnRate"
              :disabled="isRoleEdit == false"
              class="filter-item"
              placeholder="请输入超出比例"
              type="text"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="默认撒料超出重量禁止比例："
            prop="overweightBanRate"
          >
            <el-input
              ref="overweightBanRate"
              v-model="createTemp.overweightBanRate"
              :disabled="isRoleEdit == false"
              class="filter-item"
              placeholder="请输入禁止比例"
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.kcyjts')"
            prop="repertoryWarn"
          >
            <el-input
              ref="repertoryWarn"
              v-model="createTemp.repertoryWarn"
              :disabled="isRoleEdit == false"
              class="filter-item"
              placeholder="请输入库存预警天数"
              type="text"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用库存管理：" prop="isfeedstorage">
            <el-select
              v-model="createTemp.isfeedstorage"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="水分调节选项"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in priceCalculationList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('defaultParameter.bcsl')" prop="times">
            <el-select
              v-model="createTemp.times"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="水分调节选项"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in numberList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.sfqyslht')"
            prop="isEnableContract"
          >
            <el-select
              v-model="createTemp.isEnableContract"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="启用饲料合同"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in feedontractList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="$t('defaultParameter.slfbfs')"
            prop="remainOptDis"
          >
            <el-select
              v-model="createTemp.remainOptDis"
              :disabled="isRoleEdit == false"
              filterable
              placeholder="剩料分配方式"
              class="filter-item"
              style="width: 100%"
            >
              <el-option
                v-for="item in leftoverMaterialList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="剩料分配占比原混料(%)：" prop="remainOptRate">
            <el-input
              ref="remainOptRate"
              v-model="createTemp.remainOptRate"
              :disabled="isRoleEdit == false"
              class="filter-item"
              :placeholder="$t('defaultParameter.slfpzb')"
              type="text"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="bottom: 0">
      <el-button
        v-if="isRoleEdit"
        class="success"
        :disabled="isokDisable"
        @click="handleSave"
        >{{ $t('formulationEvaluation.save') }}</el-button
      >
      <el-button
        v-if="isRoleEdit"
        class="warning"
        :disabled="isokDisable"
        @click="handleRecovery"
      >
        {{ $t('defaultParameter.hfmr') }}</el-button
      >
    </div>
  </div>
</template>

<script>
import {
  GetDataByName,
  PostDataByName,
  GetDataByNames,
  failproccess,
  checkButtons,
  ExecDataByConfig
} from '@/api/common'
import Cookies from 'js-cookie'
import { MessageBox } from 'element-ui'
export default {
  name: 'DefaultParameter',
  data() {
    return {
      isRoleEdit: [],
      planBeforeDayList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否可取前一天计划
      planNextDayList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否可取后一天计划
      isSupplementList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否启用补料
      isLeftoverMaterialList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否启用剩料
      decimalPlacesList: [
        { id: '0', name: this.$t('supplier.zs') },
        { id: '1', name: this.$t('supplier.xs1') },
        { id: '2', name: this.$t('supplier.xs2') },
        { id: '3', name: this.$t('supplier.xs3') }
      ], // 日计划报表小数点位数
      isLockBullsList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 启用饲料锁定牛头数
      disposalList: [], // 剩料处理选项
      waterConditioningList: [
        { id: '0', name: this.$t('defaultParameter.qz') },
        { id: '1', name: this.$t('defaultParameter.bcl') },
        { id: '2', name: this.$t('defaultParameter.bcl') }
      ], // 水分调节选项
      priceCalculationList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否启用库存管理
      numberList: [
        { id: '0', name: '1' },
        { id: '1', name: '2' },
        { id: '2', name: '3' },
        { id: '3', name: '4' }
      ], // 班次数量
      feedontractList: [
        { id: '1', name: this.$t('supplier.yes') },
        { id: '0', name: this.$t('supplier.no') }
      ], // 是否启用饲料合同
      leftoverMaterialList: [
        { id: '0', name: this.$t('supplier.defb') },
        { id: '1', name: '平均分配' }
      ],
      requestParams: [
        {
          name: 'getRemainFeedList',
          offset: 0,
          pagecount: 0,
          parammaps: { pastureid: Cookies.get('pastureid') }
        }
      ],
      getdataListParm: {
        name: 'getysoptList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          pastureid: Cookies.get('pastureid')
        }
      },
      tableKey: 0,
      listLoading: true,
      createTemp: {},
      rules: {},
      isokDisable: false,
      requestParam: {}
    }
  },
  created() {
    this.getList()
    this.getButtons()
    this.getDownList()
  },

  methods: {
    getButtons() {
      const Edit = 'DefaultParameter'
      const isRoleEdit = checkButtons(
        JSON.parse(sessionStorage.getItem('buttons')),
        Edit
      )
      this.isRoleEdit = isRoleEdit
    },
    getDownList() {
      GetDataByNames(this.requestParams).then((response) => {
        this.disposalList = response.data.getRemainFeedList.list
      })
    },
    getList() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then((response) => {
        console.log('table1数据', response.data.list)
        if (response.data.list !== null) {
          this.createTemp = response.data.list[0]
        } else {
          this.list = []
        }
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    handleSave() {
      console.log('点击了保存')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam.common = { returnmap: '0' }
      this.requestParam.data = []
      this.requestParam.data[0] = {
        name: 'updatesysopt',
        type: 'e',
        parammaps: {
          pastureid: this.createTemp.pastureid,
          isGetLastPlan: this.createTemp.isGetLastPlan,
          isGetNextPlan: this.createTemp.isGetNextPlan,
          isEnableSupplyFeed: this.createTemp.isEnableSupplyFeed,
          isEnableRemainFeed: this.createTemp.isEnableRemainFeed,
          reportDigit: this.createTemp.reportDigit,
          isLockCount: this.createTemp.isLockCount,
          remainOpt: this.createTemp.remainOpt,
          waterOpt: this.createTemp.waterOpt,
          overweightWarnRate: this.createTemp.overweightWarnRate,
          overweightBanRate: this.createTemp.overweightBanRate,
          repertoryWarn: this.createTemp.repertoryWarn,
          isfeedstorage: this.createTemp.isfeedstorage,
          times: this.createTemp.times,
          isEnableContract: this.createTemp.isEnableContract,
          remainOptDis: this.createTemp.remainOptDis,
          remainOptRate: this.createTemp.remainOptRate
        }
      }
      this.requestParam.data[1] = {
        name: 'updateFPbyFTChangeALL',
        type: 'e',
        parammaps: {
          pastureid: this.createTemp.pastureid
        }
      }

      ExecDataByConfig(this.requestParam).then((response) => {
        console.log('保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.$notify({
            title: this.$t('common.succes'),
            message: this.$t('driver.saveSuccess'),
            type: 'success',
            duration: 2000
          })
          this.getList()
        } else {
          this.$notify({
            title: this.$t('driver.saveFail'),
            message: response.data,
            type: 'warning',
            duration: 2000
          })
        }
      })
    },
    handleRecovery() {
      console.log('点击了恢复默认')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      MessageBox.confirm(this.$t('message.msg344'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        this.requestParam.common = { returnmap: '0' }
        this.requestParam.data = []
        this.requestParam.data[0] = {
          name: 'updatesysoptinit',
          type: 'e',
          parammaps: {
            pastureid: this.createTemp.pastureid
          }
        }
        this.requestParam.data[1] = {
          name: 'updateFPbyFTChangeALL',
          type: 'e',
          parammaps: {
            pastureid: this.createTemp.pastureid
          }
        }

        ExecDataByConfig(this.requestParam).then((response) => {
          console.log('保存发送参数', this.requestParam)
          if (response.msg !== 'fail') {
            this.$notify({
              title: this.$t('common.succes'),
              message: this.$t('driver.saveSuccess'),
              type: 'success',
              duration: 2000
            })
            this.getList()
          } else {
            this.$notify({
              title: this.$t('driver.saveFail'),
              message: response.data,
              type: 'warning',
              duration: 2000
            })
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
