<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="large"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="提供医院" prop="hospitals">
          <el-input
            v-model="queryParams.hospitals"
            placeholder="请输入提供医院"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="业务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入业务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="large"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="large" @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="large"
            @click="handleAdd"
            v-hasPermi="['system:business:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="large"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:business:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="large"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:business:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="large"
            @click="handleExport"
            v-hasPermi="['system:business:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row> -->

    <el-table
      key="outer"
      v-loading="loading"
      :data="businessList"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'id', order: 'ascending' }"
      border=""
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="ID"
        align="center"
        prop="id"
        :show-overflow-tooltip="true"
        sortable
      />
      <!-- <el-table-column label="提供医院" align="center" prop="hospitalsList">
        <template slot-scope="scope">
          <el-tag v-for="(item, index) in scope.row.hospitalsList" :key="index" style="margin-right: 8px">{{ item
          }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        label="业务名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="服务" align="center" prop="name">
        <template slot-scope="props">
          <el-button type="primary" round @click="clickServices(props.row)"
            >服务</el-button
          >
          <!-- <el-button size="large" type="text" icon="el-icon-edit">新增1111</el-button> -->
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="large"
            type="primary"
            icon="el-icon-edit"
            @click="handlenew(scope.row)"
            >新增</el-button
          >
          <el-button
            size="large"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="large"
            type="primary"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</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="servicesOpen"
      width="80%"
      append-to-body
      max-height="100px"
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="large"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <!-- <el-form-item label="提供医院" prop="hospitals">
          <el-input
            v-model="queryParams.hospitals"
            placeholder="请输入提供医院"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="服务名称" prop="name">
          <el-input
            v-model="name"
            placeholder="请输入服务名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable v-model="cityId" placeholder="请选择">
            <el-option
              v-for="item in cityVoList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="large"
            @click="handleQuerys"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-table key="inside" :data="servicesList" border="">
        <el-table-column
          prop="name"
          label="服务名称"
          :show-overflow-tooltip="true"
          align="center"
        ></el-table-column>
        <el-table-column prop="cover" label="封面" width="170" align="center">
          <template slot-scope="{ row }">
            <imageUpload
              v-model="row.cover"
              :limit="
                row.cover && row.cover.split(',') && row.cover.split(',').length
                  ? row.cover.split(',').length
                  : 1
              "
              :disabled="true"
              :isShowTip="false"
            >
            </imageUpload>
          </template>
        </el-table-column>
        <el-table-column
          prop="highPrice"
          label="高价"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="price"
          label="价格"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="cityName"
          label="城市"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="fenmo"
          label="平台分成"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="duration"
          label="时长(小时)"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="overtime"
          label="超时计费/30分钟"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="服务时段" :show-overflow-tooltip="true">
          <template slot-scope="{ row }">
            <span style="text-align:center" v-if="row.startTime && row.endTime"
              >{{ row.startTime }} - {{ row.endTime }}</span
            >
            <span style="text-align:center" v-else>未设置</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="large"
              type="primary"
              icon="el-icon-edit"
              @click="handleUpdateServe(scope.row)"
              >修改服务</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totals > 0"
        :total="totals"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="clickServices"
      />
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center !important"
      >
        <!-- <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="clcikCancel" type="primary">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改业务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="提供医院" prop="hospitalsIdsList">
          <el-select v-model="form.hospitalsIdsList" multiple clearable="" filterable placeholder="请选择">
            <el-option v-for="item in hospitalsList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="业务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入业务名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="service == true ? '修改服务' : '新增服务'"
      :visible.sync="openServe"
      width="50%"
      append-to-body
    >
      <div class="hidden-dialog">
        <el-form
          ref="serveForm"
          :model="serveForm"
          :rules="serveRules"
          label-width="80px"
        >
          <el-form-item label="服务名称" prop="name">
            <el-input v-model="serveForm.name" placeholder="请输入服务名称">
              <template slot="prepend">
                <i class="el-icon-edit"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="城市" prop="cityId">
            <el-select
              filterable
              clearable
              v-model="serveForm.cityId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in cityVoList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="Cover" prop="cover">
            <imageUpload v-model="serveForm.cover"></imageUpload>
          </el-form-item>
          <el-form-item label="高价" prop="highPrice">
            <strong>¥</strong>
            <el-input-number
              v-model="serveForm.highPrice"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <strong>¥</strong>
            <el-input-number
              v-model="serveForm.price"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="平台分成" prop="fenmo">
            <strong>%</strong>
            <el-input-number
              v-model="serveForm.fenmo"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="服务时长" prop="duration">
            <el-input-number
              v-model="serveForm.duration"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="超时付费" prop="overtime">
            <!-- <el-input v-model="serveForm.overtime" placeholder="请输入">
              <template slot="prepend"> ¥ </template>
            </el-input> -->
            <strong>¥</strong>
            <el-input-number
              v-model="serveForm.overtime"
              controls-position="right"
              :min="0"
            ></el-input-number>
            <div>每30分钟。未满30分钟按30分钟算</div>
          </el-form-item>

          <el-form-item label="服务时段" prop="date">
            <el-time-picker
              is-range
              arrow-control
              v-model="serveForm.date"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="HH:mm:ss"
              placeholder="选择时间范围"
            >
            </el-time-picker>
          </el-form-item>
          <el-form-item label="介绍" prop="info">
            <editor v-model="serveForm.info" :min-height="192" />
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button
          :loading="buttonLoading"
          type="primary"
          @click="submitServeForm"
          >确 定</el-button
        >
        <el-button @click="cancelServeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBusiness,
  getBusiness,
  delBusiness,
  addBusiness,
  updateBusiness,
  getServe,
  addServe,
  updateServe,
  servicesGET,
} from "@/api/operation/business";
import { listHospital, cityVoListGET } from "@/api/hispotalManage/hospital";

export default {
  name: "Business",
  data() {
    return {
      servicesList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      service: true,
      cityVoList: [],
      name: "",
      cityId: "",
      // 总条数
      total: 0,
      totals: 0,
      bid: "",
      // 业务表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      servicesOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitals: undefined,
        name: undefined,
      },
      pageNum: 1,
      pageSize: 10,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      openServe: false,
      serveForm: {},
      serveRules: {
        name: [{ required: true, message: "请输入服务名称", trigger: "blur" }],
        cityId: [{ required: true, message: "请选择城市", trigger: "change" }],
        highPrice: [
          {
            required: true,
            message: "请输入高价",
            trigger: ["blur", "change"],
          },
        ],
        price: [
          {
            required: true,
            message: "请输入高价",
            trigger: ["blur", "change"],
          },
        ],
      },
      hospitalsList: [],
    };
  },
  created() {
    this.getList();
    this.cityVoListAxios();
  },
  methods: {
    hospitalsListAxios() {
      listHospital({ pageNum: 1, pageSize: 99999 }).then((response) => {
        this.hospitalsList = response.rows;
      });
    },
    /** 查询业务列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then((response) => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelServeForm() {
      this.openServe = false;
      this.resetServe();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        hospitals: [],
        name: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
      };
      this.resetForm("form");
    },
    // 重置服务表单数据
    resetServe() {
      this.serveForm = {
        name: undefined,
        cover: undefined,
        highPrice: 0,
        price: 0,
        fenmo: 0,
        duration: 0,
        overtime: 0,
        startTime: undefined,
        endTime: undefined,
        info: undefined,
        date: ["", ""],
      };
      this.resetForm("serveForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    async cityVoListAxios() {
      const data_back = await cityVoListGET();
      const { code, data } = data_back;
      if (code === 200) {
        this.cityVoList = data;
      }
    },
    /** 重置按钮操作 */
    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.loading = true;
      // this.hospitalsListAxios();
      this.reset();
      const id = row.id || this.ids;
      getBusiness(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改业务";
      });
    },
    handlenew(row) {
      this.service = false;
      this.openServe = true;
      this.bid = row.id;
      this.resetServe();
    },
    handleQuerys() {
      this.pageNum = 1;
      this.pageSize = 10;
      let par = {};
      this.clickServices(par);
    },
    clickServices(val) {
      // console.log(val, '==============')
      if (val.id != undefined) {
        this.servicesID = val.id;
      }
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        bid: this.servicesID,
        name: this.name,
        cityId: this.cityId,
      };
      servicesGET(params).then((res) => {
        this.totals = res.total;
        this.servicesList = res.rows;
        this.servicesOpen = true;
      });
      // return
    },
    clcikCancel() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.servicesOpen = false;
    },
    // 修改服务
    handleUpdateServe(row) {
      this.service = true;
      this.openServe = true;
      this.resetServe();
      const id = row.id;
      getServe(id).then((response) => {
        this.serveForm = response.data;
        if (response.data && response.data.startTime && response.data.endTime) {
          this.$set(this.serveForm, "date", [
            response.data.startTime,
            response.data.endTime,
          ]);
        } else {
          this.$set(this.serveForm, "date", undefined);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBusiness(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBusiness(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    // 服务提交按钮
    submitServeForm() {
      this.$refs["serveForm"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.service == true) {
            updateServe({
              ...this.serveForm,
              startTime:
                this.serveForm.date && this.serveForm.date.length
                  ? this.serveForm.date[0]
                  : "",
              endTime:
                this.serveForm.date && this.serveForm.date.length
                  ? this.serveForm.date[1]
                  : "",
            })
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.openServe = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addServe({
              ...this.serveForm,
              startTime:
                this.serveForm.date && this.serveForm.date.length
                  ? this.serveForm.date[0]
                  : "",
              endTime:
                this.serveForm.date && this.serveForm.date.length
                  ? this.serveForm.date[1]
                  : "",
              bid: this.bid,
            })
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.openServe = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除业务编号为"' + ids + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delBusiness(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/business/export",
        {
          ...this.queryParams,
        },
        `business_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.hidden-dialog {
  height: 600px;
  overflow-x: hidden;
}
</style>