<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="申请人名称" label-width="100px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入申请人名称"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人手机号:" prop="goodsId">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入申请人手机号"
          clearable
          style="width: 240px;"
          @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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="申请人姓名" align="center" prop="name" />
      <el-table-column label="申请人手机号" align="center" prop="mobile" />
      <el-table-column label="职业" align="center" prop="careerName" />
      <el-table-column label="销售渠道" align="center" prop="saleChannel" />
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="最近一次备注" align="center" prop="remark" />
      <el-table-column label="最近一次更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作员" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="setRemark(scope.row)"
            v-hasPermi="['distribution:distributionapplication:remarks']"
          >备注</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="备注"
      :visible.sync="visibleStatus"
      width="600px"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form v-loading="visibleStatusLoading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label-width="0" prop="remark">
          <el-input type="textarea" v-model="ruleForm.remark" maxlength="50" show-word-limit :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="visibleStatusLoading" type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { pageList,setRemark } from '@/api/distribution/distributionapplication'
export default {
  name: "distributionapplication",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined
      },
      visibleStatus: false,
      visibleStatusLoading: false,
      ruleForm: {},
      rules: {
        remark: [
          { required: true, message: '请输入备注', trigger: ['blur','change'] },
          { min: 0, max: 50, message: '长度在 0 到 50 个字符', trigger: ['blur','change'] }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams)
      .then(response => {
          this.dataList = response.data.rows;
          this.dataList.forEach(item =>{
            item.switchValue = item.status;
          })
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this.queryParams, 'name', '');
      this.$set(this.queryParams, 'mobile', '');
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    setRemark(data) {
      this.visibleStatus = true
      this.resetForm.id = data.id
    },
    submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            try { 
              this.visibleStatusLoading = true
              let res = await setRemark({
                id: this.resetForm.id,
                "remark": this.ruleForm.remark
              })
              this.visibleStatus = false
              this.visibleStatusLoading = false
              this.resetForm('ruleForm')
              this.queryParams.pageNum = 1;
              this.getList()
            } catch (error) {
              this.visibleStatusLoading = false
            }
            
          } else {
            this.visibleStatusLoading = false
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  }
};
</script>
