<template>
  <div class="app-container">

    <!-- 操作工具栏 -->
    <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="['ecw:dock:create']">{{$t('新增')}}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['ecw:dock:export']">{{$t('导出')}}</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('序号')" type="index" width="50" />

      <el-table-column :label="$t('中文名称')" align="center" prop="titleZh" />
      <el-table-column :label="$t('英文名称')" align="center" prop="titleEn" />

      <el-table-column prop="portType" :label="$t('港口类型')" align="center">
          <template slot-scope="scope">
            <div>
              {{ portTypeName(scope.row.portType) }}
            </div>
          </template>
      </el-table-column>

      <el-table-column :label="$t('国家')" align="center" prop="countryZh">
        <template slot-scope="{row}">
          {{$l(row,'country')}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('省')" align="center" prop="provinceZh">
        <template slot-scope="{row}">
          {{$l(row,'province')}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('城市')" align="center" prop="cityZh">
        <template slot-scope="{row}">
          {{$l(row,'city')}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('码头编号')" align="center" prop="number" />
      <el-table-column :label="$t('码头类型')" align="center" prop="type" />
      <el-table-column :label="$t('排序')" align="center" prop="sort" />
      <el-table-column :label="$t('操作')" 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)"
                     v-hasPermi="['ecw:dock:update']">{{$t('修改')}}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ecw:dock:delete']">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item :label="$t('中文名称')" prop="titleZh">
          <el-input v-model="form.titleZh" :placeholder="$t('请输入中文名称')" />
        </el-form-item>
        <el-form-item :label="$t('英文名称')" prop="titleEn">
          <el-input v-model="form.titleEn" :placeholder="$t('请输入英文名称')" />
        </el-form-item>
        <el-form-item :label="$t('排序')" prop="sort">
          <el-input v-model="form.sort" :placeholder="$t('请输入排序')" />
        </el-form-item>

      
        <el-form-item :label="$t('国家')" prop="countryId">
          <el-select v-model="form.countryId" :placeholder="$t('请选择国家')" @change="changeCountry">
            <el-option
              v-for="item in countryList"
              :key="item.id"
              :label="$l(item,'title')"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item :label="$t('省')" prop="provinceId">
          <el-select v-model="form.provinceId" :placeholder="$t('请选择省')" @change="changeProvince">
            <el-option
              v-for="item in provinceList"
              :key="item.id"
              :label="$l(item,'title')"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item :label="$t('城市')" prop="cityId">
          <el-select v-model="form.cityId" :placeholder="$t('请选择城市')" @change="changeCity">
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="$l(item,'title')"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('港口类型')" prop="portType">
          <el-select v-model="form.portType" :placeholder="$t('请选择')">
            <el-option v-for="dict in portTypeDatas" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
      
        <el-form-item :label="$t('码头编号')" prop="number">
          <el-input v-model="form.number" :placeholder="$t('请输入码头编号')" />
        </el-form-item>
        <el-form-item :label="$t('码头类型')" prop="type">
          <el-input v-model="form.type" :placeholder="$t('请输入码头类型')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('确 定')}}</el-button>
        <el-button @click="cancel">{{$t('取 消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createDock, updateDock, deleteDock, getDock, getDockPage, exportDockExcel } from "@/api/ecw/dock";
import { getListTree } from "@/api/ecw/region";
import { getDictDatas, DICT_TYPE } from '@/utils/dict';

export default {
  name: "Dock",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 码头信息列表
      list: [],

      //国家信息列表
      countryList: [],
      //省信息列表
      provinceList: [],
      //市信息列表
      cityList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        titleZh: null,
        titleEn: null,
        sort: null,
        countryId: null,
        countryZh: null,
        countryEn: null,
        provinceId: null,
        provinceZh: null,
        provinceEn: null,
        cityId: null,
        cityZh: null,
        cityEn: null,
        number: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        titleZh: [{ required: true, message: this.$t("中文名称不能为空"), trigger: "blur" }],
        titleEn: [{ required: true, message: this.$t("英文名称不能为空"), trigger: "blur" }],
        sort: [{ required: true, message: this.$t("排序不能为空"), trigger: "blur" }]
      },
      portTypeDatas: getDictDatas(DICT_TYPE.ECW_PORT_TYPE)
    };
  },

  computed: {
    portTypeName() {
      return portType => {
        for(let index in this.portTypeDatas) {
          let portTypeItem = this.portTypeDatas[index];
          if(portTypeItem.value == portType) {
            return this.$l(portTypeItem,'label');
          }
        }
      }
    },
  },

  watch: {
    'form.countryId'(newV, oldV) {
      if(oldV && oldV != newV) {
          this.form.provinceId = null;
          this.form.provinceZh = null;
          this.form.provinceEn = null;
          this.form.cityId = null;
          this.form.cityZh = null;
          this.form.cityEn = null;
      }
    },

    'form.provinceId'(newV, oldV) {
        if(oldV && oldV != newV) {
          this.form.cityId = null;
          this.form.cityZh = null;
          this.form.cityEn = null;
        }
    }
  },

  created() {
    this.getList();
    this.getCountryList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getDockPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    getCountryList() {
        this.queryParams.treeType = 1;
        getListTree(this.queryParams).then(response => {
          this.countryList = response.data;
      });
    },

    changeCountry(countryId) {
      for(let index in this.countryList) {
        let country = this.countryList[index];
        if(country.id == countryId) {
          this.provinceList = country.children;
          
          this.form.countryZh = country.titleZh;
          this.form.countryEn = country.titleEn;
          return;
        }
      }
    },

    changeProvince(provinceId) {
      for(let index in this.provinceList) {
        let province = this.provinceList[index];
        if(province.id == provinceId) {
          this.cityList = province.children;
          
          this.form.provinceZh = province.titleZh;
          this.form.provinceEn = province.titleEn;
          return;
        }
      }
    },

    changeCity(cityId) {
      for(let index in this.cityList) {
        let city = this.cityList[index];
        if(city.id == cityId) {
          this.form.cityZh = city.titleZh;
          this.form.cityEn = city.titleEn;
          return;
        }
      }
    },

    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        titleZh: undefined,
        titleEn: undefined,
        sort: undefined,
        countryId: undefined,
        countryZh: undefined,
        countryEn: undefined,
        provinceId: undefined,
        provinceZh: undefined,
        provinceEn: undefined,
        cityId: undefined,
        cityZh: undefined,
        cityEn: undefined,
        number: undefined,
        type: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("添加码头信息");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getDock(id).then(response => {
        this.form = response.data;
        this.changeCountry(response.data.countryId);
        this.changeProvince(response.data.provinceId);
        
        this.open = true;
        this.title = this.$t("修改码头信息");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateDock(this.form).then(response => {
            this.$modal.msgSuccess(this.$t("修改成功"));
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createDock(this.form).then(response => {
          this.$modal.msgSuccess(this.$t("新增成功"));
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm(this.$t('是否确认删除码头信息编号为"') + id + this.$t('"的数据项?')).then(function() {
          return deleteDock(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("删除成功"));
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm(this.$t('是否确认导出所有码头信息数据项?')).then(() => {
          this.exportLoading = true;
          return exportDockExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>