<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="平台名称" prop="siteNameCn">
        <el-input
          v-model="queryParams.siteNameCn"
          placeholder="请输入平台名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.isLock" placeholder="状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_normal_disable"-->
<!--            :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:sitesinfo: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:sitesinfo: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:sitesinfo:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="siteId" width="50" />
      <el-table-column label="平台名称(中)" align="center" prop="siteNameCn" width="100" fixed="left" :show-overflow-tooltip="true"/>
      <el-table-column label="平台名称(英)" align="center" prop="siteNameEn" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="绑定域名" align="center" prop="domainName" :show-overflow-tooltip="true"/>
      <el-table-column label="首页标题" align="center" prop="homeTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="网站备案号" align="center" prop="siteProvider" width="110" :show-overflow-tooltip="true"/>
      <el-table-column label="底部版权" align="center" prop="siteCopyright" :show-overflow-tooltip="true"/>
      <el-table-column label="公司名称" align="center" prop="siteCompany" :show-overflow-tooltip="true"/>
      <el-table-column label="通讯地址" align="center" prop="postalAddress" :show-overflow-tooltip="true"/>
      <el-table-column label="联系电话" align="center" prop="siteTel" :show-overflow-tooltip="true"/>
      <el-table-column label="传值号码" align="center" prop="siteFax" :show-overflow-tooltip="true"/>
      <el-table-column label="电子邮箱" align="center" prop="siteEmail" :show-overflow-tooltip="true"/>
      <el-table-column label="排序数字" align="center" prop="sortId" width="80" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="状态" align="center" prop="isLock" width="50">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.isLock"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="平台权限单价" align="center" prop="powerUnit" width="110" :show-overflow-tooltip="true"/>
      <el-table-column label="平台手续费比例" align="center" prop="percentageFee" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="游记发布佣金比例" align="center" prop="percentageBrokerage" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="170" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tuya:sitesinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tuya:sitesinfo: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="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="平台名称(中文)" prop="siteNameCn">
                <el-input v-model="form.siteNameCn" placeholder="请输入中文名称" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="平台名称(英文)" prop="siteNameEn">
                <el-input v-model="form.siteNameEn" placeholder="请输入英文名称" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="绑定域名" prop="domainName">
                <el-input v-model="form.domainName" placeholder="请输入绑定域名" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="首页标题" prop="homeTitle">
                <el-input v-model="form.homeTitle" placeholder="请输入首页标题" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="网站备案号" prop="siteProvider">
                <el-input v-model="form.siteProvider" placeholder="请输入网站备案号" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="底部版权" prop="siteCopyright">
                <el-input v-model="form.siteCopyright" placeholder="请输入底部版权" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="公司名称" prop="siteCompany">
                <el-input v-model="form.siteCompany" placeholder="请输入公司名称" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="通讯地址" prop="postalAddress">
                <el-input v-model="form.postalAddress" placeholder="请输入通讯地址" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系电话" prop="siteTel">
                <el-input v-model="form.siteTel" placeholder="请输入联系电话" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="传值号码" prop="siteFax">
                <el-input v-model="form.siteFax" placeholder="请输入传值号码" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="电子邮箱" prop="siteEmail">
                <el-input v-model="form.siteEmail" placeholder="请输入电子邮箱" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="LOGO1" prop="siteLogo1">
                <div class="update-logo">
                  <el-input v-model="form.siteLogo1" placeholder="请输入URL" />
                  <el-upload class="logo-uploader" action="#"
                             :on-change="(file,fileList) => {uploadImage(file, fileList, 1)}"
                             :limit="1"
                             :auto-upload="false"
                             :show-file-list="false"
                  >
                    <el-button class="uploader-button" size="small">
                      选择
                      <i class="el-icon-upload el-icon--right"></i>
                    </el-button>
                  </el-upload>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="排序数字" prop="sortId">
                <el-input-number v-model="form.sortId" controls-position="right" :min="0" />
              </el-form-item>
            </div>
<!--            <div class="grid-content bg-purple">-->
<!--              <el-form-item label="状态" prop="isLock">-->
<!--                <el-radio-group v-model="form.isLock">-->
<!--                  <el-radio-->
<!--                    v-for="dict in dict.type.sys_normal_disable"-->
<!--                    :key="dict.value"-->
<!--                    :label="dict.value"-->
<!--                  >{{dict.label}}</el-radio>-->
<!--                </el-radio-group>-->
<!--              </el-form-item>-->
<!--            </div>-->
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="LOGO2" prop="siteLogo2">
                <div class="update-logo">
                  <el-input v-model="form.siteLogo2" placeholder="请输入URL" />
                  <el-upload class="logo-uploader" action="#"
                             :on-change="(file,fileList) => {uploadImage(file, fileList, 2)}"
                             :limit="1"
                             :auto-upload="false"
                             :show-file-list="false"
                  >
                    <el-button class="uploader-button" size="small">
                      选择
                      <i class="el-icon-upload el-icon--right"></i>
                    </el-button>
                  </el-upload>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="平台权限单价" prop="powerUnit">
                <el-input-number v-model="form.powerUnit" controls-position="right" :min="0.000" :step="0.001" :precision="3"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="LOGO3" prop="siteLogo3">
                <div class="update-logo">
                  <el-input v-model="form.siteLogo3" placeholder="请输入URL" />
                  <el-upload class="logo-uploader" action="#"
                             :on-change="(file,fileList) => {uploadImage(file, fileList, 3)}"
                             :limit="1"
                             :auto-upload="false"
                             :show-file-list="false"
                  >
                    <el-button class="uploader-button" size="small">
                      选择
                      <i class="el-icon-upload el-icon--right"></i>
                    </el-button>
                  </el-upload>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="平台手续费比例" prop="percentageFee">
                <el-input-number v-model="form.percentageFee" controls-position="right" :min="0.000" :step="0.001" :precision="3" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="游记发布佣金比例" label-width="110" prop="percentageBrokerage">
                <el-input-number v-model="form.percentageBrokerage" controls-position="right" :min="0.000" :step="0.001" :precision="3" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"  :rows="5" />
              </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 {listSitesInfo, getSitesInfo, delSitesInfo, addSitesInfo, updateSitesInfo, uploadAvatar} from "@/api/tuya/stiesinfo";
import store from "@/store";

export default {
  name: "Info",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteNameCn: null,
        isLock: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteNameCn: [
          { required: true, message: "平台名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询平台信息列表 */
    getList() {
      this.loading = true;
      listSitesInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        siteId: null,
        siteNameCn: null,
        siteNameEn: null,
        domainName: null,
        siteLogo1: null,
        siteLogo2: null,
        siteLogo3: null,
        homeTitle: null,
        siteProvider: null,
        siteCopyright: null,
        siteCompany: null,
        postalAddress: null,
        siteTel: null,
        siteFax: null,
        siteEmail: null,
        seoTitle: null,
        seoKeyword: null,
        seoDescription: null,
        sortId: 1,
        isLock: "0",
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        flag: null,
        remark: null,
        powerUnit: 0.000,
        percentageFee: 0.000,
        percentageBrokerage: 0.000
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.siteId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const siteId = row.siteId || this.ids
      getSitesInfo(siteId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.siteId != null) {
            console.log(this.form);
            updateSitesInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSitesInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const siteIds = row.siteId || this.ids;
      this.$modal.confirm('是否确认删除平台信息编号为"' + siteIds + '"的数据项？').then(function() {
        return delSitesInfo(siteIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tuya/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    // 上传图片
    uploadImage(file,fileList,i) {
      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";
      if (!isJPG && !isJPEG && !isPNG && !isGIF && !isBMP) {
        this.$message.error("只能上传图片格式为jpg,jpeg,png,gif,bmp！");
      }else {
        let fd = new FormData()
        fd.append('file', file.raw)
        uploadAvatar(fd).then(response => {
          if (i === 1){
            this.form.siteLogo1 = response.data;
          }
          if (i === 2){
            this.form.siteLogo2 = response.data;
          }
          if (i === 3){
            this.form.siteLogo3 = response.data;
          }
        });
      }
    }
  }
};
</script>

<style scoped lang="scss">
 .update-logo{
   display:flex;
   align-items:center;
 }
 .logo-uploader{
   padding-left: 13px;
 }
 .uploader-button{
   height: 37px;
 }
</style>
