<template>
  <div class="app-container">
    <div class="operation">


      <!-- <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="search">
      <el-input v-model="table.getdataListParm.parammaps.dealerName" placeholder="经销商名称" style="width: 180px;" class="filter-item" clearable />
      <!-- <el-input v-model="table.getdataListParm.parammaps.dealerType" placeholder="经销商类型" style="width: 180px;" class="filter-item" clearable /> -->
      <el-button class="successBorder" @click="form_search">查询</el-button>
      <el-button class="successBorder" @click="form_clear">重置</el-button>
      <el-button v-if="isRoleEdit" class="success"  @click="form_add">添加</el-button>
    </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" :max-height="myHeight">
        <!-- <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.dealerName }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="经销商类型" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.dealerType }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="地址" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.dealerAddress }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系人" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.contacts }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.telephone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>

        <el-table-column label="启用" min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" @change="change_enable(scope.$index, scope.row)" />
          </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-form-item label="经销商名称：" prop="dealerName">
            <el-input ref="dealerName" v-model="create.temp.dealerName" class="filter-item" style="width: 100%;" />
          </el-form-item>

          <!-- <el-form-item label="经销商类型：" prop="dealerType">
            <el-input ref="dealerType" v-model="create.temp.dealerType" />
          </el-form-item>
        -->
          <el-form-item label="地址：" prop="dealerAddress">
            <el-input ref="dealerAddress" v-model="create.temp.dealerAddress" type="textarea" :autosize="{ minRows: 1, maxRows: 8} "  :rows="8" placeholder="" maxlength="200" class="filter-item" style="width: 100%;"   />
          </el-form-item>
          <el-form-item label="联系人：" prop="contacts">
            <el-input ref="contacts" v-model="create.temp.contacts" />
          </el-form-item>
          <el-form-item label="联系电话：" prop="telephone">
            <el-input ref="telephone" v-model="create.temp.telephone" />
          </el-form-item>
          <el-form-item label="备注：" prop="remark">
            <el-input ref="remark" v-model="create.temp.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 8} "  :rows="8" placeholder="" maxlength="200" class="filter-item" style="width: 100%;"   />
          </el-form-item>
          <el-form-item label="启用" prop="enable">
            <el-switch ref="enable" v-model="create.temp.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </el-form-item>

        </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" @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 { isIntegerZero } from '@/utils/validate'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
export default {
  name: 'DealerManagement',
  components: { Pagination },
  data() {
    return {
      myHeight:document.documentElement.clientHeight - 85 - 160,
      isRoleEdit: [],

      //表格请求参数~
      table: {
        getdataListParm: {
          name: 'getDealers',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pagecount')),
          returntype: 'Map',
          parammaps: {
            dealerName: '',
            dealerType: '',
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },

      // 新增/编辑
      create: {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: {
          dealerName: '',
          dealerType: '',
          contacts: '',
          dealerAddress: '',


          remark: '',
          sort: '',
          enable: 1,
          createrId:Cookies.get('employeid')
        },
        rules: {
          dealerName: [{ type: 'string', required: true, message: '必填', trigger: 'blur' }],


        },
      },
      textMap: {
        create: '添加',
        update: '编辑'
      },

      requestParam: {},

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

  created() {
    this.get_table_data()

    // this.get_auto_buttons()
  },

  methods: {
    //获取按钮权限
    get_auto_buttons() {
      const Edit = 'DealerManagement'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },

    //加载表格
    get_table_data() {
      this.table.listLoading = true
      console.log("this.table.getdataListParm======>", this.table.getdataListParm)
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据======>', response.data.list)
        if (response.data.list !== null) {
          for (var i = 0; i < response.data.list.length; i++) {
            response.data.list[i].enable = parseInt(response.data.list[i].enable)
          }

          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_search() {
      console.log('点击了查询')

      this.table.getdataListParm.offset = 1
      this.get_table_data()
    },
    form_clear() {
      console.log('点击了重置')
      this.table.getdataListParm.parammaps.cname = ''
      this.table.getdataListParm.offset = 1
      this.get_table_data()
    },

    // 添加
    form_reset() {
      this.create.temp = {   dealerName: '',
          dealerType: '',
          contacts: '',
          dealerAddress: '',
          createrId: '',

          remark: '',
          sort: '',
          enable: 1 }
    },


    //添加
    form_add() {
      console.log('点击了新增')
      this.form_reset()
      this.create.dialogStatus = 'create'
      this.create.dialogFormVisible = true
    },
    //添加保存
    add_dialog_save() {
      console.log('点击了新增保存',)


      this.$refs['temp'].validate(valid => {
        if (valid) {
          //验证名字是否存在
          this.requestParam.name = 'checkDealerName'
          this.requestParam.parammaps = this.create.temp
          GetDataByName(this.requestParam).then(response => {
            console.log('验证名字是否存在发送参数', this.requestParam)
            if (response.data.list[0].vmsg == 0) {
              //新增保存
              this.requestParam.name = 'insertDealer'
              this.requestParam.parammaps = this.create.temp
              this.requestParam.parammaps.createrId =  Cookies.get('employeid')
              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 {
                  this.$notify({ title: '失败', message: '保存失败', type: 'error', duration: 2000 })
                }
              })
            } else {
              this.$notify({ title: '失败', message: response.data.list[0].vmsg, type: 'error', duration: 2000 })
            }
          })

        }
      })
    },


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

      this.create.temp = Object.assign({}, row)
      this.create.temp.id = row.id
      this.create.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },

    // 编辑保存
    form_edit_save() {
      this.$refs['temp'].validate(valid => {
        if (valid) {

           //验证名字是否存在
           this.requestParam.name = 'checkDealerName'
          this.requestParam.parammaps = this.create.temp
          GetDataByName(this.requestParam).then(response => {
            console.log('验证名字是否存在发送参数', this.requestParam)
            if (response.data.list[0].vmsg == 0) {
              //新增保存
              this.requestParam.name = 'updateDealer'
              this.requestParam.parammaps = this.create.temp
              this.requestParam.parammaps.createrId =  Cookies.get('employeid')
              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 {
                  this.$notify({ title: '失败', message: '保存失败', type: 'error', duration: 2000 })
                }
              })
            } else {
              this.$notify({ title: '失败', message: response.data.list[0].vmsg, type: 'error', duration: 2000 })
            }
          })


        }
      })
    },

    // 表格中的启用
    change_enable(index, row) {


      var send_data =  {
           name: "updateDealerEnable",
           parammaps: {
              "enable": row.enable,
              "id": row.id
          }
      }


      PostDataByName(send_data).then(response => {
        console.log('表格中的启用发送参数',send_data)
        if (response.msg !== 'fail') {
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.create.dialogFormVisible = false
          this.get_table_data()
        } else {
          this.$notify({ title: '失败', message: '保存失败', type: 'error', duration: 2000 })
        }
      })


    },

    // 删除
    form_delete(row) {
      MessageBox.confirm('是否确认删除此信息?', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        this.requestParam.name = 'deleteDealer'
        this.requestParam.parammaps = {}
        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>
