<template>
  <div class="app-container">
    <div class="operation">
      <el-button class="success" style="float: left;" @click="handleNutritional">营养评估</el-button>
      <el-button class="import" style="float: right;">导入</el-button>
      <el-dropdown style="float: right;margin-right: 10px;" @click="handleExport">
        <el-button class="export">导出</el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="handleExportTemp">导出模板</el-dropdown-item>
          <el-dropdown-item @click.native="handleExport">导出数据</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-button class="export" style="float: right;margin-right: 10px;" @click="handleHistoryRecords">历史记录</el-button>
    </div>
    <div class="search" />
    <div class="table">
      <el-table
        :key="table.tableKey"
        v-loading="table.listLoading"
        element-loading-text="给我一点时间"
        :data="table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
        @header-click="headerClick"
        @selection-change="handleSelect"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (table.pageNum-1) * table.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="栏舍名称" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="软件牛头数" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实际牛头数" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.brandName" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="系数(%)" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.brandName" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="配方模板" min-width="110px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-select v-if="scope.row.Edit" v-model="scope.row.brandName" placeholder="配方模板" class="filter-item" style="width:80%;padding:10px 0;">
              <el-option v-for="item in recipeTemplateList" :key="item.id" :label="item.name" :value="item.name" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="早班比例(%)" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.brandName" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="早班重量" min-width="130px" align="center" :render-header="renderHeader">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.eqName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.eqName" :disabled="scope.row.isMorningWeight" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="中班比例(%)" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.brandName" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="中班重量" min-width="130px" align="center" :render-header="renderHeader">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.eqName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.eqName" :disabled="scope.row.isMiddleWeight" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="晚班比例(%)" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.brandName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.brandName" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="晚班重量" min-width="130px" align="center" :render-header="renderHeader">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.eqName }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.eqName" :disabled="scope.row.isNightWeight" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" style="width:80%;padding:10px 0;" maxlength="32" />
          </template>
        </el-table-column>
        <el-table-column label="总比例(%)" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="投喂量" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="配方重量" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="补料重量" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <!-- 编辑 -->
            <el-button v-if="row.isUpdate" class="miniSuccess" @click="handleUpdate(row)">编辑</el-button>
            <el-button v-if="row.isUpdate" class="miniDanger" @click="handleRowDelete(row)">删除</el-button>
            <!-- 编辑保存 -->
            <el-button v-if="row.isUpdateSave" class="miniSuccess" @click="updateData(row)">保存</el-button>
            <el-button v-if="row.isUpdateSave" class="minCancel" @click="updateCancel(row)">取消</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="table.total>0" :total="table.total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="getList" />
    </div>

    <!-- 营养评估 -->
    <el-dialog :title="textMap[nutritional.dialogStatus]" :destroy-on-close="true" :visible.sync="nutritional.dialogFormVisible" :close-on-click-modal="false" width="90%">
      <div class="app-nutritional">
        <h4 style="position: absolute;top:-5px;left:140px;font:18px/24px '' ;color:#303133;">栏舍:</h4>
        <el-form ref="nutritional.temp" :rules="nutritional.rules" :model="nutritional.temp" label-position="right" label-width="95px" style="width: 100%;margin-bottom:30px">
          <el-row>
            <el-col :span="4">
              <el-form-item label="类别" prop="eqClassName">
                <el-select v-model="nutritional.temp.eqClassName" placeholder="" class="filter-item" style="width:100%;">
                  <el-option v-for="item in livestockTypeList" :key="item.id" :label="item.name" :value="item.name" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="月龄" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="体重(kg)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="日增重(kg)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="胎次(次)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="泌乳天数(天)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4">
              <el-form-item label="怀孕天数(天)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="产奶量(kg)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="乳脂率(%)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="乳蛋白率(%)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="乳糖率(%)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="温度(℃)" prop="eqClassName">
                <el-input ref="eqClassName" v-model="nutritional.temp.eqClassName" class="filter-item" placeholder="" style="width:100%;" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div style="position: relative;height: 50px;">
          <el-button class="success" :disabled="isokDisable" style="position: absolute;left: 0;right: 0;margin:0 auto;" @click="assessmentData()">评估</el-button>
        </div>
        <div class="table">
          <el-table
            :key="nutritional.tableKey"
            v-loading="nutritional.listLoading"
            element-loading-text="给我一点时间"
            :data="nutritional.list"
            border
            fit
            highlight-current-row
            style="width: 100%;margin-bottom:60px;"
            :row-style="rowStyle"
            :cell-style="cellStyle"
            class="elTable table-fixed"
          >
            <el-table-column type="selection" width="50" />
            <el-table-column label="序号" align="center" type="index" width="50px">
              <template slot-scope="scope">
                <span>{{ scope.$index + (nutritional.pageNum-1) * nutritional.pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column label="指标" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="奶牛需要" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="饲料提供" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实际执行" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="相差" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="粗料提供" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="精料提供" min-width="130px" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancel" @click="nutritional.dialogFormVisible = false; ">关闭</el-button>
        </div>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { GetDataByName } from '@/api/common'
import Pagination from '@/components/Pagination'
export default {
  name: 'DhedFormula',
  components: { Pagination },
  data() {
    return {
      recipeTemplateList: [{ id: 0, name: '泌乳牛高产' }, { id: 1, name: '高产日粮' }], // 配方模板
      livestockTypeList: [{ id: 0, name: '高产' }, { id: 1, name: '低产' }, { id: 2, name: '中产' }, { id: 3, name: '青年后备' }, { id: 4, name: '围产' }], // 类别

      getdataListParm: {
        name: 'getAssetList',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          enable: ''
        }
      },
      table: {
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true
      },
      selectList: [],
      nutritional: {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: {},
        rules: {},
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        getdataListParm: {
          name: 'getAssetList',
          page: 1,
          offset: 1,
          pagecount: 10,
          returntype: 'Map',
          parammaps: {
            enable: ''
          }
        }
      },
      textMap: {
        Nutritional: '营养评估'
      },

      isokDisable: false,
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },

  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.table.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            this.$set(response.data.list[i], 'Edit', false) // 编辑
            this.$set(response.data.list[i], 'NoEdit', true) // 不可编辑/输入
            this.$set(response.data.list[i], 'isUpdate', true) // 编辑操作
            this.$set(response.data.list[i], 'isUpdateSave', false) // 编辑保存
          }
          this.table.list = response.data.list
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
          this.table.total = response.data.total
        } else {
          this.table.list = []
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },

    renderHeader(h, { column, $index }) { // h即为cerateElement的简写,具体可看vue官方文档
      console.log(column)
      return h(
        'div',
        [
          h('span', column.label),
          h('i', { class: 'el-icon-unlock', ref: 'el-icon-unlock', style: 'margin-left:5px;' })
        ],
      )
    },

    headerClick(column, event) {
      console.log(column, event)
      if (column.label == '早班重量') {
        for (let i = 0; i < this.table.list.length; i++) {
          if (this.table.list[i].Edit == true) {
            if (event.target.outerHTML == '<i class="el-icon-unlock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-lock'
              this.$set(this.table.list[i], 'isMorningWeight', true) // 锁住不可编辑
            } else if (event.target.outerHTML == '<i class="el-icon-lock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-unlock'
              this.$set(this.table.list[i], 'isMorningWeight', false) // 解锁可编辑
            }
          }
        }
      } else if (column.label == '中班重量') {
        for (let i = 0; i < this.table.list.length; i++) {
          if (this.table.list[i].Edit == true) {
            if (event.target.outerHTML == '<i class="el-icon-unlock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-lock'
              this.$set(this.table.list[i], 'isMiddleWeight', true) // 锁住不可编辑
            } else if (event.target.outerHTML == '<i class="el-icon-lock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-unlock'
              this.$set(this.table.list[i], 'isMiddleWeight', false) // 解锁可编辑
            }
          }
        }
      } else if (column.label == '晚班重量') {
        for (let i = 0; i < this.table.list.length; i++) {
          if (this.table.list[i].Edit == true) {
            if (event.target.outerHTML == '<i class="el-icon-unlock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-lock'
              this.$set(this.table.list[i], 'isNightWeight', true) // 锁住不可编辑
            } else if (event.target.outerHTML == '<i class="el-icon-lock" style="margin-left: 5px;"></i>') {
              event.target.className = 'el-icon-unlock'
              this.$set(this.table.list[i], 'isNightWeight', false) // 解锁可编辑
            }
          }
        }
      }
    },

    handleUpdate(row) {
      for (let i = 0; i < this.table.list.length; i++) {
        if (this.table.list[i].Edit == true) {
          this.$message({ type: 'error', message: '当前内容未保存,请点击取消或保存继续进行操作', duration: 2000 })
          return false
        }
      }
      // 编辑true,不可编辑false
      row.Edit = true
      row.NoEdit = false
      // 编辑false,编辑保存true
      row.isUpdate = false
      row.isUpdateSave = true
    },
    updateData(row) {
      console.log('点击了编辑保存', row)
    },
    updateCancel(row) {
      console.log('点击了编辑取消')
      // 编辑false,不可编辑true
      row.Edit = false
      row.NoEdit = true
      // 编辑true,编辑保存false
      row.isUpdate = true
      row.isUpdateSave = false
    },
    handleRowDelete(row) {
      console.log('点击了行内删除')
    },

    handleSelect(val) {
      console.log('勾选数据', val)
      this.selectList = val
    },
    // 营养评估
    handleNutritional() {
      console.log('点击了营养评估')
      if (this.selectList.length !== 1) {
        this.$message({ type: 'error', message: '请选择一条栏舍信息进行营养评估', duration: 2000 })
        return false
      } else {
        this.temp = this.selectList
        this.nutritional.dialogStatus = 'Nutritional'
        this.nutritional.dialogFormVisible = true
        this.getList2()
      }
    },
    assessmentData() {
      console.log('评估保存')
      this.getList2()
    },
    getList2() {
      this.nutritional.listLoading = true
      GetDataByName(this.nutritional.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          this.nutritional.list = response.data.list
          this.nutritional.pageNum = response.data.pageNum
          this.nutritional.pageSize = response.data.pageSize
          this.nutritional.total = response.data.total
        } else {
          this.nutritional.list = []
        }
        setTimeout(() => {
          this.nutritional.listLoading = false
        }, 100)
      })
    },
    // 历史记录
    handleHistoryRecords() {
      console.log('点击了历史记录')
    },
    // 导出模板
    handleExportTemp() {
      console.log('点击了导出模板')
    },
    // 导出
    handleExport() {
      console.log('点击了导出数据')
    },

    // 导入
    handleImport() {
      console.log('点击了导入')
    }
  }
}
</script>
<style lang="scss" scoped>
  .search{clear: both;}
  .table{margin-top:10px;}
 </style>