<template>
  <div class="app-container">
    <div class="search">
      <el-input v-model="getdataListParm.parammaps.inputValue" 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 class="successBorder" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button  class="success" icon="el-icon-plus" @click="handleCreate">添加</el-button>
      <el-button class="export" @click="form_export()">导出</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" :max-height="myHeight"
      >
        <el-table-column label="姓名" min-width="100px" align="center" prop="customerName" />
        <el-table-column label="电话" min-width="100px" align="center" prop="telephone" />
        <el-table-column label="职务" min-width="100px" align="center" prop="duties" />
        <el-table-column label="公司/牧场名称" min-width="100px" align="center" prop="company" />
        <el-table-column label="畜牧类别" min-width="100px" align="center" prop="cowTypeName" />
        <el-table-column label="所在地区" min-width="100px" align="center" prop="region" />
        <el-table-column label="详细地址" min-width="100px" align="center" prop="address" />
        <el-table-column label="存栏" min-width="100px" align="center" prop="cowCount" />
        <el-table-column label="需求" min-width="100px" align="center" prop="demandName" />
        <el-table-column label="备注" min-width="100px" align="center" prop="remark" />
        <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="18">
                <el-form-item label="姓名:" prop="customerName">
                  <el-input ref="customerName" maxlength="20" v-model="edit.temp.customerName" placeholder="请输入姓名" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="联系电话:" prop="telephone">
                  <el-input ref="telephone" maxlength="11" type="number" v-model="edit.temp.telephone" placeholder="请填写能联系到您的电话" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="职务:" prop="duties">
                  <el-input ref="duties" maxlength="120" show-word-limit v-model="edit.temp.duties" placeholder="请输入职务" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="公司/牧场名称:" prop="company">
                  <el-input ref="company" maxlength="20" show-word-limit v-model="edit.temp.company" placeholder="请输入公司/牧场名称" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="畜牧类别:" prop="cowTypeId">
                 <el-select v-model="edit.temp.cowTypeId" placeholder="请选择" style="width: 100%;" @change="changeCowType">
                   <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="18">
                <el-form-item label="所在地区:" prop="region2">
                  <el-cascader size="large" :options="pcaTextArr" v-model="edit.temp.region2" style="width: 100%;"></el-cascader>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="详细地址:" prop="address">
                  <el-input ref="address" maxlength="60" v-model="edit.temp.address" type="textarea" placeholder="街道、门牌号等" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="存栏:" prop="cowCount">
                  <el-input ref="cowCount" maxlength="20" v-model="edit.temp.cowCount" type="number" placeholder="请输入存栏" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="需求:" prop="demandId">
                 <el-select v-model="edit.temp.demandId" placeholder="请选择" style="width: 100%;">
                   <el-option v-for="item in demandList" :key="item.id" :label="item.name" :value="item.id" />
                 </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="备注:" prop="remark">
                  <el-input ref="remark" maxlength="320" type="textarea" v-model="edit.temp.remark" placeholder="请填写您其他特别说明" style="width: 100%;" />
                </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,json2excel } from '@/utils/index.js'
import { pcaTextArr } from 'element-china-area-data'
export default {
  name: 'Leadcustomers',
  components: { Pagination },
  data() {
    return {
      myHeight:document.documentElement.clientHeight - 85 - 160,
      getdataListParm: {
        name: 'getCustomerinfo',
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          cowTypeId: '',
          inputValue: '',
          beginTime:'',
          endTime:'',
          inputDatetime:''
        }
      },
      tableKey: 0,
      list: [{ sqlname: '', id: 0, sqlstr: '' }],
      total: 0,
      listLoading: true,
      edit: {
        dialogStatus: '',
        dialogFormVisible: false,
        temp: {}
      },
      rules: {
        customerName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        telephone: [
          { required: true, message: '请填写能联系到您的电话', trigger: 'blur' },
          { validator: validatePhone,required: true,message: '请填写正确的手机号', trigger: 'blur' },
        ],
        company: [{ required: true, message: '请输入公司/牧场名称', trigger: 'blur' }],
        cowTypeId: [{ required: true, message: '请选择畜牧类别', trigger: 'blur' }],
        region2: [{ required: true, message: '请选择所在地区', trigger: 'blur' }],
        demandId: [{ required: true, message: '请选择需求', trigger: 'blur' }],
      },
      isokDisable: false,
      textMap: {
        add: '添加',
        update:'编辑'
      },

      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      animalType:[],
      demandList:[],
      pcaTextArr,
    }
  },

  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()
    },
    changeCowType(item){
    	this.edit.temp.cowTypeId = item
    	if(this.edit.temp.cowTypeId !== ''){
    		this.get_demandList()
    	}
    },
    changedDmand(item){
    	this.edit.temp.demandId = item
    },
    get_demandList(){
      let url = 'GetDataByNameOther'
      let data = {
        name:'getDemandByCowTypeId',
        parammaps:{
          cowTypeId:this.edit.temp.cowTypeId
        }
      }
      postJson(url, data).then((res)=> {
      	if (res.data.list !== null) {
      	  this.demandList = res.data.list
    	  // for(var i=0;i<this.demandList.length;i++){
    	  // 	this.demandList[i].text = this.demandList[i].name;
    	  // 	this.demandList[i].value = this.demandList[i].id;
    	  // }
      	} else {
      	  this.demandList = []
      	}
      })
    },
    handleCreate(){
      this.edit.dialogStatus = 'add'
      this.edit.dialogFormVisible = true
      this.edit.temp= {
        content:'',cowTypeId:'',ordinal:''
      }
    },
    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 = {
          	name:'insertCustomerinfo',
          	parammaps:{
          		customerName:this.edit.temp.customerName,
          		telephone:this.edit.temp.telephone,
          		duties:this.edit.temp.duties,
          		company:this.edit.temp.company,
          		cowTypeId:this.edit.temp.cowTypeId,
          		region:this.edit.temp.region2.join(""),
              region2:this.edit.temp.region2,
          		address:this.edit.temp.address,
          		cowCount:this.edit.temp.cowCount,
          		demandId:this.edit.temp.demandId,
          		remark:this.edit.temp.remark
          	}
          }
          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 = {
          	name:'updateCustomerinfo',
          	parammaps:{
          		customerName:this.edit.temp.customerName,
          		telephone:this.edit.temp.telephone,
          		duties:this.edit.temp.duties,
          		company:this.edit.temp.company,
          		cowTypeId:this.edit.temp.cowTypeId,
          		region:this.edit.temp.region2.join(""),
          		region2:this.edit.temp.region2,
          		address:this.edit.temp.address,
          		cowCount:this.edit.temp.cowCount,
          		demandId:this.edit.temp.demandId,
          		remark:this.edit.temp.remark,
          		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 = 'deleteCustomerinfo'
        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
          })
        })
      })
    },
    form_export(){
      this.listLoading = false
      let url = 'authdata/GetDataByName'
      let data = this.getdataListParm
      data.pagecount = ''
      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 => {
        var dataList = [
          {
            tHeader: ['姓名', '电话', '职务','公司/牧场名称','畜牧类别','所在地区','详细地址','存栏','需求','备注','提交时间'],
            filterVal: ['customerName','telephone','duties','company','cowTypeName','region','address','cowCount','demandName','remark','createTime'],
            tableDatas: response.data.list,
            sheetName: '泌乳牛采食量'
          }
        ]
        json2excel(dataList, '线索客户', true, 'xlsx')
      })
    }
  }
}
</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>
