<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="name" label-width="auto">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入供销社名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address" label-width="auto">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="chargePeople" label-width="auto">
        <el-input
          v-model="queryParams.chargePeople"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="chargePhone" label-width="auto">
        <el-input
          v-model="queryParams.chargePhone"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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:cooperative: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:cooperative: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:cooperative: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:cooperative:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cooperativeList" @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="序号" align="center" type="index">
        <template slot-scope="scope">
          <span>{{scope.$index + 1 + (queryParams.pageNum-1) * queryParams.pageSize}}</span>
        </template>
      </el-table-column>
      <el-table-column label="供销社名称" align="center" prop="name" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="负责人" align="center" prop="chargePeople" />
      <el-table-column label="负责人电话" align="center" prop="chargePhone" />
      <!--<el-table-column label="备注信息" align="center" prop="remark" />-->
      <el-table-column label="图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <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:cooperative:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:cooperative:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cooperative: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入供销社名称" maxLength="20" :readonly="view"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" maxLength="20" :readonly="view"/>
        </el-form-item>
        <el-form-item label="负责人" prop="chargePeople">
          <el-input v-model="form.chargePeople" placeholder="请输入负责人" maxLength="20" :readonly="view"/>
        </el-form-item>
        <el-form-item label="电话" prop="chargePhone">
          <el-input v-model="form.chargePhone" placeholder="请输入负责人电话" maxLength="20" :readonly="view"/>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <image-preview :src="form.imgUrl" :width="100" :height="100" v-if="view" />
          <image-upload v-model="form.imgUrl" :fileSize="10" :limit="1" v-else />
        </el-form-item>
        <!--<el-form-item label="备注信息" prop="remark" maxLength="500">-->
          <!--<el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!view">确 定</el-button>
        <el-button @click="cancel" v-if="!view">取 消</el-button>
        <el-button @click="cancel" v-if="view">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCooperative, getCooperative, delCooperative, addCooperative, updateCooperative } from "@/api/cooperative/cooperative";

export default {
  name: "Cooperative",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名称数组
      names: [],
      // 详情只读
      view: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供销合作社表格数据
      cooperativeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        version: null,
        name: null,
        address: null,
        chargePeople: null,
        chargePhone: null,
        imgUrl: null
      },
      // 表单参数
      form: {
        name: null,
        address: null,
        chargePeople: null,
        chargePhone: null,
        imgUrl: null
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "供销社名称不能为空", trigger: "blur" }
        ],
        chargePhone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供销合作社列表 */
    getList() {
      this.loading = true;
      listCooperative(this.queryParams).then(response => {
        this.cooperativeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.view = false;
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        version: null,
        name: null,
        address: null,
        chargePeople: null,
        chargePhone: null,
        imgUrl: null
      };
      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.names = selection.map(item => item.name);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供销合作社";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCooperative(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供销合作社";
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      this.view = true; // 开启详情只读
      const id = row.id || this.ids
      getCooperative(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "供销合作社详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCooperative(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCooperative(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除供销社名称为"' + names + '"的数据项？').then(function() {
        return delCooperative(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      let _this = this;
      this.$modal.confirm('是否确认导出全部供销社数据？').then(function() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        month = month >= 10 ? month : '0' + month;
        let day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
        let hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
        let minute = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
        let second = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds();
        let dateTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        _this.download('system/cooperative/export', {
          ..._this.queryParams
        }, `供销社_${dateTime}.xlsx`);
      }).then(() => {
        this.$modal.msgSuccess("供销社数据导出成功,共计："+this.total+"条");
      }).catch(() => {});
    }
  }
};
</script>
<style scoped lang="scss">
  /*::v-deep.hide .el-upload--picture-card {*/
    /*display: none;*/
  /*}*/
</style>
