<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="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入群名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群编号" prop="groupCode">
        <el-input
          v-model="queryParams.groupCode"
          placeholder="请输入群编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客服类型" prop="customerServiceType">
        <el-select clearable v-model="queryParams.customerServiceType" placeholder="请选择客服类型">
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </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="['system:groupQrCodeInfo: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="['system:groupQrCodeInfo: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="['system:groupQrCodeInfo: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="['system:groupQrCodeInfo:export']"
          >导出</el-button
        >
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="groupQrCodeInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <!-- <el-table-column label="所属护理机构id" align="center" prop="nurseStationId" /> -->
      <el-table-column label="群名称" align="center" prop="groupName" />
      <el-table-column label="群编号" align="center" prop="groupCode" />
      <!-- <el-table-column label="群二维码存放路径" align="center" prop="groupQrCodeUrl" /> -->
      <el-table-column label="客服类型" align="center" prop="customerServiceType">
        <template slot-scope="scope">
          {{
          scope.row.customerServiceType == "NURSE_AGENCY_CUSTOMER"
          ? "护理机构客服"
          : ""
          }}
          {{
          scope.row.customerServiceType == "SYSTEM_CUSTOMER" ? "系统客服" : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="展示顺序" align="center" prop="groupSort" />
      <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="['system:groupQrCodeInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:groupQrCodeInfo: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="editopen"
      width="500px"
      append-to-body
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="群名称" prop="groupName">
          <el-input
            maxlength="50"
            style="width: 225px"
            v-model="form.groupName"
            placeholder="请输入群名称"
          />
        </el-form-item>
        <el-form-item label="群二维码" prop="groupQrCodeUrl">
          <stationAcatar
            @imgUrl="setUpdateImgUrl"
            :img="form.groupQrCodeUrl"
            :type="'groupQrCodeUrl'"
          />
        </el-form-item>
        <el-form-item label="客服类型" prop="customerServiceType">
          <el-select style="width: 225px" v-model="form.customerServiceType" placeholder="请选择客服类型">
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="所属护理站"
          prop="nurseStationId"
          v-if="form.customerServiceType == 'NURSE_AGENCY_CUSTOMER'"
        >
          <template>
            <el-button
              @click="ParamsStation(false)"
              style="
                width: 225px;
                text-align: left;
                height: 36px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                color: #c0c4cc;
              "
              v-if="form.nurseStationName == '请选择所属护理站'"
            >{{ form.nurseStationName }}</el-button>
            <el-button
              type
              style="
                width: 225px;
                text-align: left;
                height: 36px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              "
              @click="ParamsStation(false)"
              v-else
            >{{ form.nurseStationName }}</el-button>
          </template>
        </el-form-item>
        <el-form-item label="展示顺序" prop="groupSort">
          <el-input
            style="width: 225px"
            v-model.number="form.groupSort"
            placeholder="请输入群二维码展示顺序"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
          />
        </el-form-item>
        <!-- </div> -->
      </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>
    <!-- 添加在线客服对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <div
          v-for="(item, index) in form.groupQrCodeInfoDTOList"
          :key="index"
          style="margin-bottom: 30px"
        >
          <el-form-item
            :rules="rules.groupQrCodeInfoDTOList.groupName"
            label="群名称"
            prop="groupQrCodeInfoDTOList[0].groupName"
          >
            <el-input
              maxlength="50"
              style="width: 225px"
              v-model="item.groupName"
              placeholder="请输入群名称"
            />
          </el-form-item>
          <el-form-item
            label="群二维码"
            prop="groupQrCodeInfoDTOList[0].groupQrCodeUrl"
            :rules="rules.groupQrCodeInfoDTOList.groupQrCodeUrl"
          >
            <stationAcatar @imgUrl="imgUrl" :img="item.groupQrCodeUrl" :type="'groupQrCodeUrl'" />
          </el-form-item>
          <el-form-item
            label="客服类型"
            prop="groupQrCodeInfoDTOList[0].customerServiceType"
            :rules="rules.groupQrCodeInfoDTOList.customerServiceType"
          >
            <el-select
              style="width: 225px"
              v-model="item.customerServiceType"
              placeholder="请选择客服类型"
            >
              <el-option
                v-for="item in option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="所属护理站"
            prop="groupQrCodeInfoDTOList[0].nurseStationId"
            :rules="rules.groupQrCodeInfoDTOList.nurseStationId"
            v-if="item.customerServiceType == 'NURSE_AGENCY_CUSTOMER'"
          >
            <template>
              <el-button
                type
                @click="ParamsStation(true)"
                style="
                  width: 225px;
                  text-align: left;
                  height: 36px;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                  color: #c0c4cc;
                "
                v-if="item.nurseStationName == '请选择所属护理站'"
              >{{ item.nurseStationName }}</el-button>
              <el-button
                type
                style="
                  width: 225px;
                  text-align: left;
                  height: 36px;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                "
                @click="ParamsStation(true)"
                v-else
              >{{ item.nurseStationName }}</el-button>
            </template>
          </el-form-item>
          <el-form-item
            label="展示顺序"
            prop="groupQrCodeInfoDTOList[0].groupSort"
            :rules="rules.groupQrCodeInfoDTOList.groupSort"
          >
            <el-input
              style="width: 225px"
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="5"
              v-model.number="item.groupSort"
              placeholder="请输入群二维码展示顺序"
            />
          </el-form-item>
        </div>
      </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>
    <!-- 护理站 -->
    <el-dialog
      title="选择所属护理站"
      :visible.sync="nurseStationshow"
      width="1100px"
      append-to-body
      :before-close="clicknurseStationshow"
    >
      <el-form ref="form" :model="getListByUserquery" label-width="110px" :inline="true">
        <el-form-item label="护理站编码" prop="nurseStationCode">
          <el-input v-model="getListByUserquery.nurseStationCode" placeholder="请输入护理站编码" />
        </el-form-item>
        <el-form-item label="护理站名称" prop="nurseStationName">
          <el-input v-model="getListByUserquery.nurseStationName" placeholder="请输入护理站名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="info">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="stationcancel">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="nurseStationlist" @cell-dblclick="choicestationid">
        <el-table-column label="请选择" width="70" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 15px; height: 15px"
              circle
              @click="choicestationid(scope.row)"
              v-if="stationidIndex == scope.row.id"
            ></el-button>

            <el-button
              style="width: 15px; height: 15px"
              circle
              v-else
              @click="choicestationid(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column property="nurseStationCode" label="护理站编码" align="center"></el-table-column>
        <el-table-column property="nurseStationName" label="护理站名称" align="center"></el-table-column>
        <el-table-column property="phone" label="联系电话" align="center"></el-table-column>
        <el-table-column
          property="address"
          label="护理站地址"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
      </el-table>

      <pagination
        v-show="total4 > 0"
        :total="total4"
        :page.sync="getListByUserquery.pageNum"
        :limit.sync="getListByUserquery.pageSize"
        @pagination="info"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listGroupQrCodeInfo,
  getListByUser,
  getGroupQrCodeInfo,
  delGroupQrCodeInfo,
  addGroupQrCodeInfo,
  updateGroupQrCodeInfo
} from "@/api/system/groupQrCodeInfo";
import stationAcatar from "../stationAvatar/index.vue";
import { updatePicture } from "@/api/system/station";
export default {
  components: { stationAcatar },
  name: "GroupQrCodeInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      groupName:[],
      getListByUserquery: {
        pageNum: 1,
        pageSize: 10
      },
      nurseStationlist: [],
      option: [
        {
          value: "NURSE_AGENCY_CUSTOMER",
          label: "护理机构客服"
        },
        {
          value: "SYSTEM_CUSTOMER",
          label: "系统客服"
        }
      ],
      value: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 在线客服表格数据
      groupQrCodeInfoList: [],
      // 弹出层标题
      title: "",
      total4: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nurseStationId: null,
        groupName: null,
        groupCode: null,
        groupQrCodeUrl: null,
        customerServiceType: null,
        groupSort: null
      },
      nurseStationshow: false,
      stationidIndex: "",
      editopen: false,
      imgsurl: { pictureUrlList: [] },
      imgone: "",
      // 表单参数
      form: {
        groupQrCodeInfoDTOList: [],
        nurseStationId: null,
        nurseStationName: "请选择所属护理站"
      },
      homenumber: false,
      // deletNewImgs: [],
      objitem: [],
      // 表单校验
      rules: {
        groupQrCodeInfoDTOList: {
          groupName: [
            {
              required: true,
              message: "请输入名称",
              trigger: "blur"
            }
          ],
          groupSort: [
            {
              required: true,
              message: "请输入排序",
              trigger: "blur"
            }
          ],
          groupQrCodeUrl: [
            {
              required: true,
              message: "请选择上传图片",
              trigger: "blur"
            }
          ],
          customerServiceType: [
            {
              required: true,
              message: "请选择客服类型",
              trigger: "blur"
            }
          ],
          nurseStationId: [
            { required: true, message: "请选择所属护理站", trigger: "blur" }
          ]
        },
        groupName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        groupSort: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur"
          }
        ],
        groupQrCodeUrl: [
          {
            required: true,
            message: "请选择上传图片",
            trigger: "blur"
          }
        ],
        customerServiceType: [
          {
            required: true,
            message: "请选择客服类型",
            trigger: "blur"
          }
        ],
        nurseStationId: [
          { required: true, message: "请选择所属护理站", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //护理站页面  =》选择护理站
    choicestationid(item) {
      this.stationidIndex = item.id;
      if (this.editopen) {
        this.form.nurseStationName = item.nurseStationName;
        this.form.nurseStationId = item.id;
      } else if (this.open) {
        // this.form.groupQrCodeInfoDTOList[0].nurseStationId = item.id;
        this.form.groupQrCodeInfoDTOList[0].nurseStationName =
          item.nurseStationName;
        this.form.groupQrCodeInfoDTOList[0].nurseStationId = item.id;
      }
      this.nurseStationshow = false;
      this.stationcancel();
    },
    //护理站重置
    stationcancel() {
      this.getListByUserquery = {
        pageNum: 1,
        pageSize: 10
      };
      this.info();
    },
    //关闭护理站页面
    clicknurseStationshow() {
      this.nurseStationshow = false;
    },
    //所属护理站页面
    ParamsStation(item) {
      console.log(
        item,
        this.form.nurseStationId,
        "打印this.queryParams.nurseStationId"
      );
      this.info();
      this.stationidIndex = this.form.nurseStationId;
      this.nurseStationshow = true;
    },
    //护理站list
    info() {
      getListByUser(this.getListByUserquery).then(res => {
        this.nurseStationlist = res.rows;
        this.total4 = res.total;
      });
    },
    // 设置修改的图片地址
    setUpdateImgUrl(e) {
      this.form.groupQrCodeUrl = e;
      console.log(this.form);
    },
    imgUrl(imgUrl) {
      console.log(this.form);
      // this.$set(this.form,"groupQrCodeInfoDTOList",[])
      this.form.groupQrCodeInfoDTOList.forEach(e => {
        this.$set(e, "groupQrCodeUrl", imgUrl);
      });
    },
    /** 查询在线客服列表 */
    getList() {
      this.loading = true;
      listGroupQrCodeInfo(this.queryParams).then(response => {
        this.groupQrCodeInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      if (this.editopen) {
        var obj = { pictureUrlList: [] };
        if (this.form.groupQrCodeUrl != this.imgone) {
          obj.pictureUrlList.push(this.form.groupQrCodeUrl);
        }
        console.log(obj);
        updatePicture(obj).then(res => {
          this.editopen = false;
        });
      }
      if (this.open) {
        var obj = { pictureUrlList: [] };
        if (this.form.groupQrCodeInfoDTOList) {
          this.form.groupQrCodeInfoDTOList.forEach(e => {
            obj.pictureUrlList.push(e.groupQrCodeUrl);
          });
        }
        updatePicture(obj).then(res => {
          this.open = false;
        });
        this.imgsurl = { pictureUrlList: [] };
      }

      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupQrCodeInfoDTOList: [
          {
            nurseStationId: null,
            groupName: null,
            // groupCode: null,
            groupQrCodeUrl: null,
            customerServiceType: null,
            groupSort: null,
            nurseStationName: "请选择所属护理站"
          }
        ]
      };
      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.groupName = selection.map(item => item.groupName);

      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {
        groupQrCodeInfoDTOList: [
          {
            nurseStationId: null,
            groupName: null,
            groupSort: null,
            customerServiceType: null,
            nurseStationName: "请选择所属护理站",
            groupQrCodeUrl: null
          }
        ]
      };

      this.open = true;
      this.title = "添加在线客服";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.form.nurseStationId = response.data.nurseStationId;
      this.reset();
      const id = row.id || this.ids;
      getGroupQrCodeInfo(id).then(response => {
        console.log(response.data);
        this.form = response.data;

        // this.form.nurseStationName = item.nurseStationName;
        // this.form.nurseStationId = item.id;
        this.imgone = this.form.groupQrCodeUrl;

        this.editopen = true;
        this.title = "修改在线客服";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGroupQrCodeInfo(this.form).then(response => {
              if (this.form.groupQrCodeUrl != this.imgone) {
                var obj = { pictureUrlList: [] };
                obj.pictureUrlList.push(this.imgone);
                updatePicture(obj).then(res => {});
              }

              this.$modal.msgSuccess("修改成功");
              this.editopen = false;
              this.getList();
            });
          } else {
            addGroupQrCodeInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      const ids = row.id || this.ids;
      const groupNames = row.groupName || this.groupNames;
      this.$modal
        .confirm('确认删除群名称为"' + groupNames + '"的信息吗？')
        .then(function() {
          return delGroupQrCodeInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/groupQrCodeInfo/export",
        {
          ...this.queryParams
        },
        `groupQrCodeInfo_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
