<template>
  <container-query>
    <div v-if="$accessCheck($Access.GoodsEmpregoaddGoodsEmprego)" slot="left">
      <el-button size="small" type="primary" @click="open()">
        添加商品服务
      </el-button>
    </div>
    <div slot="more">
      <el-form inline size="small">
        <el-form-item>
          <el-input
            v-model="keyword"
            placeholder="商品服务名称"
            style="width: 220px"
            clearable
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="select()"
            ></el-button>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          prop="servicesName"
          label="商品服务名称"
        ></el-table-column>
        <el-table-column prop="describe" label="描述"></el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            {{ $_common.formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="$accessCheck($Access.GoodsEmpregoeditGoodsEmprego)"
              type="text"
              @click="open(scope.row.id)"
            >
              编辑
            </el-button>

            <el-button
              v-if="$accessCheck($Access.GoodsEmpregodeleteGoodsEmprego)"
              type="text"
              size="mini"
              @click="delData(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    编辑服务-->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form
        :label-position="labelPosition"
        :model="formLabelAlign"
        label-width="80px"
      >
        <el-form-item label="商品服务" prop="name">
          <template>
            <el-input
              v-model="servicesName"
              style="width: 200px"
              placeholder="服务名称"
            ></el-input>
          </template>
        </el-form-item>
        <el-form-item label="描述" prop="name">
          <div>
            <el-input
              v-model="describe"
              style="width: 200px"
              placeholder="请描具体服务"
              type="textarea"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item label="图标" prop="Icon">
          <UploadQiniu
            :modal="false"
            :file-list="img_list"
            @uploadSuccess="uploadSuccess"
            @handleRemove="uploadRemove"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoodsSupport()">确 定</el-button>
      </span>
    </el-dialog>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </container-query>
</template>
<script>
  import UploadQiniu from "@/component/common/UploadQiniu.vue";
  import {
    addGoodsSupport,
    editGoodsSupport,
    delGoodsSupport,
    getAllGoodsSupport,
    getGoodsSupportInfo,
  } from "@/api/goods";
  export default {
    components: {
      UploadQiniu,
    },
    data() {
      return {
        title: "",
        service_id: "",
        keyword: "",
        total: 0,
        page: 1,
        pageSize: 10,
        Icon: "",
        describe: "",
        servicesName: "",
        img_list: [],
        labelPosition: "right",
        formLabelAlign: {
          name: "",
          region: "",
          type: "",
        },
        dialogVisible: false,
        // 搜索商品
        value: "",
        tableData: [],
      };
    },
    activated() {
      if (this.$_isInit()) return;
      this.getAllGoodsSupport();
    },
    created() {
      this.getAllGoodsSupport();
    },
    methods: {
      // 编辑
      async open(id) {
        if (id > 0) {
          this.title = "编辑商品服务";
          const result = await getGoodsSupportInfo(id);
          this.servicesName = result.data.servicesName;
          this.describe = result.data.describe;
          this.Icon = result.data.Icon;
          if (result.data.Icon) {
            this.img_list = [
              {
                name: "",
                url: result.data.Icon,
              },
            ];
          } else {
            this.img_list = [];
          }
        } else {
          this.title = "添加商品服务";
          this.servicesName = "";
          this.describe = "";
          this.img_list = [];
          this.Icon = "";
        }
        this.dialogVisible = true;
        this.service_id = id || 0;
      },
      // 增加服务
      async addGoodsSupport() {
        if (
          this.servicesName.split(" ").join("").length == 0 ||
          this.describe.split(" ").join("").length == 0
        ) {
          this.$message.error("不能为空");
        } else {
          if (!this.service_id) {
            const res = await addGoodsSupport({
              servicesName: this.servicesName,
              describe: this.describe,
              Icon: this.Icon,
            });
            console.log(this.Icon);
            this.dialogVisible = false;
            await this.getAllGoodsSupport();
          } else {
            const res = await editGoodsSupport({
              id: this.service_id,
              servicesName: this.servicesName,
              describe: this.describe,
              Icon: this.Icon,
            });
            this.dialogVisible = false;
            await this.getAllGoodsSupport();
          }
        }
      },
      async select() {
        const res = await getAllGoodsSupport({
          servicesName: this.keyword,
          page: this.page,
          pageSize: this.pageSize,
        });
        console.log(res.data);
        this.tableData = [];
        this.tableData = res.data;
        // this.tableData.push();
        this.total = res.pageTotal;
      },
      // 获取商品服务
      async getAllGoodsSupport() {
        const res = await getAllGoodsSupport({
          page: this.page,
          pageSize: this.pageSize,
        });
        if (Array.isArray(res.data)) {
          this.tableData = res.data;
        } else {
          this.tableData = [];
        }
        this.total = res.pageTotal;
      },
      // 图片上传成功
      uploadSuccess(val, res, file, fileList) {
        this.Icon = val;
        this.img_list = fileList;
      },
      uploadRemove(file, fileList) {
        this.Icon = "";
        this.img_list = fileList;
      },
      // 删除服务
      delData(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const res = await delGoodsSupport(id);
          await this.getAllGoodsSupport();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      },
      pageChange(val) {
        this.page = val;
        this.getAllGoodsSupport();
      },
      // 每页数据大小改变
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
    },
  };
</script>

<style scoped></style>
