<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="screenForm"
        :model="screenForm"
        label-width="120px"
        size="mini"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="物流公司名称" prop="customerTel">
              <el-input
                placeholder="请输入物流公司名称"
                v-model="screenForm.logisticsCompany"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="客服电话" prop="logisticsCompany">
              <el-input
                placeholder="请输入客服电话"
                v-model="screenForm.customerTel "
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="12" class="tr">
            <el-form-item label="">
              <el-button size="mini" @click="resetScreenForm">清空</el-button>
              <el-button size="mini" type="primary" @click="submitScreenForm"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="hanleAdd" v-if="$checkBtnRole('add', $route.meta.roles)"
          >新增</el-button
        >
        <el-popconfirm
        v-if="$checkBtnRole('del', $route.meta.roles)"
          confirm-button-text="好的"
          cancel-button-text="不用了"
          icon="el-icon-info"
          icon-color="red"
          title="内容确定删除吗？"
          @onConfirm="hanleDelete"
        >
          <el-button
            type="primary"
            size="mini"
            slot="reference"
            class="el-popover-left"
            >删除</el-button
          >
        </el-popconfirm>
      </div>
      <div class="fr">
        <!-- <ExportButton :exUrl="'/logistics/export'" :exParams="exParams" /> -->

        <el-button type="primary" size="mini" @click="handleExport"
          >导出</el-button
        >
        <!-- <el-button type="primary" size="mini" @click="hanlePrint"
          >打印</el-button
        > -->
      </div>
    </div>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          @select="hanleSelectAll"
          @select-all="hanleSelectAll"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column type="selection" align="center"></el-table-column>
          <el-table-column
            align="center"
            label="物流公司名称"
            prop="logisticsCompany"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="物流公司代码"
            prop="logisticsNumber"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="提货人"
            prop="pickName"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="提货人电话"
            prop="pickTel"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="客服电话"
            prop="customerTel"
            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="updateBy"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <!-- <el-button
                type="text"
                class="textColor"
                @click="hanleDetail(scope.row)"
                >详情</el-button
              > -->
              <el-button
                type="text"
                v-if="$checkBtnRole('edit', $route.meta.roles)"
                class="textColor"
                @click="editFn(scope.row.id, scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
              v-if="$checkBtnRole('del', $route.meta.roles)"
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                @onConfirm="hanleDelete(scope.row.id)"
              >
                <el-button
                  slot="reference"
                  type="text"
                  class="textColor el-popover-left"
                  >删除</el-button
                >
              </el-popconfirm>
            </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="showDialogForm"
      width="50%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :rules="type == 2 ? rules : ''"
        :model="diaLogForm"
        label-width="120px"
      >
        <el-form-item label="物流公司名称" prop="logisticsCompany">
          <el-input v-model="diaLogForm.logisticsCompany"></el-input>
        </el-form-item>
        <el-form-item label="物流公司代码" prop="logisticsNumber">
          <el-input v-model="diaLogForm.logisticsNumber"></el-input>
        </el-form-item>
        <el-form-item label="提货人" prop="pickName">
          <el-input v-model="diaLogForm.pickName"></el-input>
        </el-form-item>
        <el-form-item label="提货人电话" prop="pickTel">
          <el-input v-model="diaLogForm.pickTel"></el-input>
        </el-form-item>
        <el-form-item label="客服电话" prop="customerTel">
          <el-input v-model="diaLogForm.customerTel"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hanleCancel('form')">取 消</el-button>
        <el-button type="primary" @click="handelInfo('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Mixin from '@/mixin/index'
import { addInof, deleteInfo, getLogisticsList, updateInfo } from '@/api/basic_data/logistics'
import { downloadFiles } from '@/utils/util'

export default {
  mixins: [Mixin],
  data() {
    return {
      type: 1, // 1 ,2, 3
      diaLogForm: {
        id: null,
        customerTel: "",
        logisticsCompany: "",
        logisticsNumber: "",
        pickName: "",
        pickTel: "",
      },
      showDialogForm: false,
      screenForm: {
        name: "",
        username: "",
        telephone: "",
        customerTel: "",
        logisticsCompany: "",
      },
      ids: [],
      rules: {
        logisticsCompany: [
          { required: true, message: "请输入物流公司名称", trigger: "blur" },
        ],
        logisticsNumber: [
          { required: true, message: "请输入物流公司代码", trigger: "blur" },
        ],
        pickName: [
          { required: true, message: "请输入提货人", trigger: "blur" },
        ],
        pickTel: [
          { required: true, message: "请输入提货人电话", trigger: "blur" },
        ],
        customerTel: [
          { required: true, message: "请输入客服电话", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //显示新增弹框
    hanleAdd() {
      this.type = 2;
      this.showDialogForm = true;
    },
    //显示编辑，编辑数据初始化
    editFn(id, row) {
      this.type = 1;
      this.diaLogForm = {
        id,
        customerTel: row.customerTel,
        logisticsCompany: row.logisticsCompany,
        logisticsNumber: row.logisticsNumber,
        pickName: row.pickName,
        pickTel: row.pickTel,
      };
      this.showDialogForm = true;
    },
    // 初始化数据
    getList() {
      this.listLoading = true;
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        customerTel: this.screenForm.customerTel,
        logisticsCompany: this.screenForm.logisticsCompany,
      };
      getLogisticsList(params).then((res) => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
        console.log(this.dataList, 1233);
      });
    },
    //新增数据或者编辑数据请求接口  type 1 为编辑  type 2 新增
    handelInfo(formName) {
      if (this.type === 1) {
        const params = {
          ...this.diaLogForm,
        };
        updateInfo(params).then((res) => {
          this.$successMsg("编辑成功");
          this.showDialogForm = false;
          this.getList();
          this.hanleCancel();
        });
        this.diaLogForm.id = null;
      } else if (this.type === 2) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addInof(this.diaLogForm).then((res) => {
              this.$successMsg("保存成功");
              this.showDialogForm = false;
              this.hanleCancel();
              this.getList();
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        this.showDialogForm = false;
      }
    },
    //查看详情
    hanleDetail(detail) {
      this.type = 3;
      this.showDialogForm = true;
      this.diaLogForm = detail;
      this.hanleCancel();
    },
    // 导出文档
    handleExport() {
      let screenData = {
        customerTel: this.diaLogForm.customerTel,
        logisticsCompany: this.diaLogForm.logisticsCompany,
      };
      downloadFiles("/logistics/export", screenData);
    },

    // 删除数据
    hanleDelete(id) {
      this.hanleDeleteAllPromise(id).then((ids) => {
        deleteInfo(ids).then(() => {
          this.$successMsg("删除成功");
          this.getList();
        });
      });
    },
    // 取消按钮重置数据
    hanleCancel(form) {
      this.$refs['form'].resetFields()
      // this.diaLogForm = {
      //   id: null,
      //   customerTel: "",
      //   logisticsCompany: "",
      //   logisticsNumber: "",
      //   pickName: "",
      //   pickTel: "",
      // };
      this.showDialogForm = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.textColor {
  color: black;
}

::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>
