<template>
  <div class="app-container">
    <el-card>
      <span>客户名称：</span>
      <el-select v-model="CustomerNameValue" filterable placeholder="请输入客户名称" style="margin-right: 10px" size="small" clearable>
        <el-option
          v-for="item in CustomerName"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <!--<el-input v-model="CustomerName" placeholder="请输入客户名称" size="small"></el-input>-->
      <!--<span class="move">销售经理：</span>-->
      <!--<el-input clearable v-model="MarketingManagerValue" @dblclick.native="dialogFormVisiblePeople = true" size="small" style="width:200px" placeholder="请选择营销经理" clearable></el-input>-->
      <!--<span class="move">信誉等级：</span>-->
      <!--<el-select v-model="CreditRatingValue" placeholder="请选择信誉等级" size="small">-->
      <!--<el-option-->
      <!--v-for="item in CreditRating"-->
      <!--:key="item.value"-->
      <!--:label="item.label"-->
      <!--:value="item.value">-->
      <!--</el-option>-->
      <!--</el-select>-->
      <el-button type="success" icon="el-icon-search" class="move" size="small" @click="selectCustomerOfName">查询</el-button>
    </el-card>
    <el-card>
      <el-button type="text" icon="el-icon-circle-plus-outline" size="mini" @click="addMarketingManager();flag = 1">添加销售经理</el-button>
      <span style="float: right">销售经理</span>
      <el-table v-loading="loading3" :data="tableData3" height="192" size="mini" style="width: 100%;margin-top: 10px" border>
        <!--<el-table-column label="责权省份" align="center" min-width="80">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.省份 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">
            {{ scope.row.性别 }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center">
          <template slot-scope="scope">
            {{ scope.row.联系电话 }}
          </template>
        </el-table-column>
        <el-table-column label="生日" align="center">
          <template slot-scope="scope">
            {{ scope.row.出生日期 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="handleEditMarketingManager(scope.row);flag = 2">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="handleDeleteMarketingManager(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-row>
      <el-col :span="5">
        <el-card>
          <el-table
            v-loading="loading1"
            ref="multipleTable"
            :data="tableData1"
            stripe
            tooltip-effect="dark"
            style="width: 100%"
            height="690"
            size="mini"
            border>
            <el-table-column align="center" label="序号" width="80">
              <template slot-scope="scope">
                <el-radio :label="scope.row.省份流水号" v-model="radio" @change="pageCurrent = 1;pageSize = 30;getCurrentRow(scope.row.省份流水号, scope.row.省份)"/>
              </template>
            </el-table-column>
            <el-table-column align="center" label="地区">
              <template slot-scope="scope">
                {{ scope.row.省份 }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="19">
        <el-card>
          <el-button type="text" icon="el-icon-circle-plus-outline" size="mini" @click = "addCustomer();flag1 = 1">新增客户</el-button>
          <span style="float: right">客户信息</span>
          <el-table v-loading="loading2" :data="tableData2" height="620" size="mini" style="width: 100%;margin-top: 10px" border>
            <el-table-column label="客户名称" align="center" min-width="340">
              <template slot-scope="scope">
                {{ scope.row.客户名称 }}
              </template>
            </el-table-column>
            <el-table-column label="联系人" align="center" min-width="100">
              <template slot-scope="scope">
                {{ scope.row.联系人 }}
              </template>
            </el-table-column>
            <el-table-column label="联系电话" align="center" min-width="300">
              <template slot-scope="scope">
                {{ scope.row.电话 }}
              </template>
            </el-table-column>
            <el-table-column label="开票电话" align="center" min-width="200">
              <template slot-scope="scope">
                {{ scope.row.传真 }}
              </template>
            </el-table-column>
            <el-table-column label="联系地址" align="center" min-width="350">
              <template slot-scope="scope">
                {{ scope.row.地址 }}
              </template>
            </el-table-column>
            <el-table-column label="电子邮箱" align="center" min-width="250">
              <template slot-scope="scope">
                {{ scope.row.电子邮箱 }}
              </template>
            </el-table-column>
            <el-table-column label="税号" align="center" min-width="120">
              <template slot-scope="scope">
                {{ scope.row.税号 }}
              </template>
            </el-table-column>
            <el-table-column label="开户行" align="center" min-width="200">
              <template slot-scope="scope">
                {{ scope.row.开户行 }}
              </template>
            </el-table-column>
            <el-table-column label="银行账号" align="center" min-width="260">
              <template slot-scope="scope">
                {{ scope.row.银行账号 }}
              </template>
            </el-table-column>
            <el-table-column label="信誉等级" align="center" min-width="80">
              <template slot-scope="scope">
                {{ scope.row.价值评估 }}
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" min-width="350">
              <template slot-scope="scope">
                {{ scope.row.备注 }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" min-width="200px" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  class="el-icon-edit"
                  @click="handleEdit(scope.row);flag1 = 2">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  class="el-icon-delete"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              v-if="!loading2"
              :current-page="pageCurrent"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      :visible.sync="dialogVisible"
      title="提示"
      width="30%">
      <span>该人员已经是销售经理了，是否还要继续添加</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPer">确 定</el-button>
      </span>
    </el-dialog>
    <!--选择人员-->
    <el-dialog :visible.sync="dialogFormVisiblePeople" title="人员信息" center width="750px">
      <div style="height: 420px">
        <div style="float: left;margin-top: 10px;margin-left: 20px;overflow-y: scroll;height: 400px">
          <el-tree
            :data="dataPeople"
            :default-expanded-keys="treeData"
            node-key="id"
            style="width: 150px;float: left"
            height="400"
            accordion
            @node-click="handleNodeClick"
          />
        </div>
        <el-table :data="ListPeople" height="400" style="width: 400px;float: left;margin-left: 30px" size="mini" highlight-current-row @row-click="handleChange">
          <el-table-column label="考勤编号" align="center" >
            <template slot-scope="scope">
              {{ scope.row.考勤编号 }}
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="center" >
            <template slot-scope="scope">
              {{ scope.row.姓名 }}
            </template>
          </el-table-column>
          <el-table-column label="性别" align="center">
            <template slot-scope="scope">
              {{ scope.row.性别 }}
            </template>
          </el-table-column>
          <el-table-column label="工作岗位" align="center">
            <template slot-scope="scope">
              {{ scope.row.岗位名称 }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-button style="margin-left: 260px" @click="offName">取消</el-button>
      <el-button type="primary" style="margin-top: 20px" @click="getName">确认</el-button>
    </el-dialog>

    <el-dialog :title="title1" :visible.sync="dialogFormVisible1" center width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px" class="demo-ruleForm" style="margin: auto">
        <el-form-item label="营销经理" prop="营销经理">
          <el-input v-model="form1.营销经理" clearable size="small" style="width:200px" placeholder="请选择营销经理" readonly @dblclick.native="dialogFormVisiblePeople = true;searchPer()"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff1()">取消</el-button>
        <el-button type="primary" @click="submit1('form1')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="dialogFormVisible2" center width="700px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="90px" class="demo-ruleForm" style="margin: auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="省份:" prop="省份">
              <el-input v-model="form2.省份" :disabled="true" clearable size="small" style="width:200px" placeholder="请选择省份"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称:" prop="客户名称">
              <el-input v-model="form2.客户名称" clearable size="small" style="width:200px" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人:" prop="联系人">
              <el-input v-model="form2.联系人" clearable size="small" style="width:200px" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="90px" class="demo-ruleForm" style="margin: auto">
              <el-form-item label="联系电话:" prop="联系电话">
                <el-input v-model="form2.联系电话" clearable size="small" style="width:200px" placeholder="请输入联系电话"/>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票电话:" prop="传真">
              <el-input v-model="form2.传真" clearable size="small" style="width:200px" placeholder="请输入开票电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系地址:" prop="联系地址">
              <el-input v-model="form2.联系地址" clearable size="small" style="width:200px" placeholder="请输入联系地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱:" prop="电子邮箱">
              <el-input v-model="form2.电子邮箱" clearable size="small" style="width:200px" placeholder="请输入电子邮箱"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税号:" prop="税号">
              <el-input v-model="form2.税号" clearable size="small" style="width:200px" placeholder="请输入税号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号:" prop="银行账号">
              <el-input v-model="form2.银行账号" clearable size="small" style="width:200px" placeholder="请输入银行账号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用等级:" prop="信用等级">
              <el-select v-model="form2.信用等级" placeholder="请选择信誉等级" size="small">
                <el-option
                  v-for="item in CreditRating"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户行:" prop="开户行">
              <el-input v-model="form2.开户行" clearable size="small" style="width:200px" placeholder="请输入开户行"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注:" prop="备注">
              <el-input v-model="form2.备注" clearable size="small" style="width:200px" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff2()">取消</el-button>
        <el-button type="primary" @click="submit2('form2')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elInput from 'element-ui/packages/input'
import { searchValue, selectAdress, selectCustomer, getTree, tree, getTablePeople, selectMarketingManager, selectCustomerOfName, initCustomer, deleteCustomer, deleteMarketingManager, addMarketingManager, editMarketingManager, addCustomer, selectAdressOfID, editCustomer, addPer } from '@/api/MarketingCenter/CRM'
export default {
  components: {
    elInput
  },
  data() {
    return {
      dialogVisible: false,
      treeData: [7],
      a: '', // 营销经理地区流水号
      b: '', // 省份
      c: '', // 客户流水号
      flag: null,
      flag1: null,
      title1: '', // 营销经理对话框
      title2: '', // 客户对话框
      dialogFormVisible1: false, // 营销经理对话框可见性
      dialogFormVisible2: false, // 客户对话框可见性
      CustomerNameValue: '',
      CustomerName: [], // 客户名称
      // CreditRatingValue: '', // 信誉等级
      CreditRating: [], // 信誉等级数组
      tableData1: [], // 地区表
      loading1: false, // 地区的加载动画
      loading2: false, // 客户的加载动画
      radio: null, // 地区选择框
      tableData2: [], // 客户表
      pageCurrent: 1,
      pageSize: 30,
      total: null,
      AdressID: null, // 地区流水号
      MarketingManagerValue: '', // 营销经理
      dialogFormVisiblePeople: false,
      dataPeople: [],
      ListPeople: [],
      peoplename: '',
      peopleid: '',
      tableData3: [],
      loading3: false,
      form1: {
        营销经理: '' // 营销经理
      },
      form2: {
        省份: '',
        客户名称: '',
        联系人: '',
        联系电话: '',
        传真: '',
        联系地址: '',
        电子邮箱: '',
        税号: '',
        银行账号: '',
        信用等级: 1,
        备注: '',
        开户行: ''
      },
      rules1: { // 表单验证规则
        营销经理: [{ required: true, message: '请选择营销经理', trigger: 'change' }]
      },
      rules2: { // 表单验证规则
        省份: [{ required: true, message: '请选择省份' }],
        客户名称: [{ required: true, message: '请输入客户名称' }],
        联系人: [{ required: true, message: '请输入联系人' }],
        信用等级: [{ required: true, message: '请选择信用等级' }]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.loading1 = true
      selectAdress().then(response => {
        this.tableData1 = response.data
        this.loading1 = false
      })
      this.getSelect(searchValue, ['价值评估', '价值评估流水号'], 'CreditRating')
      getTree().then(response => { // 获取人员信息树
        const data = response.data
        this.dataPeople = tree(data, 0)
      })
      this.getSelect(initCustomer, ['客户名称', '客户流水号'], 'CustomerName')
      selectMarketingManager(1).then(response => {
        this.loading3 = false
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
        }
        this.tableData3 = response.data
      })
    },
    getCurrentRow(row) {
      this.CustomerNameValue = ''
      this.AdressID = row
      this.getTable(selectCustomer, [this.AdressID, this.pageCurrent, this.pageSize], ['tableData2', 'total', 'loading2'])
      this.loading3 = true
      selectMarketingManager(1).then(response => {
        this.loading3 = false
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
        }
        this.tableData3 = response.data
      })
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTable(selectCustomer, [this.AdressID, this.pageCurrent, this.pageSize], ['tableData2', 'total', 'loading2'])
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.getTable(selectCustomer, [this.AdressID, this.pageCurrent, this.pageSize], ['tableData2', 'total', 'loading2'])
    },
    searchPer() {
      this.getData(getTablePeople, 'ListPeople', 7)
    },
    handleNodeClick(data) { // 树节点点击
      this.getData(getTablePeople, 'ListPeople', data.id)
    },
    offName() {
      this.dialogFormVisiblePeople = false
      this.peopleid = ''
      this.peoplename = ''
    },
    getName() {
      this.dialogFormVisiblePeople = false
      this.form1.营销经理 = this.peoplename
    },
    handleChange(row) {
      this.peopleid = row.人员编号
      this.peoplename = row.姓名
    },
    selectCustomerOfName() {
      selectCustomerOfName(this.CustomerNameValue, this.pageCurrent, this.pageSize).then(response => {
        this.radio = parseInt(response.data.rows[0].客户名称省份流水号)
        this.AdressID = parseInt(response.data.rows[0].客户名称省份流水号)
        this.tableData2 = response.data.rows
        return this.AdressID
      }).then(data => {
        this.loading3 = false
        selectMarketingManager(1).then(response => {
          this.loading3 = false
          for (let i = 0; i < response.data.length; i++) {
            response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
          }
          this.tableData3 = response.data
        })
      })
    },
    handleDelete(index, row) {
      function a() {
        if (this.CustomerNameValue === '') {
          this.getTable(selectCustomer, [this.AdressID, this.pageCurrent, this.pageSize], ['tableData2', 'total', 'loading2'])
          // this.loading3 = true
          // selectMarketingManager(this.AdressID).then(response => {
          //   this.loading3 = false
          //   this.tableData3 = response.data
          // })
        }
        if (this.CustomerNameValue !== '') {
          selectCustomerOfName(row.客户流水号, this.pageCurrent, this.pageSize).then(response => {
            // this.radio = parseInt(response.data.rows[0].客户名称省份流水号)
            // this.AdressID = parseInt(response.data.rows[0].客户名称省份流水号)
            this.tableData2 = response.data.rows
            // return this.AdressID
          })
        }
      }
      this.deleteRow(deleteCustomer, row.客户流水号, a)
    },
    handleDeleteMarketingManager(index, row) {
      function a() {
        selectMarketingManager(1).then(response => {
          this.loading3 = false
          for (let i = 0; i < response.data.length; i++) {
            response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
          }
          this.tableData3 = response.data
        })
      }
      this.deleteRow(deleteMarketingManager, row.营销经理地区流水号, a)
    },
    addMarketingManager() {
      this.dialogFormVisible1 = true
      this.title1 = '增加'
      this.addForm('form1')
    },
    callOff1() {
      this.dialogFormVisible1 = false
    },
    submit1() {
      function a() {
        this.dialogFormVisible1 = false
        this.loading3 = true
        selectMarketingManager(1).then(response => {
          this.loading3 = false
          for (let i = 0; i < response.data.length; i++) {
            response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
          }
          this.tableData3 = response.data
        })
      }
      if (this.flag === 1) {
        addMarketingManager(this.peopleid, 1).then(response => {
          console.log(response)
          if (response.data.hasOwnProperty('output') === false) {
            if (response[0].result === '0') {
              this.$notify.error(`增加失败`)
            }
          } else {
            console.log(response)
            if (response.data.result[0].result === '0') {
              this.$notify.error(`增加失败`)
            }
            if (response.data.result[0].result === '1' && response.data.output[0].导入类型 === '1') {
              this.dialogFormVisible1 = false
              this.$notify.success('增加成功')
              this.loading3 = true
              selectMarketingManager(1).then(response => {
                this.loading3 = false
                for (let i = 0; i < response.data.length; i++) {
                  response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
                }
                this.tableData3 = response.data
              })
            }
            if (response.data.output[0].导入类型 === '2') {
              this.dialogVisible = true
            }
          }
        })
        // this.addTable(addMarketingManager, [this.peopleid, 1], 'form1', a)
      } else {
        this.editTable(editMarketingManager, [this.peopleid, this.a], 'form1', a)
      }
    },
    addPer() {
      addPer(this.peopleid, 1).then(response => {
        if (response.data[0].result === '0') {
          this.$notify.error(`增加失败`)
        } else {
          this.dialogFormVisible1 = false
          this.dialogVisible = false
          this.$notify.success('增加成功')
          this.loading3 = true
          selectMarketingManager(1).then(response => {
            this.loading3 = false
            for (let i = 0; i < response.data.length; i++) {
              response.data[i].出生日期 = (response.data[i].出生日期).split(' ')[0]
            }
            this.tableData3 = response.data
          })
        }
      })
    },
    handleEditMarketingManager(row) {
      this.dialogFormVisible1 = true
      this.title1 = '编辑'
      this.form1.营销经理 = row.姓名
      this.peopleid = row.营销经理流水号
      this.a = row.营销经理地区流水号
    },
    addCustomer() {
      this.addForm('form2')
      if (this.AdressID === '' || this.AdressID === null) {
        this.$message.warning('请选择一个省份')
      } else {
        this.dialogFormVisible2 = true
        this.title2 = '增加'
      }
      selectAdressOfID(this.AdressID).then(response => {
        this.form2.省份 = response.data[0].省份
      })
    },
    callOff2() {
      this.dialogFormVisible2 = false
    },
    handleEdit(row) {
      this.dialogFormVisible2 = true
      this.title2 = '编辑'
      this.form2.省份 = row.省份
      this.form2.客户名称 = row.客户名称
      this.form2.联系电话 = row.电话
      this.form2.传真 = row.传真
      this.form2.联系地址 = row.地址
      this.form2.联系人 = row.联系人
      this.form2.电子邮箱 = row.电子邮箱
      this.form2.银行账号 = row.银行账号
      this.form2.税号 = row.税号
      this.form2.信用等级 = parseInt(row.价值评估流水号)
      this.form2.备注 = row.备注
      this.c = row.客户流水号
      this.form2.开户行 = row.开户行
    },
    submit2() {
      function a() {
        this.dialogFormVisible2 = false
        this.getTable(selectCustomer, [this.AdressID, this.pageCurrent, this.pageSize], ['tableData2', 'total', 'loading2'])
      }
      if (this.flag1 === 1) {
        this.addTable(addCustomer, [this.AdressID, this.form2.客户名称, this.form2.联系电话, this.form2.传真, this.form2.联系地址, this.form2.联系人, this.form2.电子邮箱, this.form2.银行账号, this.form2.税号, this.form2.信用等级, this.form2.备注, this.form2.开户行], 'form2', a)
      } else {
        this.editTable(editCustomer, [this.AdressID, this.form2.客户名称, this.form2.联系电话, this.form2.传真, this.form2.联系地址, this.form2.联系人, this.form2.电子邮箱, this.form2.银行账号, this.form2.税号, this.form2.信用等级, this.form2.备注, this.c, this.form2.开户行], 'form2', a)
      }
    }
  }
}
</script>

<style scoped>
  .el-input{
    width: 200px;
  }
  .el-select{
    width: 200px;
  }
  .move{
    margin-left: 10px;
  }
  .el-table td, .el-table th.is-leaf,
  .el-radio__inner,
  .el-input__inner{
    border: 1px solid black!important;
  }
</style>
