<template>
  <div class="customer-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-radio-group v-model="queryForm.comment_is_reply" @change="queryData">
              <el-radio-button v-for="item in stateOptions" :key="item.value" :label="item.value" >{{item.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入用户昵称')"
              remote
              :remote-method="remoteMethod"
              reserve-keyword
            >
              <el-option
                v-for="item in accountOptions"
                :key="item.user_id"
                :label="item.user_nickname"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('客户编号')"
        prop="customer_number"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户名称')"
        prop="customer_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户账号')"
        prop="user_account"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户类别')"
        prop="customer_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户等级')"
        prop="customer_level_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('税率')"
        prop="customer_tax_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('备注消息')"
        prop="customer_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属业务员')"
        prop="seller_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('开户银行')"
        prop="customer_bank"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('银行账号')"
        prop="customer_card"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('开户名称')"
        prop="customer_account_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('纳税人识别号')"
        prop="customer_taxnum"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('发票抬头')"
        prop="customer_invoice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人')"
        prop="customer_contacter_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人IM/MSN/QQ')"
        prop="customer_contacter_im"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('联系人手机')"
        prop="customer_contacter_mobile"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('联系人座机')"
        prop="customer_contacter_telephone"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('联系人区域')"
        prop="customer_contacter_district_ids"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('联系人地址')"
        prop="customer_contacter_address"
        show-overflow-tooltip
        width="120"
      />
    </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" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, getList } from '@/api/customer/base'
  import Edit from './components/CustomerBaseEdit'
  import { getAccounts } from '@/api/user/base'
  import VabQueryFormLeftPanel from '@/vab/components/VabQueryForm/components/VabQueryFormLeftPanel'

  export default {
    name: 'CustomerBase',
    components: { VabQueryFormLeftPanel, Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        accountOptions: [],
        list: [],
        loading: true,
        listLoading: true,
        restaurants: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          user_account: '',
        },
      }
    },
    created() {
      this.fetchData(), this.functionBase()
    },
    methods: {
      __,

      handleEdit(row) {
        if (row.customer_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },

      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            customer_id: row.customer_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },

      async functionBase() {
        // 发请求获取渠道下拉框的值
        await getAccounts()
          .then((res) => {
            this.restaurants = res.data // 把获取到的数据赋给this.restaurants
          })
          .catch((err) => {})
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
