<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户姓名" prop="customer">
        <el-input
          v-model="queryParams.customer"
          placeholder="请输入客户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="干系人" prop="stakeholder">
        <el-input
          v-model="queryParams.stakeholder"
          placeholder="请输入干系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="是否启用" clearable size="small" style="width: 240px">
          <el-option v-for="dict in enableOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:personalPage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:personalPage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:personalPage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:personalPage:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户姓名" align="center" prop="customer" />

      <!-- <el-table-column label="干系人姓名" align="center" prop="stakeholder" /> -->
      <el-table-column label="干系人姓名" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/personal/detail/pic/' + scope.row.id" class="link-type">
            <span>{{ scope.row.stakeholder }}</span>
          </router-link>
        </template>
      </el-table-column>


      <el-table-column label="干系人生日" align="center" prop="stakeholderBirthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stakeholderBirthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifyBy" />
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <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:personalPage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:personalPage: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="1000px" append-to-body :close-on-click-modal=false>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="0" type="flex" justify="start">
          <el-col :span="12">
            <el-form-item label="干系人姓名" prop="stakeholder">
              <el-input v-model="form.stakeholder" placeholder="请输入干系人姓名" />
            </el-form-item>

            <el-form-item label="客户姓名" prop="customer">
              <el-input v-model="form.customer" placeholder="请输入客户姓名" />
            </el-form-item>

            <el-form-item label="干系人生日" prop="stakeholderBirthday">
              <el-date-picker clearable size="small" style="width: 200px"
                v-model="form.stakeholderBirthday"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择干系人生日">
              </el-date-picker>
            </el-form-item> 
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="0px" prop="avatar">
              <div class="text-center">
                <personalAvatar :personal="form" :key="reFresh" />
              </div>
            </el-form-item>
          </el-col>
          </el-row>

          <el-row :gutter="0" type="flex" justify="start">
          <el-col :span="12">
            <el-form-item label="模板id" prop="templateId">
              <el-select v-model="form.templateId" placeholder="请选择模板" >
                <el-option v-for="item in templateList" :key="item.id" :label="item.title" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用" prop="enable">
              <el-radio-group v-model="form.enable">
                <el-radio
                  v-for="dict in enableOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="平生描述" prop="content">
          <editor v-model="form.content" :min-height="192" ref="myEditor"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="viewPage">预 览</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPersonal, getPersonal, delPersonal, addPersonal, updatePersonal, exportPersonal,changeEnable } from "@/api/system/personalPage";
import { listTemplate } from "@/api/system/templatePage";
import Editor from '@/components/Editor';
import personalAvatar from "./detail/personalAvatar";

export default {
  name: "personalPage",
  components: {
    Editor,
    personalAvatar
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人页表格数据
      pageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否启用数据字典
      enableOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customer: null,
        stakeholder: null,
        stakeholderBirthday: null,
        enable: null,
        modifyBy: null,
        modifyTime: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      reFresh: 1,

      templateParam:{
        enable: 0
      },
      templateList: []

    };
  },
  watch:{
    // 监听form值，只要有改变，则key值reFresh变更。
    // 然后自定义上传图片组件personalAvatar重新渲染
    form(){
      ++this.reFresh
    }
  },
  created() {
    this.getList();
    this.getTemplateList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.enableOptions = response.data;
    });
  },
  methods: {
    /** 查询个人页列表 */
    getList() {
      this.loading = true;
      listPersonal(this.queryParams).then(response => {
        this.pageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTemplateList(){
      listTemplate(this.templateParam).then(response => {
        this.templateList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customer: null,
        avatar : null,
        stakeholder: null,
        stakeholderBirthday: null,
        enable: null,
        createBy: null,
        createTime: null,
        modifyBy: null,
        modifyTime: null,
        remarks: 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.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.enable === "0" ? "启用" : "停用";
      this.$confirm(
        '确认要 ' + text + ' [' + row.title + ']吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return changeEnable(row.id, row.enable);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.enable = row.enable === "0" ? "1" : "0";
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人页";
    },
    /** 修改按钮操作 */
    handleUpdate(row) { 
      this.reset();
      const id = row.id || this.ids
      getPersonal(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) {
            updatePersonal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPersonal(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除个人页编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPersonal(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有个人页数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPersonal(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    viewPage(){
      const pid = this.form.id;
      // this.$router.push({ name:'/template/t1/'+pid}) 
      window.location.href=process.env.VUE_APP_BASE_API+'/template/t1/'+pid
    }
  }
};
</script>

<style scoped>
.img-lg {
  width: 100%;
  height: auto;
}
.el-form-item {
  margin-bottom: 5px;
}
</style>