<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="100px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商名称" prop="customerName">
              <el-input
                v-model="searchForm.customerName"
                placeholder="请输入提货人姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="提货人姓名" prop="takerName">
              <el-input
                v-model="searchForm.takerName"
                placeholder="请输入提货人姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="电话" prop="telephone">
              <el-input
                v-model="searchForm.telephone"
                placeholder="请输入电话"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="clearFn">清空</el-button>
              <el-button size="small" type="primary" @click="searchFn"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button type="primary" size="small" @click="addFn">新增</el-button>
      </div>
      <div class="fr">
        <el-button type="primary" size="small" @click="exportFn"
          >导出</el-button
        >
        <el-button type="primary" size="small">打印</el-button>
      </div>
    </div>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="提货人姓名"
            prop="takerName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="身份证"
            prop="identity"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="联系电话"
            prop="phone"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="备用电话"
            prop="sparePhone"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="有效期"
            prop="expireTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建人"
            prop="createBy"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="状态"
            prop="flag"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.flag === 1">有效</el-tag>
              <el-tag type="danger" v-else-if="scope.row.flag === 2"
                >失效</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
            prop="caozuo"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope"
              ><el-button
                type="text"
                class="textColor"
                @click="seeFn(scope.row)"
                >查看</el-button
              >
              <el-popconfirm
                @onConfirm="deleFn(scope.row.id)"
                title="这是一段内容确定删除吗？"
              >
                <el-button type="text" class="textColor" slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
              <el-button
                type="text"
                class="textColor"
                @click="stopFn(scope.row.id)"
                v-if="scope.row.flag === 1"
                >停用</el-button
              >
              <el-button
                type="text"
                class="textColor"
                @click="rebuild(scope.row)"
                v-else-if="scope.row.flag === 2"
                >重建</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      title="提货人档案"
      :visible.sync="dialogForm"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addForm"
        :rules="rules"
        :model="addForm"
        label-width="120px"
      >
        <el-form-item label="经销商名称" prop="customerId">
          <el-select v-model="addForm.customerId" placeholder="名称">
            <el-option
              v-for="item in dealerList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提货人姓名" prop="takerName">
              <el-input v-model="addForm.takerName"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="身份证号码" prop="identity">
              <el-input v-model="addForm.identity"></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="addForm.phone"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="备用电话" prop="sparePhone">
              <el-input v-model="addForm.sparePhone"></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="有效期" prop="expireTime">
              <el-date-picker
                v-model="addForm.expireTime"
                type="date"
                placeholder="选择日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="验证码" prop="code">
              <el-row :gutter="6">
                <el-col :span="14">
                  <el-input v-model="addForm.code" />
                </el-col>
                <el-col :span="9">
                  <el-button type="info" @click="getCodeFn"
                    >获取验证码</el-button
                  >
                </el-col>
              </el-row>
            </el-form-item></el-col
          >
        </el-row>
        <el-form-item label="上传提货人附件" prop="fileUrl">
          <fileUpload :fileList="fileList" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button v-if="showOK" type="primary" @click="addDataFn"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  addDataList,
  getCodeApi,
  stopTaker,
  deleDataList,
} from "@/api/basic_data/taker";
import { getDealerList } from "@/api/basic_data/dealer";
import { downloadFiles } from "@/utils/util";
import fileUpload from "@/components/Common/file-upload.vue";
export default {
  components: {
    fileUpload,
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      fileList: [],
      listLoading: false, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      addForm: {
        customerId: "",
        takerName: "",
        identity: "",
        phone: "",

        sparePhone: "",
        expireTime: "",
        code: "",
      },

      rules: {
        customerId: [
          { required: true, message: "请选择经销商", trigger: "blur" },
        ],
        takerName: [
          { required: true, message: "请输入提货人姓名", trigger: "blur" },
        ],
        identity: [
          {
            required: true,

            message: "请输入身份证号码",
            trigger: "blur",
          },
          {
            pattern:
              /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            message: "身份证号格式不正确",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur",
          },
          {
            required: true,
            pattern:
              /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/,
            message: "联系电话格式不正确",
            trigger: "blur",
          },
        ],
        sparePhone: [
          {
            required: true,
            pattern:
              /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/,
            message: "请输入备用电话",
            trigger: "blur",
          },
        ],
        expireTime: [
          { required: true, message: "有效期不能为空", trigger: "blur" },
        ],
        code: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
      },

      dialogForm: false,
      dataList: [],
      searchForm: {
        customerName: "",
        takerName: "",
        telephone: "",
      },
      dealerList: [],
      id: "", //重建之前的Id
      showOK: true,
    };
  },
  async created() {
    await this.getDataList({ pageNum: 1, pageSize: 10 });
    await this.getDealerDataList({ pageNum: 1, pageSize: 10 });
  },
  methods: {
    //重建
    rebuild(data) {
      this.showOK = true;
      this.id = data.id;
      this.addForm = {
        customerId: data.customerId,
        takerName: data.takerName,
        identity: data.identity,
        phone: data.phone,

        sparePhone: data.sparePhone,
        expireTime: "",
        code: "",
      };
      this.fileList = [];
      this.dialogForm = true;
    },
    //删除
    async deleFn(ids) {
      await deleDataList({ ids });
      this.$message.success("删除成功");
      this.getDataList({ pageNum: 1, pageSize: 10 });
    },
    //查看
    seeFn(data) {
      this.showOK = false;
      this.addForm = data;
      this.fileList = [];
      this.dialogForm = true;
    },

    //取消
    cancelFn() {
      this.addForm = {
        customerId: "",
        takerName: "",
        identity: "",
        phone: "",

        sparePhone: "",
        expireTime: "",
        code: "",
      };
      this.$refs.addForm.resetFields();
      this.dialogForm = false;
    },
    //上传
    handlefile(response, file, fileList) {
      console.log(response, file, fileList);
    },
    //导出
    exportFn() {
      let screenData = {
        customerName: this.searchForm.customerName,
        takerName: this.searchForm.takerName,
      };
      downloadFiles("/taker-record/export", screenData);
    },
    //停用提货人档案
    async stopFn(id) {
      await stopTaker({ id });
      this.$message.success("成功");
      this.getDataList({ pageNum: 1, pageSize: this.pageSize });
    },
    //获取验证码
    async getCodeFn() {
      const res = await getCodeApi({ mobile: this.addForm.phone });
      console.log(res);
    },
    //获取经销商数据
    async getDealerDataList(data) {
      const res = await getDealerList(data);
      this.dealerList = res.data.records;
    },
    //获取列表数据
    async getDataList(data) {
      let res = await getList(data);
      console.log(res);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
    //搜索功能
    async searchFn() {
      console.log(this.searchForm);
      await this.getDataList({ ...this.searchForm, pageNum: 1, pageSize: 10 });
    },
    //重置
    clearFn() {
      console.log(this.$refs.searchForm);
      this.$refs.searchForm.resetFields();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getDataList({ pageNum: 1, pageSize: this.pageSize });
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList({ pageNum: val, pageSize: 10 });
    },
    //新增
    async addDataFn() {
      console.log(this.fileList);

      await this.$refs.addForm.validate();

      const res = this.dealerList.filter(
        (v) => v.id === this.addForm.customerId
      )[0];
      console.log(res);

      if (this.fileList.length == 1) {
        await addDataList({
          ...this.addForm,
          orgNumber: res.useOrgNumber,

          fileName: this.fileList[0].name,
          fileUrl: this.fileList[0].url,
        });
      } else {
        await addDataList({
          ...this.addForm,
          orgNumber: res.useOrgNumber,

          fileName: "",
          fileUrl: "",
        });
      }

      if (this.id) {
        await deleDataList({ ids: this.id });
      }

      this.$message.success("新增成功");
      this.id = "";
      // this.addForm = {
      //   customerId: "",
      //   takerName: "",
      //   identity: "",
      //   phone: "",

      //   sparePhone: "",
      //   expireTime: "",
      //   code: "",
      // };
      this.getDataList({ pageNum: 1, pageSize: 10 });
      this.dialogForm = false;
    },
    addFn() {
      this.addForm = {
        customerId: "",
        takerName: "",
        identity: "",
        phone: "",

        sparePhone: "",
        expireTime: "",
        code: "",
      };
      this.fileList = [];
      this.showOK = true;
      this.dialogForm = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor {
  width: 100%;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-col-9 .el-button {
  padding: 5px;
}
::v-deep .el-dialog__header {
  background-color: #dddddd;
}
.base {
  padding: 20px 20px 0;
}
.table {
  margin-top: 12px;
}
.right {
  float: right;
}
</style>