<template>
  <div style="margin-top: 10px;">
    <!-- 搜索工作栏 -->
    <el-form v-show="showSearch" ref="customeForm" :model="customeParams" size="small" :inline="true" label-width="100px">
      <el-form-item :label="$t('customer.group')" prop="companyZh">
        <el-select v-model="customeParams.pid">
          <el-option
            v-for="item in groupList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('customer.name')" prop="name">
        <el-input v-model="customeParams.name" :placeholder="$t('customer.nameNotice')" clearable />
      </el-form-item>
      <el-form-item :label="$t('customer.company')" prop="company">
        <el-input v-model="customeParams.company" :placeholder="$t('customer.companyNotice')" clearable />
      </el-form-item>
      <el-form-item :label="$t('customer.phone')" prop="phone">
        <el-input v-model="customeParams.phone" :placeholder="$t('customer.phoneNotice')" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('common.reload') }}</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{ $t('common.add') }}</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <div style="margin-top: 16px">
      <el-table
        :data="customerList"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="index"
          width="70"
          :label="$t('customer.index')"
          align="center"
          header-align="center"
        />
        <el-table-column
          :label="$t('customer.groupBy')"
          width="80"
          align="center"
          header-align="center"
        ><template slot-scope="scope">
          <span>{{ groupList.find(item=>item.id==scope.row.pid)?groupList.find(item=>item.id==scope.row.pid).title:'' }}</span>
        </template></el-table-column>
        <el-table-column
          prop="name"
          :label="$t('customer.name')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="company"
          :label="$t('customer.company')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="phone"
          :label="$t('customer.phone')"
          align="center"
          header-align="center"
        />
        <el-table-column
          :label="$t('customer.createTime')"
          align="center"
          header-align="center"
        ><template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template></el-table-column>
        <el-table-column :label="$t('common.action')" align="center" header-align="center" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >{{ $t('common.update') }}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >{{ $t('common.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="customeParams.page"
      :limit.sync="customeParams.rows"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="126px">
        <el-form-item :label="$t('customer.name')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('customer.nameNotice')" />
        </el-form-item>
        <el-form-item :label="$t('customer.company')" prop="company">
          <el-input v-model="form.company" :placeholder="$t('customer.companyNotice')" />
        </el-form-item>
        <el-form-item :label="$t('customer.maitou')" prop="maitou">
          <el-input v-model="form.maitou" :placeholder="$t('customer.maitouNotice')" />
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item :label="$t('customer.phone')" prop="dialCode">
              <el-select v-model="form.dialCode">
                <el-option
                  v-for="item in countryList"
                  :key="item.id"
                  :label="'+'+item.tel+' '+item.nameZh"
                  :value="item.tel*1"
                >
                  <span>{{ '+'+item.tel + ' ' + item.nameZh }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item prop="phone">
              <span slot="label" />
              <el-input v-model="form.phone" :placeholder="$t('customer.phoneNotice')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label="$t('customer.email')" prop="email">
          <el-input v-model="form.email" :placeholder="$t('customer.emailNotice')" />
        </el-form-item>
        <el-form-item :label="$t('customer.address')" prop="address">
          <el-input v-model="form.address" :placeholder="$t('customer.addressNotice')" />
        </el-form-item>
        <el-form-item :label="$t('customer.groupBy')" prop="pid">
          <el-select v-model="form.pid">
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('customer.default')">
          <el-radio v-model="form.defaultContact" :label="true">{{ $t('customer.yes') }}</el-radio>
          <el-radio v-model="form.defaultContact" :label="false">{{ $t('customer.no') }}</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('customer.submit') }}</el-button>
        <el-button @click="cancel">{{ $t('customer.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomer, getCustomerList, createCustomer, updateCustomer, deleteCustomer, getGroupList } from '@/api/customer'
import { getCountryList } from '@/api/user'
import { validatorPhone } from '@/api/data'
import { parseTime } from '@/utils'
import RightToolbar from '@/components/RightToolbar'
import Pagination from '@/components/Pagination'
export default {
  components: {
    RightToolbar,
    Pagination
  },
  data() {
    return {
      parseTime,
      customerList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      customeParams: {
        page: 1,
        rows: 10,
        phone: null,
        name: null,
        company: null,
        pid: null
      },
      countryList: [],
      title: '',
      // 是否显示弹出层
      open: false,
      groupList: [],
      form: {
        defaultContact: false
      },
      rules: {
        name: [{ required: true, message: this.$t('customer.nameRules'), trigger: 'blur' }],
        company: [{ required: true, message: this.$t('customer.companyRules'), trigger: 'blur' }],
        phone: [{ required: true, message: this.$t('customer.phoneRules1'), trigger: 'blur' }, { pattern: /^[0-9]\d*$/, message: this.$t('customer.phoneRules2') }],
        dialCode: [{ required: true, message: this.$t('customer.dialCode'), trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getGroup()
    getCountryList().then(r => {
      this.countryList = r.data
    })
  },
  methods: {
    getList() {
      getCustomerList(this.customeParams).then((r) => {
        this.customerList = r.data.list
        this.total = r.data.total
      })
    },
    getGroup() {
      getGroupList({ page: 1, rows: 100 }).then(r => {
        this.groupList = r.data.list
        this.groupList.push({ id: 0, title: this.$t('customer.none') })
      })
    },
    handleAdd() {
      this.reset()
      this.getGroup()
      this.open = true
      this.title = this.$t('customer.addCustomer')
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        defaultContact: false
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.customeParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.customeParams = {}
      this.handleQuery()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        validatorPhone({ code: this.form.dialCode, mobile: this.form.phone }).then(res => {
          if (res.data) {
            // 修改的提交
            if (this.form.id != null) {
              updateCustomer(this.form).then(response => {
                this.$message({
                  message: this.$t('common.success'),
                  type: 'success'
                })
                this.open = false
                this.getList()
              })
              return
            }
            // 添加的提交
            createCustomer(this.form).then(response => {
              this.$message({
                message: this.$t('common.success'),
                type: 'success'
              })
              this.open = false
              this.getList()
            })
          } else {
            this.$message.error(this.$t('手机号验证不通过'))
          }
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getCustomer({ id: id }).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('customer.updateCustomer')
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$confirm(this.$t('customer.deleteCustomer')).then(function() {
        return deleteCustomer(id)
      }).then(() => {
        this.getList()
        this.$message({
          message: this.$t('common.success'),
          type: 'success'
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
::v-deep .el-table__header .el-table__cell {
  background-color: #27417c;
  font-size: 13px;
  height: 38px;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  color: #ffffff;
}
</style>