<template>
  <div>
    <div class="search-wrapper">
      <el-form
        :inline="true"
        :model="searchForm"
        class="search_form"
        style="text-align: left"
      >
        <el-form-item label="名称">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入店铺名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input
            v-model="searchForm.phone"
            placeholder="请输入电话"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="类型">
          <el-cascader
            :options="menuOptions"
            clearable
            filterable
            placeholder="请选择类型"
            @change="menuChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="入驻类型">
          <el-select
            v-model="searchForm.type"
            clearable
            placeholder="请选择入驻类型"
            size="medium"
            @change="onSubmit"
          >
            <el-option label="全部" value=""> </el-option>
            <el-option label="商家入驻" value="0"> </el-option>
            <el-option label="上报网红墙" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <br />
        <el-form-item label="状态" style="margin-top: 10px">
          <el-radio-group v-model="searchForm.state" @change="onSubmit">
            <el-radio label="">全部</el-radio>

            <el-radio :label="0">未审核</el-radio>
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="4">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <!-- <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane
        :label="`我的待办 ( ${taskData.myTask} )`"
        name="1"
      ></el-tab-pane>
      <el-tab-pane
        :label="`我的已办 ( ${taskData.finishedTask} )`"
        name="2"
      ></el-tab-pane>
      <el-tab-pane
        :label="`所有任务 ( ${taskData.allTask} )`"
        name="3"
      ></el-tab-pane>
    </el-tabs> -->

    <div class="table-wrapper">
      <table-template
        :tableData="tableData"
        :tableColumns="tableColumns"
        :loading="loading"
        :pagination="pagination"
        @Jump="jumpPage"
      >
        <el-table-column slot="type" label="入驻类型" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.type == 0 || scope.row.type == 1"
              :type="scope.row.type == 1 ? 'success' : ''"
              >{{
                scope.row.type == 0
                  ? "商家入驻"
                  : scope.row.type == 1
                  ? "上报网红墙"
                  : ""
              }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column slot="state" label="审核状态" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="
                scope.row.state == 0 ||
                scope.row.state == 1 ||
                scope.row.state == 4
              "
              :type="
                scope.row.state == 4
                  ? 'danger'
                  : scope.row.state == 1
                  ? 'success'
                  : ''
              "
              >{{ scope.row.stateName }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column slot="delFlag" label="启用状态" align="center">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.delFlag == 0 || scope.row.delFlag == 1"
              :type="
                scope.row.delFlag == 1
                  ? 'danger'
                  : scope.row.delFlag == 0
                  ? 'success'
                  : ''
              "
              >{{ scope.row.flagName }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column slot="icon" label="照片" align="center">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.pictureList"
              style="width: 30px; height: 30px"
              :src="scope.row.pictureList && scope.row.pictureList[0].url"
              :preview-src-list="scope.row.previewSrcList"
            >
            </el-image>
            <div v-else>暂无图片</div>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="350" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" round @click="view(scope.row)"
              >查看</el-button
            >
            <el-button
              @click="delFlagHandle(scope.row)"
              :type="scope.row.delFlag == 0 ? 'danger' : 'success'"
              size="mini"
              round
              slot="reference"
              >{{ scope.row.delFlag == 0 ? "禁用" : "启用" }}
            </el-button>
          </template>
        </el-table-column>
      </table-template>
    </div>
    <el-dialog
      title="详情"
      :visible.sync="dialogVisible"
      width="30%"
      destroy-on-close
    >
      <div class="cell">
        <p class="label">名称</p>
        <p class="value">{{ detail.name }}</p>
      </div>
      <div class="cell">
        <p class="label">入驻类型</p>
        <p class="value">
          <el-tag
            v-if="detail.type == 0 || detail.type == 1"
            :type="detail.type == 1 ? 'success' : ''"
          >
            {{ detail.type == 0 ? "商家入驻" : "上报网红墙" }}</el-tag
          >
        </p>
      </div>
      <div class="cell">
        <p class="label">类型</p>
        <p class="value">{{ detail.category }}</p>
      </div>
      <div class="cell" v-if="detail.otherInfo">
        <p class="label">经营范围和内容</p>
        <p class="value">{{ detail.otherInfo }}</p>
      </div>
      <div class="cell">
        <p class="label">详细地址</p>
        <p class="value">{{ detail.address }}</p>
      </div>
      <div class="cell">
        <p class="label">区</p>
        <p class="value">{{ detail.district }}</p>
      </div>
      <div class="cell">
        <p class="label">电话</p>
        <p class="value">{{ detail.phone }}</p>
      </div>
      <div class="cell">
        <p class="label">营业时间</p>
        <p class="value">{{ detail.businessHours }}</p>
      </div>
      <div class="cell">
        <p class="label">创建时间</p>
        <p class="value">{{ detail.createTime }}</p>
      </div>
      <div class="cell">
        <p class="label">审核状态</p>
        <p class="value">
          <el-tag
            v-if="detail.state == 0 || detail.state == 1 || detail.state == 4"
            :type="
              detail.state == 4 ? 'danger' : detail.state == 1 ? 'success' : ''
            "
            >{{
              detail.state == 4
                ? "审核拒绝"
                : detail.state == 1
                ? "审核通过"
                : "未审核"
            }}</el-tag
          >
        </p>
      </div>
      <div class="cell">
        <p class="label">启用状态</p>
        <p class="value">
          <el-tag
            v-if="detail.delFlag == 0 || detail.delFlag == 1"
            :type="detail.delFlag == 1 ? 'danger' : 'success'"
            >{{ detail.delFlag == 1 ? "禁用" : "启用" }}</el-tag
          >
        </p>
      </div>
      <div class="cell">
        <p class="label">照片</p>
        <p class="value">
          <el-image
            v-for="(item, index) in detail.pictureList"
            :key="index"
            style="width: 100px; height: 100px; margin-right: 10px"
            :src="item.url"
            :z-index="7777777"
            :preview-src-list="[item.url]"
          >
          </el-image>
        </p>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button
          type="success"
          round
          slot="reference"
          :disabled="detail.state != 0"
          @click="reviewHandle('pass', detail)"
          >通过
        </el-button>

        <el-button
          type="danger"
          round
          slot="reference"
          :disabled="detail.state != 0"
          @click="reviewHandle('refuse', detail)"
          >拒绝
        </el-button>

        <!-- <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        > -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTemplate from "@/components/Table";
import {
  applyList,
  applyReview,
  applyDetail,
  shopEnable,
  shopMenu,
} from "@/api/livingCircle";
export default {
  name: "enter",
  components: { tableTemplate },
  data() {
    return {
      dialogVisible: false,
      detail: {},
      searchForm: {
        name: "",
        state: "",
        type: "",
        phone: "",
        categoryId: "",
      },
      menuOptions: [],

      loading: false,
      tableData: [],
      tableColumns: [
        {
          key: "name",
          label: "名称",
        },
        {
          key: "type",
          label: "入驻类型",
          type: "slot",
        },
        {
          key: "category",
          label: "类型",
        },
        {
          key: "district",
          label: "区",
        },
        {
          key: "address",
          label: "详细地址",
          width: 220,
        },
        {
          key: "icon",
          label: "照片",
          type: "slot",
        },
        {
          key: "phone",
          label: "联系电话",
        },
        {
          key: "businessHours",
          label: "营业时间",
        },
        {
          key: "createTime",
          label: "创建时间",
          width: 180,
        },

        {
          key: "state",
          label: "审核状态",
          type: "slot",
        },
        {
          key: "delFlag",
          label: "启用状态",
          type: "slot",
        },
        // {
        //   key: "url",
        //   label: "营业执照",
        //   type: "slot",
        // },
        // {
        //   key: "url1",
        //   label: "身份证",
        //   type: "slot",
        // },
      ],
      pagination: {
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
      },
      userName: "",
      typeList: [],
    };
  },
  mounted() {
    this.userName = localStorage.getItem("userName");

    this.init();
    this.getShopMenu();
  },
  methods: {
    getShopMenu() {
      shopMenu().then((res) => {
        if (res.code == 200) {
          let arr = [];
          res.data.forEach((item) => {
            let obj = {
              value: item.id,
              label: item.name,
              children: [],
            };
            if (item.categoryDOList && item.categoryDOList.length > 0) {
              item.categoryDOList.forEach((it) => {
                obj.children.push({
                  value: it.id,
                  label: it.name,
                });
              });
            }
            arr.push(obj);
            this.menuOptions = arr;
          });
          console.log("res--->", this.menuOptions);
        }
      });
    },
    menuChange(val) {
      this.searchForm.categoryId = val[1];
      this.init();
      // console.log("menuChange--->", val, this.searchForm.categoryId);
    },

    init() {
      applyList({
        name: this.searchForm.name,
        state: this.searchForm.state,
        type: this.searchForm.type,
        phone: this.searchForm.phone,
        pageNum: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        categoryId: this.searchForm.categoryId,
      }).then((res) => {
        if (res.code == 200) {
          this.tableData =
            res.data.list && res.data.list.length > 0
              ? res.data.list.map((item) => {
                  return {
                    ...item,
                    stateName:
                      item.state == 0
                        ? "未审核"
                        : item.state == 1
                        ? "审核通过"
                        : item.state == 4
                        ? "审核拒绝"
                        : "",
                    flagName:
                      item.delFlag == 0
                        ? "启用"
                        : item.delFlag == 1
                        ? "禁用"
                        : "",
                    previewSrcList:
                      item.pictureList && item.pictureList.length > 0
                        ? item.pictureList.map((item) => {
                            return item.url;
                          })
                        : [],
                  };
                })
              : [];
          // this.tableData = res.data.list;
          this.pagination.total = res.data.total;
        }
      });
    },

    reviewHandle(type, row) {
      let params = {};
      if (type === "pass") {
        this.$prompt(`确定通过该申请吗?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入原因",
          inputType: "textarea",
        }).then(({ value }) => {
          if (!value) {
            this.$message.error("未输入原因");
            return;
          } else {
            params = {
              id: row.id,
              state: 1,
              user: this.userName,
              reason: value,
            };
            applyReview(params).then((res) => {
              if (res.code == 200) {
                this.$message.success("审核成功");
                this.init();
                this.dialogVisible = false;
              }
            });
          }
        });
      } else {
        this.$prompt(`确定拒绝该申请吗?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入原因",
          inputType: "textarea",
        }).then(({ value }) => {
          if (!value) {
            this.$message.error("未输入原因");
            return;
          } else {
            params = {
              id: row.id,
              state: 4,
              user: this.userName,
              reason: value,
            };
            applyReview(params).then((res) => {
              if (res.code == 200) {
                this.$message.success("审核成功");
                this.init();
                this.dialogVisible = false;
              }
            });
          }
        });
      }
      // applyReview(params).then((res) => {
      //   if (res.code == 200) {
      //     this.$message.success("审核成功");
      //     this.init();
      //   }
      // });
      // this.dialogVisible = false;
    },

    view(row) {
      applyDetail(row.id).then((res) => {
        if (res.code == 200) {
          this.detail = res.data;
          this.dialogVisible = true;
        }
      });
    },
    delFlagHandle(row) {
      shopEnable({
        id: row.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.init();
        }
      });
    },
    handleClick() {
      this.loading = true;
      this.init();
      this.loading = false;
      console.log("this.init();--->", this.activeName);
    },
    jumpPage() {
      this.init();
    },
    onSubmit() {
      this.init();
    },
    previewSrcList(list) {
      this.$nextTick(() => {
        if (list && list.length > 0) {
          console.log("previewSrcList--->", list);
          list.forEach((item) => {
            return item.url;
          });
        }
      });
    },
  },
};
</script>

<style lang="stylus" scoped>
@import '~@/stylus/common';
.btn{
    margin-right 10px
}
.cell {
  display: flex;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #eee;

  .label {
    width: 150px;
    color: #999;
    text-align: left;

    span {
      color: red;
      font-size: 12px;
    }
  }

  .value {
    color: #000;
    text-align: left;
  }

  .value1 {
    color: red;
  }

  .value2 {
    color: green;
  }

  .info {
    .item {
      width: 100%;
      text-align: left;
      color: #000;
      margin-bottom: 10px;
    }

    .item:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
