<!--
 * @Author: wanglj 471442253@qq.com
 * @Date: 2022-12-15 15:38:21
 * @LastEditors: wanglj
 * @LastEditTime: 2022-12-28 16:10:14
 * @Description: file content
 * @FilePath: \opms_frontend\src\views\customer\openSea.vue
-->
<template>
  <div class="open-sea-container">
    <el-row :gutter="10" style="margin-bottom: 10px">
      <el-col :span="4">
        <el-input v-model="queryForm.custCode" placeholder="客户编码" />
      </el-col>
      <el-col :span="4">
        <el-input v-model="queryForm.custName" placeholder="客户名称" />
      </el-col>
      <el-col :span="4">
        <el-input v-model="queryForm.custIndustry" placeholder="客户行业" />
      </el-col>
      <el-col :span="4">
        <el-input v-model="queryForm.custLevel" placeholder="客户级别" />
      </el-col>
      <el-col :span="8">
        <el-button icon="el-icon-plus" type="primary" @click="fetchData">查询</el-button>
        <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
      </el-col>
    </el-row>
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="$refs.edit.init()">新建</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="$refs.allocate.visible = true">分配</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleReceive">领取</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-button icon="el-icon-download" />
        <el-button icon="el-icon-setting" />
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="calc(100vh - 340px)"
      @selection-change="setSelectRows">
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column align="center" label="客户编码" prop="custCode" />
      <el-table-column align="center" label="客户名称" prop="custName" />
      <el-table-column align="center" label="助记名" prop="abbrName" />
      <el-table-column align="center" label="所在地区" prop="custLocation" />
      <el-table-column align="center" label="客户行业" prop="custIndustry" />
      <el-table-column align="center" label="客户级别" prop="custLevel" />
      <el-table-column align="center" label="客户状态" prop="custStatus">
        <template slot-scope="scope">
          {{ scope.row.custStatus == 10 ? '正常' : '异常' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后跟进时间" prop="followUpDate" />
      <el-table-column align="center" label="创建人" prop="createdName" />
      <el-table-column align="center" label="创建时间" prop="createdTime" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <!-- 新增编辑客户弹窗 -->
    <Edit ref="edit" @createContact="createContact" @customerSave="customerSave" />
    <!-- 新建联系人弹窗 -->
    <Contact ref="contact" />
    <!-- 分配客户 -->
    <Allocate ref="allocate" />
  </div>
</template>

<script>
  import to from 'await-to-js'
  import api from '@/api/customer'
  import Contact from './components/Contact'
  import Edit from './components/Edit'
  import Allocate from './components/Allocate'

  export default {
    name: 'OpenSea',
    components: {
      Contact,
      Edit,
      Allocate,
    },
    data() {
      return {
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        list: [],
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          custCode: '', // 客户编码
          custName: '', //客户名称
          custIndustry: '', // 客户行业  ()
          custLevel: '', //客户级别
        },
        selectRows: [],
      }
    },
    mounted() {
      this.fetchData()
      this.getOptions()
    },
    methods: {
      getOptions() {
        Promise.all([api.getProvinceInfo()])
          .then(([province]) => {
            this.provinceOptions = province.data.list || []
          })
          .catch((err) => console.log(err))
      },
      async fetchData() {
        this.listLoading = true
        const params = { ...this.queryForm }
        const [err, res] = await to(api.getPublicList(params))
        if (err) return (this.listLoading = false)
        this.list = res.data.list || []
        this.total = res.data.total
        this.listLoading = false
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          custCode: '', // 客户编码
          custName: '', //客户名称
          custIndustry: '', // 客户行业  ()
          custLevel: '', //客户级别
        }
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      // 客户编辑
      async handleEdit(row) {
        this.$refs.edit.init([row.id])
      },
      // 客户详情
      handleDetail(row) {
        this.$router.push({
          path: '/customer/detail',
          query: {
            id: row.id,
          },
        })
      },
      // 客户删除
      handleDelete(row) {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const [err, res] = await to(api.deleteCustomer({ Id: row.id }))
            if (err) return
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.fetchData()
            }
          })
          .catch(() => {})
      },
      // 联系人弹窗
      createContact(res) {
        this.$refs.contact.contactForm.custId = res.id
        this.$refs.contact.contactForm.custName = res.name
        this.$refs.contact.contactVisible = true
      },
      customerSave() {
        this.fetchData()
      },
      handleClose(form) {
        this.$refs[form].resetFields()
      },
      // 领取
      handleReceive() {
        if (!this.selectRows.length) return this.$message.warning('请选择客户')
        const arr = this.selectRows.map((item) => item.id)

        this.$confirm('确认领取客户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const [err, res] = await to(api.receiveCustomer({ ids: arr.join() }))
            if (err) return
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '领取成功!',
              })
            }
          })
          .then(async () => {
            const [err, res] = await to(api.receiveCustomer({ ids: arr, salesId: 1 }))
            if (err) return
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '领取成功!',
              })
            }
          })
          .catch(() => {})
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base: '.open-sea';
</style>
