<template>
  <div class="app-container">
    <el-form v-model="pagerDto" inline>
      <el-form-item label="客户名称">
        <el-input v-model="pagerDto._Eq_CustomerName" clearable />
      </el-form-item>
      
      <el-button
        icon="el-icon-search"
        type="primary"
        @click="searchData"
        >搜索</el-button
      >
      <el-button
        icon="el-icon-circle-plus-outline"
        type="success"
        @click="showAddPanel"
        >新增</el-button
      >
    </el-form>
    <div>
      <el-table v-loading="loading" :data="sourceData" border>
        <el-table-column prop="CustomerName" label="客户名称" />
        <el-table-column prop="CreateUserName" label="创建人" />
        <el-table-column prop="CreateTime" label="创建时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" plain @click="remove(scope.row)"
              >移除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        :page-sizes="[100, 200, 300, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @current-change="pagerSerach"
      />
    </div>
    <el-dialog :visible.sync="showEditPannel">
      <el-form
        ref="editForm"
        v-loading="submiting"
        label-width="8em"
        :model="editItem"
        :rules="rules"
      >
        <el-form-item label="客户名称" prop="CustomerName">
          <el-input v-model="editItem.CustomerName" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editItem.Remark" />
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button size="large" type="success" @click="validtAndSubmit"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      loading: false,
      submiting: false,
      showEditPannel: false,
      editMode: "",
      editItem: {
        CustomerName: "",
        Remark: null,
      },
        rules: {
        CustomerName: { required: true, message: '请填写客户名称', trigger: 'blur' },
      },
      sourceData: [],
      pagerDto: {
        _Eq_CustomerName: null,
        PageIndex: 1,
        PageSize: 100,
        total: 0,
      },
    };
  },
  created() {
    this.pagerDto.PageIndex = 1;
    this.pagerDto._Eq_CustomerName = "";
    this.searchData();
  },
  methods: {
    searchData() {
      this.loading = true;
      request({
        url: "/FirstCustomer/query",
        method: "post",
        data: this.pagerDto,
      })
        .then((resp) => {
          this.loading = false;
          this.pagerDto.total = resp.TotalCount;
          this.pagerDto.PageIndex = resp.PageIndex;

          this.sourceData = resp.Result;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    remove(item) {
      this.$confirm("是否删除该客户?", "正在删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.loading = true;
        request({
          url: "/FirstCustomer/remove",
          params: { id: item.Id },
          method: "post",
        })
          .then((resp) => {
            console.log(resp);
            if (resp === true) {
              const idx = this.sourceData.findIndex((i) => i.Id === item.Id);
              this.sourceData.splice(idx, 1);
            } else {
              const msg = resp.message;
              this.$message(msg);
            }
          })
          .catch(() => {
            this.$message("删除出错");
          })
          .finally(() => {
            this.loading = false;
          });
      });
    },
    pagerSerach(p) {
        this.pagerDto.PageIndex =p;
        this.searchData();
    },
    initEditItem() {
       this.editItem.Id=0;
      this.editItem.CustomerName = "";
      this.editItem.Remark = "";
    },
    showAddPanel() {
      this.editMode = "add";
      this.initEditItem();
      this.showEditPannel = true;
    },
    submit() {
      let url = "/FirstCustomer/create";
      let op = "新增";
      this.submiting = true;
      request({
        url: url,
        data: this.editItem,
        method: "post",
      })
        .then((resp) => {
          if (resp.success === true) {
            this.showEditPannel = false;
            this.initEditItem();
            this.$alert(`${op}成功`, `${op}成功`, {
              confirmButtonText: "确定",
              callback: () => {
                this.searchData();
              },
            });
          } else {
            const msg = resp.message;
            this.$message(msg);
          }
        })
        .finally(() => {
          this.submiting = false;
        });
    },
    validtAndSubmit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.submit();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
</style>
