<template>
  <div class="app-container">
    <div class="search">
      <el-input v-model="getdataListParm.parammaps.content" placeholder="请输入需求内容搜索" style="width: 300px;" class="filter-item" clearable />
      <el-select v-model="getdataListParm.parammaps.cowTypeId" placeholder="畜牧类别" class="filter-item" style="width: 120px;" clearable>
        <el-option v-for="item in animalType" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-date-picker v-model="getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" />
      <el-button v-waves class="successBorder" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button   class="success" icon="el-icon-plus" @click="handleCreate">添加</el-button>

    </div>
    <div class="table" style="margin-top:10px">
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        element-loading-text="给我一点时间"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
      >
        <el-table-column label="序号" min-width="100px" align="center" prop="sort" />
        <el-table-column label="畜牧类别" min-width="100px" align="center" prop="cowTypeName" />
        <el-table-column label="需求内容" min-width="100px" align="center" prop="content" />
        <el-table-column label="排序" min-width="100px" align="center" prop="ordinal" />
        <el-table-column label="添加人" min-width="100px" align="center" prop="createName" />
        <el-table-column label="添加时间" min-width="100px" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button class="miniSuccess" size="mini" @click="handleUpdate(row)">编辑</el-button>
            <el-button class="miniDanger" size="mini"  @click="handleRowDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>=0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="getList()" />
    </div>

    <el-dialog :title="textMap[edit.dialogStatus]" :visible.sync="edit.dialogFormVisible" :close-on-click-modal="false"
      width="40%">
      <div class="edit">
        <div class="content">
          <el-form ref="editTemp" :rules="rules" :model="edit.temp" label-position="right" label-width="115px" style="width: 90%;margin:0 auto 30px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="畜牧类别:" prop="cowTypeId">
                 <el-select v-model="edit.temp.cowTypeId" placeholder="畜牧类别" style="width: 100%;">
                   <el-option v-for="item in animalType" :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="content">
                  <el-input ref="content" maxlength="120" show-word-limit v-model="edit.temp.content" type="text" placeholder="请输入需求内容" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="排序" prop="ordinal">
                  <el-input ref="ordinal" v-model="edit.temp.ordinal" type="number" placeholder="排序" />
                  <span>数字越大显示越靠前，用户端同步</span>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer" style="right:30px;position:absolute;bottom: 40px;">
          <el-button class="success" :disabled="isokDisable" type="primary" @click="edit.dialogStatus == 'add' ? createData():updateData()"> 确认 </el-button>
          <el-button class="cancelClose" @click="edit.dialogFormVisible = false;getList(); "> 关闭 </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postJson,getJson,delJson,checkButtons } from '@/api/common'
import Cookies from 'js-cookie'
import { MessageBox } from 'element-ui'
import { validatePhone } from '@/utils/validate'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils/index.js'
export default {
  name: 'Demand',
  components: { Pagination },
  data() {
    return {
      getdataListParm: {
        name: 'getDemand',
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          content: '',
          cowTypeId:'',
          beginTime:'',
          endTime:'',
          inputDatetime:''
        }
      },
      tableKey: 0,
      list: [{ sqlname: '', id: 0, sqlstr: '' }],
      total: 0,
      listLoading: true,
      edit: {
        dialogStatus: '',
        dialogFormVisible: false,
        temp: {}
      },
      rules: {
        cowTypeId: [{
          required: true,
          message: '必填',
          trigger: 'blur'
        }],
        content: [{ required: true, message: '必填', trigger: 'blur' }],
      },
      isokDisable: false,
      textMap: {
        add: '添加',
        update:'编辑'
      },
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      animalType:[]
    }
  },

  created() {
    this.get_animalTypeList()
    this.getList()
  },
  methods: {
    get_animalTypeList(){
      let url = 'authdata/GetDataByName'
      let data = {
        name:'getDictListSelect',
        parammaps:{
          pid:73
        }
      }
      postJson(url, data).then(response => {
        if (response.code == 200) {
          this.animalType = response.data.list
        } else {
          this.animalType = []
        }
      })
    },
    getList(){
      this.listLoading = false
      let url = 'authdata/GetDataByName'
      let data = this.getdataListParm
      if(this.getdataListParm.parammaps.inputDatetime !== null && this.getdataListParm.parammaps.inputDatetime.length > 0){
        data.parammaps.beginTime = parseTime(this.getdataListParm.parammaps.inputDatetime[0],'{y}-{m}-{d}')
        data.parammaps.endTime = parseTime(this.getdataListParm.parammaps.inputDatetime[1],'{y}-{m}-{d}')
      }else{
        data.parammaps.beginTime = ''
        data.parammaps.endTime = ''
      }
      postJson(url, data).then(response => {
        if (response.code == 200) {
          this.list = response.data.list
          this.total = response.data.total
          this.pageNum = response.data.page
          this.pageSize = response.data.page_size
        } else {
          this.list = []
        }
        setTimeout(() => {
          this.listLoading = false
        }, 1000)
      })
    },
    handleFilter(){
      this.getList()
    },
    handleCreate(){
      this.edit.dialogStatus = 'add'
      this.edit.dialogFormVisible = true
      this.edit.temp= {
        content:'',cowTypeId:'',ordinal:''
      }
    },
    handleUpdate(row){
      this.edit.temp = Object.assign({}, row)
      this.edit.dialogStatus = 'update'
      this.edit.dialogFormVisible = true
    },
    createData(){
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['editTemp'].validate((valid) => {
        if (valid) {
          let url="authdata/PostDataByName"
          let data = {}
          let ordinal = 0
          if(this.edit.temp.ordinal == ''){
            ordinal = 0
          }else{
            ordinal = this.edit.temp.ordinal
          }
          data.name = 'insertDemand'
          data.parammaps = {
            content:this.edit.temp.content,
            cowTypeId:this.edit.temp.cowTypeId,
            ordinal:ordinal,
            createId:Cookies.get('employeid')
          }
          postJson(url, data).then(response => {
            if(response.msg == 'ok'){
              this.getList()
              this.edit.dialogFormVisible = false
              this.$notify({ title: '成功', message: '新增成功', type: 'success', duration: 2000 })
            }else{
              this.$notify({ title: '失败', message: '新增失败', type: 'error', duration: 2000 })
            }
          })
        }
      })
    },
    updateData(){
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['editTemp'].validate((valid) => {
        if (valid) {
          let url="authdata/PostDataByName"
          let data = {}
          let ordinal = 0
          if(this.edit.temp.ordinal == ''){
            ordinal = 0
          }else{
            ordinal = this.edit.temp.ordinal
          }
          data.name = 'updateDemand'
          data.parammaps = {
            content:this.edit.temp.content,
            cowTypeId:this.edit.temp.cowTypeId,
            ordinal:ordinal,
            id:this.edit.temp.id
          }
          postJson(url, data).then(response => {
            if(response.msg == 'ok'){
              this.getList()
              this.edit.dialogFormVisible = false
              this.$notify({ title: '成功', message: '编辑成功', type: 'success', duration: 2000 })
            }else{
              this.$notify({ title: '失败', message: '编辑失败', type: 'error', duration: 2000 })
            }
          })
        }
      })
    },
    handleRowDelete(row){
      MessageBox.confirm('信息删除后无法恢复，确定要删除吗？', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = {}
        data.name = 'deleteDemand'
        data.parammaps = {}
        data.parammaps.id = row.id
        let url="authdata/PostDataByName"
        postJson(url, data).then(response => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .search{margin-top:10px;}

  $width:350px;
  $left:325px;
  .selectInput{
    position: relative;
    display: inline-block;
    .Input{width: $width;position: relative;}
    .el-icon-arrow-down{width: 30px;height: 30px;position: absolute;left: $left;top:10px;color:#C0C4CC;}
    .el-icon-arrow-up{width: 30px;height: 30px;position: absolute;left: $left;top:10px;color:#C0C4CC;}
    .selectUl{
      z-index: 111;width: $width;background: #fff;border: 1px solid #E4E7ED;box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);margin: -1px 0 0 0;padding: 6px 0; margin: 0;box-sizing: border-box;position: absolute;
      li{
        list-style: none;font-size: 14px; padding: 0 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #606266; height: 50px; line-height: 50px; box-sizing: border-box; cursor: pointer;
        a{float:left;width: 80px;}
        a:hover{color: rgba(0, 204, 102, 0.71); font-weight: 700;}
      }
    }
  }
 </style>
