<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="供应商编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入供应商编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始地" prop="origin">
        <el-input
          v-model="queryParams.origin"
          placeholder="请输入起始地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-input
          v-model="queryParams.destination"
          placeholder="请输入目的地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="offerName">
        <el-input
          v-model="queryParams.offerName"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="priceAll">
        <el-input
          v-model="queryParams.priceAll"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人数" prop="peopleNum">
        <el-input
          v-model="queryParams.peopleNum"
          placeholder="请输入人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地点标题" prop="titleDayName">
        <el-input
          v-model="queryParams.titleDayName"
          placeholder="请输入地点标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="热门标识" prop="hotForm">
        <el-select v-model="queryParams.hotForm" placeholder="请选择热门标识" clearable>
          <el-option
            v-for="dict in dict.type.tuya_hotform_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <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="['tuya:route: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="['tuya:route: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="['tuya:route:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tuya:route:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="供应商编号" align="center" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="起始地" align="center" prop="origin" :show-overflow-tooltip="true" />
      <el-table-column label="目的地" align="center" prop="destination" :show-overflow-tooltip="true" />
      <el-table-column label="开始时间" align="center" prop="startTime" :show-overflow-tooltip="true" />
      <el-table-column label="结束日期" align="center" prop="endTime" :show-overflow-tooltip="true" />
      <el-table-column label="查看数量" align="center" prop="lookNum" :show-overflow-tooltip="true" />
      <el-table-column label="封面图" align="center" prop="titleImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.titleImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="offerName" :show-overflow-tooltip="true" />
      <el-table-column label="价格" align="center" prop="priceAll" :show-overflow-tooltip="true" />
      <el-table-column label="人数" align="center" prop="peopleNum" :show-overflow-tooltip="true" />
      <el-table-column label="建档日期" align="center" prop="jdrq" :show-overflow-tooltip="true" />
      <el-table-column label="地点标题" align="center" prop="titleDayName":show-overflow-tooltip="true" />
      <el-table-column label="热门" align="center" key="hotForm" prop="hotForm" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.hotForm"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </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)"
            v-hasPermi="['tuya:route:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tuya:route: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" width="1200" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="供应商编号" prop="code">
                <el-input v-model="form.code" placeholder="请输入供应商编号" :disabled="true" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="标题" prop="offerName">
                <el-input v-model="form.offerName" placeholder="请输入标题" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="起始地" prop="origin">
                <el-input v-model="form.origin" placeholder="请输入起始地" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="目的地" prop="destination">
                <el-input v-model="form.destination" placeholder="请输入目的地" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker
                  clearable
                  v-model="form.startTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择">
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker
                  clearable
                  v-model="form.endTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择">
                </el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="价格" prop="priceAll">
                <el-input-number v-model="form.priceAll" controls-position="right" :min="0"  :precision="2"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="人数" prop="peopleNum">
                <el-input-number v-model="form.peopleNum" controls-position="right" :min="0"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="地点标题" prop="titleDayName">
                <el-input v-model="form.titleDayName" placeholder="请输入地点标题" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="热门标识">
                <el-radio-group v-model="form.hotForm">
                  <el-radio
                    v-for="dict in dict.type.tuya_hotform_state"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="封面图" prop="titleImg">
                <div class="update-Img">
                  <el-upload
                    action="#"
                    list-type="picture-card"
                    :limit="1"
                    :auto-upload="false"
                    :on-change="uploadImage"
                    :on-remove="handeRemove"
                    :on-exceed="handleExceed"
                    :on-preview="handlePictureCardPreview"
                    :file-list="fileList"
                    accept=".jpg, .jpeg, .gif, .webp, .png"
                  >
                    <i class="el-icon-plus"></i>
                  </el-upload>
                  <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" v-if="imageUrl" :src="imageUrl" alt="">
                  </el-dialog>
                </div>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoute, getRoute, delRoute, addRoute, updateRoute,changeHotForm } from "@/api/tuya/supplierroute";
import {uploadAvatar} from "@/api/tuya/stiesinfo";

export default {
  name: "Route",
  dicts: ['tuya_hotform_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商路线表格数据
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        origin: null,
        destination: null,
        startTime: null,
        endTime: null,
        lookNum: null,
        titleImg: null,
        offerName: null,
        priceAll: null,
        peopleNum: null,
        uuid: null,
        jdrq: null,
        updateDate: null,
        titleDayName: null,
        hotForm: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileList: [],
      //图片地址
      imageUrl: "",
      dialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //图片预览
    handlePictureCardPreview(file) {
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传图片数量不能超过1个!`);
    },
    handeRemove(file) {
      this.form.titleImg = "";
    },
    // 上传图片
    uploadImage(file) {
      const isJPG = file.raw.type === "image/jpg";
      const isJPEG = file.raw.type === "image/jpeg";
      const isPNG = file.raw.type === "image/png";
      const isGIF = file.raw.type === "image/gif";
      const isBMP = file.raw.type === "image/bmp";
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG && !isJPEG && !isPNG && !isGIF && !isBMP) {
        this.$message.error("只能上传图片格式为jpg,jpeg,png,gif,bmp！");
      } else if (!isLt2M) {
        this.$message.error('图片大小不能超过 5MB!');
      } else {
        let fd = new FormData()
        fd.append('file', file.raw)
        uploadAvatar(fd).then(response => {
          this.form.titleImg = response.data;
        });
      }
    },
    // 是否热门修改
    handleStatusChange(row) {
      this.$modal.confirm('确认修改热门状态码？').then(function() {
        return changeHotForm(row.id, row.hotForm);
      }).then(() => {
        this.$modal.msgSuccess("修改成功");
      }).catch(function() {
        row.hotForm = row.hotForm === "0" ? "1" : "0";
      });
    },
    /** 查询供应商路线列表 */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then(response => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        origin: null,
        destination: null,
        startTime: null,
        endTime: null,
        lookNum: null,
        titleImg: null,
        offerName: null,
        priceAll: null,
        peopleNum: null,
        uuid: null,
        jdrq: null,
        updateDate: null,
        titleDayName: null,
        hotForm: "0"
      };
      this.fileList = [];
      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
      getRoute(id).then(response => {
        this.form = response.data;
        if (response.data.titleImg != '' && response.data.titleImg != null) {
          this.fileList.push({url: response.data.titleImg})
        }
        this.open = true;
        this.title = "修改供应商路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoute(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商路线编号为"' + ids + '"的数据项？').then(function() {
        return delRoute(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tuya/route/export', {
        ...this.queryParams
      }, `route_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
