<template>
  <div class="app-container">
    <div class="operation" style="margin-bottom:20px">
      <el-button v-if="isRoleEdit" class="success" style="float: left;margin-right:10px;" @click="form_add">添加</el-button>

      <!-- <el-button v-if="isRoleEdit" class="import" style="float: right;" @click="form_inport">导入</el-button>
      <el-dropdown style="float: right;margin-right: 10px;">
        <el-button class="export">导出</el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="form_export(1)">导出模板</el-dropdown-item>
          <el-dropdown-item @click.native="form_export(2)">导出数据</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown> -->
    </div>

    <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">
        <!-- <el-table-column type="selection" align="center" 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="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.barname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.times }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.times }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="120px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.times }}</span>
          </template>
        </el-table-column>
        <el-table-column label="顺序" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.times }}</span>
          </template>
        </el-table-column>
        <el-table-column label="启用" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">

            <el-switch v-model="scope.row.operateweight" active-color="#13ce66" inactive-color="#ff4949" disabled></el-switch>

          </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="isRoleEdit" class="miniSuccess" @click="form_edit(row)">编辑</el-button>
            <el-button v-if="isRoleEdit" class="miniDanger" @click="form_delete(row)">删除</el-button> -->

            <el-button class="miniSuccess" @click="form_edit(row)">编辑</el-button>
            <el-button class="miniDanger" @click="form_delete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="table.total>=0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="get_table_data" />
    </div>

    <!-- 新增/编辑 -->
    <el-dialog :title="textMap[create.dialogStatus]" :visible.sync="create.dialogFormVisible" :close-on-click-modal="false" width="40%">
      <div class="app-add">
        <el-form ref="temp" :rules="create.rules" :model="create.temp" label-position="right" label-width="150px" style="width: 80%;margin:0 auto 50px">
          <el-row>
            <el-form-item label="姓名：" prop="jituan">
              <el-input ref="jituan" v-model="create.temp.jituan" class="filter-item" style="width: 100%;" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="部门：" prop="barid">
              <el-select ref="barid" v-model="create.temp.barid" filterable placeholder="请选择客户" class="filter-item" style="width: 100%;" @change="change_bar">
                <el-option v-for="(item,index) in houseNameList" :key="index" :label="item.bname" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="电话：" prop="jituan">
              <el-input ref="jituan" v-model="create.temp.jituan" class="filter-item" style="width: 100%;" />
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="备注：" prop="remark">
              <el-input ref="remark" v-model="create.temp.remark" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" placeholder="请输入备注，最多255个字" maxlength="255" class="filter-item" style="width: 100%;" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="顺序：" prop="jituan">
              <el-input ref="jituan" v-model="create.temp.jituan" class="filter-item" style="width: 100%;" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="启用：" prop="remark">
              <el-switch v-model="create.temp.operateweight" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
            </el-form-item>
          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelClose" @click="create.dialogFormVisible = false;get_table_data()">关闭</el-button>
          <el-button v-if="create.dialogStatus==='create' || create.dialogStatus==='update'" class="success" :disabled="isokDisable" @click="create.dialogStatus==='create'?add_dialog_save():form_edit_save()">确认</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName, PostDataByName, failproccess, ExecDataByConfig, GetDataByNames, checkButtons, calculativeWidth } from '@/api/common'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
export default {
  name: 'OtherProjects',
  components: { Pagination },
  data() {
    return {

      isRoleEdit: [],
      send_select_list: [
        { name: 'getBarListEnable', offset: 0, pagecount: 0, parammaps: { 'pastureid': Cookies.get('pastureid') } },
        { name: 'getSysoptEnable', offset: 0, pagecount: 0, parammaps: { 'pastureid': Cookies.get('pastureid'), 'inforname': 'times' } }
      ],
      houseNameList: [], // 客户名称
      jiTuanList: [], // 集团
      jiTuanList: [], // 集团


      frequencyList: [], // 班次

      //表格请求参数~~
      table: {
        getdataListParm: {
          name: 'getDungscoreList',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pagecount')),
          returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid'),
            barid: '',
            times: '',
            startTime: '',
            stopTime: '',
            inputDatetime: ''
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },

      // 新增/编辑
      create: {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: { pastureid: Cookies.get('pastureid'), operatetime: parseTime(new Date(), '{y}-{m}-{d}'), emp: Cookies.get('employename'), barid: '', barname: '', times: '', standardscore: '', cowsum: '', score1: '', score2: '', score3: '', score4: '', score5: '', remark: '', ftid: '', ftname: '' },
        rules: {
          barid: [{ type: 'string', required: true, message: '必填', trigger: 'blur' }],
          times: [{ type: 'string', required: true, message: '必填', trigger: 'blur' }],
          tele: [{ type: 'number', message: '必须为数字值', trigger: 'blur' }],

        },
        // 操作人
        getdataListParm: {
          name: 'getDungscoreEmpHis',
          page: 1,
          offset: 1,
          pagecount: 10,
          returntype: 'Map',
          parammaps: {
            pastureid: Cookies.get('pastureid')
          }
        },

        noteTakerList: [] // 记录人
      },
      textMap: {
        create: '新增',
        see: '查看',
        update: '编辑'
      },

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

  created() {
    this.get_table_data()
    this.get_select_list()
    // this.get_auto_buttons()
  },

  methods: {
    //获取按钮权限
    get_auto_buttons() {
      const Edit = 'DungScores'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },
    //获取所有下拉框的内容
    get_select_list() {
      GetDataByNames(this.send_select_list).then(response => {

        this.houseNameList = response.data.getBarListEnable.list
        console.log("客户名称下拉框", this.houseNameList)
      })
    },
    //加载表格
    get_table_data() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {

          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)
      })
    },


    // 新增
    form_reset() {
      this.create.temp = { pastureid: Cookies.get('pastureid'), operatetime: parseTime(new Date(), '{y}-{m}-{d}'), emp: Cookies.get('employename'), barid: '', barname: '', times: '', standardscore: '', cowsum: '', score1: '', score2: '', score3: '', score4: '', score5: '', remark: '', ftid: '', ftname: '' }
    },

    //监听 - 表格中客户名称
    change_bar(item) {
      this.create.temp.barname = this.houseNameList.find(obj => obj.id === item).bname
    },


    // 操作人
    getNoteTakerList() {
      GetDataByName(this.create.getdataListParm).then(response => {
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            response.data.list[i].myId = i
          }
          this.create.noteTakerList = response.data.list
          console.log('记录人数据', response.data.list)
          console.log(this.create.noteTakerList)
        } else {
          this.create.noteTakerList = []
        }
      })
    },

    //新增
    form_add() {
      console.log('点击了新增')
      this.form_reset()
      this.getNoteTakerList()
      this.create.dialogStatus = 'create'
      this.create.dialogFormVisible = true
    },
    //新增保存
    add_dialog_save() {
      console.log('点击了新增保存')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)
          const score = /^\d*$/


          this.requestParam.name = 'insertDungscore'
          this.requestParam.parammaps = this.create.temp
          if (this.create.temp.emp == '') { this.create.temp.emp = Cookies.get('employename') }
          PostDataByName(this.requestParam).then(response => {
            console.log('新增保存发送参数', this.requestParam)
            if (response.msg !== 'fail') {
              this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
              this.create.dialogFormVisible = false
              this.get_table_data()
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },


    // 编辑
    form_edit(row) {
      console.log('点击了编辑', row)
      row.barid = String(row.barid)

      this.create.temp = Object.assign({}, row)
      this.create.temp.id = row.id

      this.getNoteTakerList()
      this.create.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },

    // 编辑保存
    form_edit_save() {
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)


          this.requestParam.name = 'updateDungscore'
          this.requestParam.parammaps = this.create.temp
          if (this.create.temp.emp == '') { this.create.temp.emp = Cookies.get('employename') }
          PostDataByName(this.requestParam).then(response => {
            console.log('新增保存发送参数', this.requestParam)
            if (response.msg !== 'fail') {
              this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
              this.create.dialogFormVisible = false
              this.get_table_data()
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },

    // 删除
    form_delete(row) {
      MessageBox.confirm('是否确认删除此信息?', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        this.selectList = []
        this.requestParam.name = 'deleteDungscore'
        this.requestParam.parammaps = {}
        this.requestParam.parammaps.pastureid = row.pastureid
        this.requestParam.parammaps.id = row.id
        PostDataByName(this.requestParam).then(response => {
          if (response.msg === 'fail') {
            this.$notify({ title: '删除失败', message: response.data, type: 'warning', duration: 2000 })
          } else {
            this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 2000 })
            this.get_table_data()
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' })
      })
    },


    // 导出
    form_export(item) {
      if (item == 1) {
        console.log('点击了导出模板')
      } else {
        console.log('点击了导出数据')
      }
    },

    // 导入
    form_inport() {
      console.log('点击了导入')
    }

  }
}
</script>
<style lang="scss" scoped>
.search {
  padding-top: 10px;
  clear: both;
}
.table {
  margin-top: 10px;
}
</style>
