<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="clearfix">
        <span class="left-text">常用快递</span>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd()"
          style="float: right"
          size="mini"
          >新增</el-button
        >
      </div>

      <el-table :data="tableData" :height="tableHeight" style="width: 100%">
        <el-table-column
          prop="logistics_company_name"
          label="快递公司"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              style="color: red"
              @click="handleDelete(scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增或修改提示框 -->
    <el-dialog
      title="新增快递公司"
      :visible.sync="dialogVisible"
      center
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      @close="clearPayform"
      width="40%"
    >
      <div style="width: 100%; margin: 50px auto 200px">
        <el-form :model="addForm" label-width="150px" center size="mini">
          <el-form-item label="选择快递公司：" prop="express">
            <el-select
              style="width: 80%"
              v-model="addForm.express"
              multiple
              placeholder="请选择..."
            >
              <el-option
                v-for="item in scopeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="closeDialogVisible()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogDelete"
      center
      :close-on-click-modal="false"
      width="30%"
      class="dialogstyle"
    >
      <div style="text-align: center; margin: 0 auto">
        <p>确定要删除该快递公司吗？</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDelete = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="closeDialogDelete()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  goodsLogistics,
  delGoodsLogistics,
  addGoodsLogistics,
  getExpressListData,
} from "@/api/module/settings";
export default {
  name: "commodityExpress",
  data() {
    return {
      fullHeight: 0,
      tableHeight: 0,
      //表格内容
      tableData: [],
      code: 0,
      //新增表单内容
      addForm: {
        express: [],
      },
      //新增快递公司下拉框数据
      scopeOptions: [],
      //新增表单弹框
      dialogVisible: false,
      //删除弹框
      dialogDelete: false,
    };
  },
  created() {
    this.$nextTick(() => {
      this.fullHeight =
        document.getElementsByClassName("box-card")[0].clientHeight;
    });
  },
  watch: {
    fullHeight(val) {
      let formHeight =
        document.getElementsByClassName("clearfix")[0].clientHeight;
      this.tableHeight = val - formHeight - 100;
    },
  },
  mounted() {
    //获取快递公司列表
    this.getGoodsLogistics();
  },
  methods: {
    //删除分组
    handleDelete(val) {
      this.code = val.id;
      this.dialogDelete = true;
    },
    //新增快递公司
    handleAdd() {
      getExpressListData().then((res) => {
        if (res.code == 1) {
          this.scopeOptions = res.data.expressLists;
          this.dialogVisible = true;
        }
      });
    },
    //删除提示框
    closeDialogDelete() {
      let obj = {
        id: this.code,
      };
      delGoodsLogistics(obj).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.dialogDelete = false;
          this.getGoodsLogistics();
        }else {
          this.$message({
            message: res.message,
            type: "error",
          });
          this.dialogDelete = false;
        }
      });
    },
    //新增弹框确定回调
    closeDialogVisible() {
      let obj = {
        logistics_company: this.addForm.express,
      };
      addGoodsLogistics(obj).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.getGoodsLogistics();
          this.dialogVisible = false;
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
          this.dialogVisible = false;
        }
      });
    },
    //获取表格内容
    getGoodsLogistics() {
      goodsLogistics().then((res) => {
        if (res.code == 1) {
          this.tableData = res.data;
        }else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      });
    },
    //清空表单内容
    clearPayform() {
      this.addForm.express = "";
    },
  },
};
</script>
<style scoped>
.app-container {
  overflow: auto;
}
.clearfix {
  margin: 10px 0 15px 0;
}
.left-text {
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #333333;
  line-height: 28px;
}
</style>