<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

<!--    上方按钮暂时不用-->
<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:BONDED:add']"-->
<!--          >注册</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:BONDED:edit']"-->
<!--          >修改</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:BONDED:remove']"-->
<!--          >删除</el-button-->
<!--        >-->
<!--      </el-col>-->
<!--      <right-toolbar-->
<!--        :showSearch.sync="showSearch"-->
<!--        @queryTable="getList"-->
<!--      ></right-toolbar>-->
<!--    </el-row>-->

    <el-table
      :data="BONDEDList"
      @selection-change="handleSelectionChange"
    >
<!--      <el-table-column type="selection" width="55" align="center" ></el-table-column>-->
      <el-table-column label="保税区名称" align="center" prop="deptName"></el-table-column>
      <el-table-column label="产品定位" align="center" prop="industryOrientation" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.industryOrientation }}
        </template>
      </el-table-column>
      <el-table-column label="规划面积（平方公里）" align="center" prop="planningArea">
        <template slot-scope="scope">
          {{ scope.row.planningArea }}
        </template>
      </el-table-column>
      <el-table-column label="验收面积（平方公里）" align="center" prop="acceptanceArea">
        <template slot-scope="scope">
          {{ scope.row.acceptanceArea }}
        </template>
      </el-table-column>
      <el-table-column label="批复时间" align="center" prop="approvalTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已使用面积（万平方米）" align="center" prop="useArea">
        <template slot-scope="scope">
          {{ scope.row.useArea }}
        </template>
      </el-table-column>
      <el-table-column label="使用率" align="center" prop="usageRate">
        <template slot-scope="scope">
          {{ scope.row.usageRate }}
        </template>
      </el-table-column>
      <el-table-column label="标仓面积（万平方米）" align="center" prop="warehouse">
        <template slot-scope="scope">
          {{ scope.row.warehouse }}
        </template>
      </el-table-column>
      <el-table-column label="油库面积（万平方米）" align="center" prop="oilDepot">
        <template slot-scope="scope">
          {{ scope.row.oilDepot }}
        </template>
      </el-table-column>
      <el-table-column label="冷库面积（万平方米）" align="center" prop="refrigeratory">
        <template slot-scope="scope">
          {{ scope.row.refrigeratory }}
        </template>
      </el-table-column>
      <el-table-column label="厂房面积（万平方米）" align="center" prop="workshop">
        <template slot-scope="scope">
          {{ scope.row.workshop }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.deptId)"
            v-hasPermi="['business:BONDED:edit']"
            >修改</el-button
          >
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row.deptId)"-->
<!--            v-hasPermi="['business:BONDED:remove']"-->
<!--            >删除</el-button-->
<!--          >-->
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total > 0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改保税区管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="50%" append-to-body novalidate>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="保税区名称" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入保税区名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品定位" prop="industryOrientation">
              <el-input v-model="form.industryOrientation" placeholder="请输入" type="textarea" autosize></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规划面积" prop="planningArea">
              <el-input type="number" v-model="form.planningArea" placeholder="请输入（平方公里）"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="验收面积" prop="acceptanceArea">
              <el-input type="number" v-model="form.acceptanceArea" placeholder="请输入（平方公里）"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="已使用面积" prop="useArea">
              <el-input type="number" v-model="form.useArea" placeholder="请输入（万平方米）"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标仓面积" prop="warehouse">
              <el-input type="number" v-model="form.warehouse" placeholder="请输入（万平方米）"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="油库面积" prop="oilDepot">
              <el-input type="number" v-model="form.oilDepot" placeholder="请输入（万平方米）"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷库面积" prop="refrigeratory">
              <el-input type="number" v-model="form.refrigeratory" placeholder="请输入（万平方米）"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="厂房面积" prop="workshop">
              <el-input type="number" v-model="form.workshop" placeholder="请输入（万平方米）"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批复时间" prop="approvalTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.approvalTime"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"></el-col>
          <el-col :span="12"></el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(form)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBONDED,
  getBONDED,
  delBONDED,
  addBONDED,
  updateBONDED,
} from "@/api/portal/BONDED/BONDED.js";

export default {
  name: "BONDED",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保税区管理表格数据
      BONDEDList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        bondedName: null,
        yardNumber: null,
        establishedTime: null,
        site: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptName: [{ required: true, message: "保税区名称不能为空", trigger: "blur" },],
        industryOrientation: [{ required: true, message: "产业定位不能为空", trigger: "blur" }],
        planningArea: [{ required: true, message: "规划面积不能为空", trigger: "blur" }],
        acceptanceArea: [{ required: true, message: "验收面积不能为空", trigger: "blur" }],
        useArea: [{ required: true, message: "已使用面积不能为空", trigger: "blur" }],
        usageRate: [{ required: true, message: "使用率不能为空", trigger: "blur" }],
        warehouse: [{ required: true, message: "仓库面积不能为空", trigger: "blur" }],
        oilDepot: [{ required: true, message: "油库面积不能为空", trigger: "blur" }],
        refrigeratory: [{ required: true, message: "冷库面积不能为空", trigger: "blur" }],
        workshop: [{ required: true, message: "厂房面积不能为空", trigger: "blur" }],
        approvalTime: [{ required: true, message: "批复时间不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保税区管理列表 */
    getList() {
      this.loading = true;
      listBONDED(this.queryParams).then((response) => {
        this.BONDEDList = response;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        bondedName: null,
        yardNumber: null,
        establishedTime: null,
        site: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 注册按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保税区管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids;
      getBONDED(row).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保税区信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 验证输入是否是有效的数字
      const isNumber = (value) => !isNaN(parseFloat(value)) && isFinite(value);
      this.form.useArea = parseFloat(this.form.useArea)

      if (isNumber(this.form.acceptanceArea) &&
        isNumber(this.form.useArea) &&
        isNumber(this.form.warehouse) &&
        isNumber(this.form.oilDepot) &&
        isNumber(this.form.refrigeratory) &&
        isNumber(this.form.workshop) &&
        isNumber(this.form.planningArea)) {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.deptId != null) {
              updateBONDED(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBONDED(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      } else {
        // 处理无效输入的逻辑
        this.$modal.msgError("请输入有效的数字");
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delBONDED(row);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/BONDED/export",
        {
          ...this.queryParams,
        },
        `BONDED_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.el-tooltip__popper{font-size: 14px; max-width:40% }/* 设置显示隐藏部分内容，按40%显示 */
</style>
