<template>
  <!-- 名额限制 -->
  <template-page
    ref="pageRef"
    :get-list="getList"
    :export-list="exportList"
    :operation="operation()"
    :options-evens-group="optionsEvensGroup"
    :column-parsing="columnParsing"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
  >
    <el-dialog
      title="商家登录名称限制"
      :visible.sync="visible"
      width="40%"
      :append-to-body="true"
      :close-on-click-modal="false"
      @close="handleClose"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="150px"
        :inline="false"
        size="mini"
        :disabled="isDetail"
      >
        <el-form-item label="商家信息" prop="customerId">
          <el-select
            v-model="formData.customerId"
            placeholder="请选择"
            clearable
            filterable
            remote
            reserve-keyword
            :remote-method="remoteMethod"
            :loading="loading"
            style="width: 100%"
            @change="handleChange"
          >
            <el-option v-for="item in customerList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="商家编号" prop="customerNumber">
          <el-input v-model="formData.customerNumber" placeholder="根据选择自动带出商家编号" readonly />
        </el-form-item>
        <el-form-item label="工装登录名额限制" prop="workLoginLimit">
          <el-input v-model.number="formData.workLoginLimit" type="number" placeholder="请输入正整数" />
        </el-form-item>
        <el-form-item label="家装登录名额限制" prop="homeLoginLimit">
          <el-input v-model.number="formData.homeLoginLimit" type="number" placeholder="请输入正整数" />
        </el-form-item>
        <el-form-item label="跨区登录名额限制" prop="spanLoginLimit">
          <el-input v-model.number="formData.spanLoginLimit" type="number" placeholder="请输入正整数" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="formData.state">
            <el-radio
              v-for="item in [
                { label: '开启', vlaue: true },
                { label: '停用', vlaue: false }
              ]"
              :key="item.key"
              :label="item.vlaue"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" :disabled="isDetail" @click="onSubmit">确定</el-button>
      </span>
    </el-dialog>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import { getDealerListV2 } from '@/api/basic_data/dealer'
import {
  getMerchantLoginQuotaLimitList,
  exportMerchantLoginQuotaLimit,
  delMerchantLoginQuotaLimit,
  editMerchantLoginQuotaLimit,
  addMerchantLoginQuotaLimit,
  getMerchantLoginQuotaLimitDetail
} from '@/api/basic_data/numerusClausus'

export default {
  components: { TemplatePage },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      loading: false,
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.formData.id = ''
                this.getDealerListV2()
                this.visible = true
              })
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async () => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                const ids = this.recordSelected.map(v => {
                  return v.id
                })
                const idList = ids
                await delMerchantLoginQuotaLimit(idList)
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              }
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      detailsId: '',
      formData: {
        id: '',
        customerId: '',
        customerNumber: '',
        customerName: '',
        homeLoginLimit: 0,
        spanLoginLimit: 0,
        state: true,
        workLoginLimit: 0
      },
      customerList: [],
      rules: {
        customerId: [{ required: true, message: '请选择', trigger: 'change' }],
        customerNumber: [{ required: true, message: '请填写', trigger: 'change' }],
        homeLoginLimit: [{ required: true, message: '请填写', trigger: 'change' }],
        spanLoginLimit: [{ required: true, message: '请填写', trigger: 'change' }],
        workLoginLimit: [{ required: true, message: '请填写', trigger: 'change' }],
        state: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      isDetail: false
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getMerchantLoginQuotaLimitList(...p)
    },
    // 列表导出函数
    exportList: exportMerchantLoginQuotaLimit,
    // 表格列解析渲染数据更改
    columnParsing(_item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (_h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            <el-button
              size="mini"
              type="text"
              onClick={() => {
                this.getDealerListV2()
                this.getMerchantLoginQuotaLimitDetail(row.id)
                this.visible = true
              }}
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              onClick={() => {
                this.getDealerListV2()
                this.getMerchantLoginQuotaLimitDetail(row.id)
                this.isDetail = true
                this.visible = true
              }}
            >
              详情
            </el-button>
            {
              <el-popconfirm
                title="确定删除吗？"
                onConfirm={() => {
                  delMerchantLoginQuotaLimit([row.id]).then(res => {
                    this.$refs.pageRef.refreshList()
                    this.$successMsg('删除成功')
                  })
                }}
              >
                <el-button slot="reference" type="text" size="mini">
                  删除
                </el-button>
              </el-popconfirm>
            }
          </div>
        )
      }
    },
    handleClose() {
      this.addOff(() => {
        this.$refs.formData.resetFields()
        this.visible = false
        this.isDetail = false
        this.$refs.pageRef.refreshList()
      })()
    },
    getMerchantLoginQuotaLimitDetail(id) {
      getMerchantLoginQuotaLimitDetail({ id }).then(res => {
        this.formData = {
          ...res.data
        }
      })
    },
    remoteMethod(e) {
      this.loading = true
      this.getDealerListV2(e)
    },
    getDealerListV2(e) {
      let params = []
      if (e) {
        params = [
          {
            param: 'a.name',
            compare: 'like',
            value: e
          }
        ]
      }
      getDealerListV2({
        pageNum: 1,
        pageSize: 200,
        params
      }).then(res => {
        this.customerList = res.data.records
        this.loading = false
      })
    },
    handleChange(e) {
      if (e) {
        const item = this.customerList.find(k => k.id === e)
        this.formData.customerNumber = item.number
        this.formData.customerName = item.name
      } else {
        this.formData.customerNumber = ''
        this.formData.customerName = ''
      }
    },
    onSubmit() {
      this.$refs.formData.validate(valid => {
        const params = {
          ...this.formData
        }
        if (valid) {
          if (this.formData.id) {
            editMerchantLoginQuotaLimit(params).then(res => {
              this.visible = false
              this.$successMsg('编辑成功')
            })
          } else {
            addMerchantLoginQuotaLimit(params).then(res => {
              this.visible = false
              this.$successMsg('新增成功')
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
