<template>
  <div class="app-container" style="width: 90%;">
    <div class="el-icon-info" style="font-size: 30px;float: right;color: #009C69;" @click="handleDescription" />
    <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="是否可取前一天计划：" 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="是否可取后一天计划：" 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="班次数量：" 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="重量小数点位数：" prop="decimalPlaces">
            <el-select v-model="createTemp.decimalPlaces" :disabled="isRoleEdit==false" filterable placeholder="重量小数点位数" class="filter-item" style="width:100%">
              <el-option v-for="item in weightDecimalPlacesList" :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="decimalRate">
            <el-select v-model="createTemp.decimalRate" :disabled="isRoleEdit==false" filterable placeholder="比例小数点位数" class="filter-item" style="width:100%">
              <el-option v-for="item in rateDecimalPlacesList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用饲料合同：" 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="启用饲料锁定牛头数：" 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-col :span="12">
          <el-form-item label="车辆载重超出重量提醒比例（%）：" prop="remainOptRate">
            <el-input ref="remainOptRate" v-model="createTemp.remainOptRate" :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="车辆载重超出重量禁止比例（%）：" prop="remainOptRate">
            <el-input ref="remainOptRate" v-model="createTemp.remainOptRate" :disabled="isRoleEdit==false" class="filter-item" placeholder="剩料分配占比原混料" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用补料：" 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-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否启用剩料：" 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-col :span="12">
          <el-form-item label="剩料处理选项：" 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-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="剩料分配方式：" 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="剩料分配占比原混料" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="小料是否称量：" prop="isSmallMaterial">
            <el-select v-model="createTemp.isSmallMaterial" :disabled="isRoleEdit==false" filterable placeholder="小料是否称量" class="filter-item" style="width:100%">
              <el-option v-for="item in smallMaterialList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用数据同步:" prop="isDataSync">
            <el-select v-model="createTemp.isDataSync" :disabled="isRoleEdit==false" filterable placeholder="是否启用数据同步" class="filter-item" style="width:100%">
              <el-option v-for="item in dataSynchronizationList" :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="remainOptRate">
            <el-input ref="remainOptRate" v-model="createTemp.remainOptRate" :disabled="isRoleEdit==false" class="filter-item" placeholder="剩料分配占比原混料" type="text" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="任意车数量:" prop="anyCar">
            <el-input ref="anyCar" v-model="createTemp.anyCar" :disabled="isRoleEdit==false" class="filter-item" placeholder="任意车数量" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="混料准确率:" prop="accuracy">
              <el-select v-model="createTemp.accuracy" :disabled="isRoleEdit==false" filterable placeholder="混料准确率" class="filter-item" style="width:100%">
                <el-option v-for="item in accuracyList" :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="remainOptRate">
            <el-input ref="remainOptRate" v-model="createTemp.remainOptRate" :disabled="isRoleEdit==false" class="filter-item" placeholder="剩料分配占比原混料" type="text" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="tmr容量上限(%):" prop="tmrUpper">
            <el-input ref="tmrUpper" v-model="createTemp.tmrUpper" :disabled="isRoleEdit==false" class="filter-item" placeholder="tmr容量上限(%)" type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="tmr容量下限(%):" prop="tmrUnder">
              <el-input ref="tmrUnder" v-model="createTemp.tmrUnder" :disabled="isRoleEdit==false" class="filter-item" placeholder="tmr容量下限(%)" type="number" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="bottom: 10px;">
      <el-button v-if="isRoleEdit" class="save" :disabled="isokDisable" @click="handleSave">保存</el-button>
      <el-button v-if="isRoleEdit" class="warning" :disabled="isokDisable" @click="handleRecovery">恢复默认</el-button>
    </div>

    <el-dialog :title="textMap[description.dialogStatus]" :destroy-on-close="true" :visible.sync="description.dialogFormVisible" :close-on-click-modal="false" width="70%">
      <div class="app-description dialogMinHeight">
        <div class="content">
          1、是否可取前一天计划：若选择是，代表在当天过了凌晨12点仍可以取到昨天的料。
          <br>
          2、是否可取后一天计划：若当天做完全部的料需要预备第二天的料的时候可以选择是。
          <br>
          3、班次数量：控制栏舍配方，计划等页面班次数量。
          <br>
          4、重量小数点位数：控制栏舍配方、撒料计划等页面重量小数位数。
          <br>
          5、是否启用饲料合同：若选择是，则开启库存管理中饲料合同页面。
          <br>
          6、启用饲料锁定牛头数：开启后则配方模板中饲料表显示此字段。
          <br>
          7、车辆载重超出重量提醒比例：撒料计划中车次分配栏舍重量，当分配的合计重量超出车辆最大重量*此比例时，进行提示。限制输入正整数。
          <br>
          8、车辆载重超出重量禁止比例：撒料计划中车次分配栏舍重量，当分配的合计重量超出车辆最大重量*此比例时，禁止分配。限制输入正整数。
          <br>
          9、是否启用补料：若选择是，则栏舍配方中启用此字段。
          <br>
          10、是否启用剩料：若选择否则剩料计划不可用，剩料量不影响原计划。
          <br>
          11、剩料处理选项：①若选择转为饲料，按饲料配方整体替换扣减；②如果选择替换某饲料，则此剩料替换待饲喂中的对应饲料量，扣完为止；③不处理：不做剩料计算处理。
          <br>
          12、剩料分配方式：①顶额分配：按栏舍原撒料量的百分比进行计算，剩料分配为此值，依次计算分配满栏舍，若剩料量未分配完，则剩余剩料不计入内；
          <br>
          ②平均分配，按收集的总剩料量平均分配值，若分配值超出栏舍原料量的百分比，剩余值累加到未分配满的栏舍中，最终剩余剩料不计入内。
          <br>
          13、剩料分配占比原料：剩料应分配量不超出此比例计算出的值，与剩料分配方式关联使用。限制输入正整数。
          <br>
          14、小料是否称量：若选择是，则饲料表中饲料可选小料称，撒料计划中可设置是否提前称量。
          <br>
          15、是否启用数据同步：启用后日执行页面开启数据同步按钮，用于与第三方系统数据对接。
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelClose" @click="description.dialogFormVisible = false; ">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName, 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: '是' }, { id: '0', name: '否' }], // 是否可取前一天计划
      planNextDayList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 是否可取后一天计划
      isSupplementList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 是否启用补料
      isLeftoverMaterialList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 是否启用剩料
      decimalPlacesList: [{ id: '0', name: '整数' }, { id: '1', name: '一位小数' }, { id: '2', name: '两位小数' }, { id: '3', name: '三位小数' }], // 日计划报表小数点位数
      isLockBullsList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 启用饲料锁定牛头数
      disposalList: [], // 剩料处理选项
      numberList: [{ id: '0', name: '1' }, { id: '1', name: '2' }, { id: '2', name: '3' }, { id: '3', name: '4' }], // 班次数量
      feedontractList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 是否启用饲料合同
      leftoverMaterialList: [{ id: '0', name: '顶额分配' }, { id: '1', name: '剩料分配' }],
      weightDecimalPlacesList: [{ id: '0', name: '整数' }, { id: '1', name: '一位小数' }, { id: '2', name: '两位小数' }, { id: '3', name: '三位小数' }, { id: '4', name: '四位小数' }], // 重量小数点位数
      rateDecimalPlacesList:  [{ id: '0', name: '整数' }, { id: '1', name: '一位小数' }, { id: '2', name: '两位小数' }],//比例小数点位数
      smallMaterialList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 小料是否称量
      dataSynchronizationList: [{ id: '1', name: '是' }, { id: '0', name: '否' }], // 是否启用数据同步
      accuracyList: [{ id: '1', name: '排除允许误差准确率' }, { id: '0', 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: {},
      description: {
        dialogFormVisible: false,
        dialogStatus: ''
      },
      textMap: {
        description: '参数说明'
      },
      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)
      if(this.createTemp.anyCar == '' || this.createTemp.anyCar == 0){
        this.$message({ type: 'error', message: '任意车数量不能为空或为0', duration: 2000 })
      }
      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,
        decimalPlaces: this.createTemp.decimalPlaces,
        decimalRate: this.createTemp.decimalRate,
        isEnableContract: this.createTemp.isEnableContract,
        remainOptDis: this.createTemp.remainOptDis,
        remainOptRate: this.createTemp.remainOptRate,
        isSmallMaterial: this.createTemp.isSmallMaterial,
        isDataSync: this.createTemp.isDataSync,
        anyCar:this.createTemp.anyCar,
        accuracy:this.createTemp.accuracy,
        tmrUpper:this.createTemp.tmrUpper,
        tmrUnder:this.createTemp.tmrUnder
      }}
      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: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
        }
      })
    },
    handleRecovery() {
      console.log('点击了恢复默认')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      MessageBox.confirm('是否确认恢复默认设置?', {
        confirmButtonText: '确认', cancelButtonText: '取消', 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: '成功', message: '保存成功', type: 'success', duration: 2000 })
            this.getList()
          } else {
            this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
          }
        })
      })
    },
    handleDescription() {
      this.description.dialogStatus = 'description'
      this.description.dialogFormVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
  .app-description{
    display:flex; justify-content: center; align-items: center;font-size: 14px;line-height: 28px;
    .content{
      display:flex;
    }
  }
</style>
