contactsId<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="门店编号" v-model="listQuery.storeCode">
      </el-input>
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="门店名称" v-model="listQuery.storeName">
      </el-input>
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="联系人" v-model="listQuery.contactsId">
      </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleQuery">查找</el-button>
      <el-button class="filter-item" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">添加</el-button>
      <el-button class="filter-item" v-waves icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit
      highlight-current-row>

      <el-table-column type="index" label="序号" header-align="center" align="center">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="门店编号" prop="storeCode">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="门店名称" prop="storeName">
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="联系人" prop="contactsId">
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="联系电话" prop="contactsPhone">
      </el-table-column>

      <el-table-column align="center" min-width="200px" label="门店地址" prop="storeAddress">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="创建时间" prop="createDate">
      </el-table-column>

      <!-- <el-table-column align="center" min-width="150px" label="备注" prop="remarks">
      </el-table-column> -->

      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="600px">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px">
        <el-form-item label="门店编号" prop="storeCode">
          <el-input v-model="dataForm.storeCode" placeholder="请输入门店编号"></el-input>
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input v-model="dataForm.storeName" placeholder="请输入门店名称"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contactsId">
          <el-input v-model="dataForm.contactsId" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactsPhone">
          <el-input v-model="dataForm.contactsPhone" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="门店地址" prop="storeAddress">
          <el-input v-model="dataForm.storeAddress" placeholder="请输入门店地址"></el-input>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入备注"
            v-model="dataForm.remarks">
          </el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { listStore, createStore, updateStore, deleteStore } from '@/api/store'
import waves from '@/directive/waves' // 水波纹指令

export default {
  directives: { waves },
  data() {
    return {
      list: undefined,
      total: undefined,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        storeCode: undefined,
        storeName: undefined,
        contactsId: undefined,
      },
      dataForm: {
        storeCode: undefined,
        storeName: undefined,
        contactsId: undefined,
        contactsPhone: undefined,
        storeAddress: undefined,
        remarks: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑门店',
        create: '添加门店'
      },
      rules: {
        storeCode: [
          { required: true, message: "门店编号不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "门店编号必须为11位", trigger: "blur" },
          { pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{11,}$/, message: "门店编号必须为11位字母加数字", trigger: "blur" }
        ],
        storeName: [{ required: true, message: '门店名称不能为空', trigger: 'blur' }],
        contactsId: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
        contactsPhone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
        storeAddress: [{ required: true, message: '门店地址不能为空', trigger: 'blur' }]
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 10,
        storeCode: undefined,
        storeName: undefined,
        contactsId: undefined,
      };
      this.handleQuery();
    },
    getList() {
      this.listLoading = true
      listStore(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleQuery() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        storeCode: undefined,
        storeName: undefined,
        contactsId: undefined,
        contactsPhone: undefined,
        storeAddress: undefined,
        remarks: undefined,
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createStore(this.dataForm).then(response => {
            this.list.unshift(response.data.data)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateStore(this.dataForm).then(() => {
            for (const v of this.list) {
              if (v.id === this.dataForm.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.dataForm)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteStore({id:row.id}).then(response => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }).catch(() => {
  
        })
    },
    handleDownload() {
      window.location.href = process.env.BASE_API + '/store-info/export';
    },
  }
}
</script>