<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="异议编号" prop="objectionCode">
        <el-input v-model="queryParams.objectionCode" placeholder="请输入异议编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="处理人" prop="handleUser">
        <el-input v-model="queryParams.handleUser" 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">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salesObjectionList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="objectionCode" />
      <!-- <el-table-column label="异议类型" align="center" prop="objectionType" /> -->
      <!-- <el-table-column label="异议内容" align="center" prop="objectionContent" /> -->
      <el-table-column label="状态" align="center" prop="objectionStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.objectionStatus === 0" type="danger">待处理</el-tag>
          <el-tag v-else-if="scope.row.objectionStatus === 1" type="success">已处理</el-tag>
          <el-tag v-else-if="scope.row.objectionStatus === 2" type="info">已关闭</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="处理内容" align="center" prop="handleContent" /> -->
      <el-table-column label="处理人" align="center" prop="handleUser" />
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="结案时间" align="center" prop="closeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <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)"></el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleDo(scope.row)"
            v-if="scope.row.objectionStatus == 0"></el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-if="scope.row.objectionStatus == 1"></el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleFinish(scope.row)"
          >结案</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"></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="objectionCode">
          <el-input v-model="form.objectionCode" placeholder="请输入异议编号" />
        </el-form-item>
        <el-form-item label="异议内容">
          <editor v-model="form.objectionContent" :min-height="192" />
        </el-form-item>
        <!-- <el-form-item label="处理内容">
          <editor v-model="form.handleContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="处理人" prop="handleUser">
          <el-input v-model="form.handleUser" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="处理时间" prop="handleTime">
          <el-date-picker clearable
            v-model="form.handleTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结案时间" prop="closeTime">
          <el-date-picker clearable
            v-model="form.closeTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结案时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="处理订单" :visible.sync="checkOpen" width="500px" append-to-body>
      <el-form ref="form" :model="checkForm" :rules="rules" label-width="80px">
        <el-form-item label="处理内容">
          <editor v-model="checkForm.handleContent" :min-height="192" />
        </el-form-item>
        <!-- <el-form-item label="处理人" prop="handleUser">
          <el-input v-model="form.handleUser" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="处理时间" prop="handleTime">
          <el-date-picker clearable
            v-model="form.handleTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitCheckForm">确 定</el-button>
        <el-button @click="checkOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看处理结果" :visible.sync="viewOpen" width="500px" append-to-body>
      <el-descriptions :column="2" :data="viewForm" label-width="80px">
        <el-descriptions-item label="处理人">{{ viewForm.handleUser }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ viewForm.handleTime }}</el-descriptions-item>
        <el-descriptions-item label="处理内容" :span="2">
          <div v-html="viewForm.handleContent"></div>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalesObjection, getSalesObjection, delSalesObjection, addSalesObjection, updateSalesObjection } from "@/api/crm/salesObjection";

export default {
  name: "SalesObjection",
  props: {
    order: {
      type: Object,
      default: () => { }
    }
  },
  computed: {
    customerId() {
      return this.order.customerId
    },
    orderId() {
      console.log(this.order, 'order')
      return this.order.orderId
    },
    currentUserName() {
      return this.$store.getters.name;
    }
  },
  watch: {
    orderId: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.queryParams.orderId = newVal
          // this.queryParams.customerId = this.customerId
          this.getList();
        }
      },
      immediate: true
    },
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售异议管理表格数据
      salesObjectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objectionCode: undefined,
        orderId: this.orderId,
        customerId: this.customerId,
        objectionType: undefined,
        objectionContent: undefined,
        objectionStatus: undefined,
        handleContent: undefined,
        handleUser: undefined,
        handleTime: undefined,
        closeTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      checkForm: {
        handleContent: undefined,
      },
      checkOpen: false,
      viewForm: {},
      viewOpen: false,
    };
  },
  methods: {
    /** 查询销售异议管理列表 */
    getList() {
      this.loading = true;
      listSalesObjection(this.queryParams).then(response => {
        this.salesObjectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleView(row) {
      this.viewForm = row;
      this.viewOpen = true;
    },
    // 表单重置
    reset() {
      this.form = {
        objectionId: undefined,
        objectionCode: undefined,
        orderId: this.orderId,
        customerId: this.customerId,
        objectionType: 0,
        objectionContent: undefined,
        objectionStatus: undefined,
        handleContent: undefined,
        handleUser: undefined,
        handleTime: undefined,
        closeTime: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objectionId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleDo(row) {
      console.log(row, '处理')
      this.checkForm = row;
      this.checkOpen = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售异议管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const objectionId = row.objectionId || this.ids
      getSalesObjection(objectionId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改销售异议管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.objectionId != null) {
            updateSalesObjection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSalesObjection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 提交处理按钮 */
    submitCheckForm() {
      this.buttonLoading = true;
      updateSalesObjection({
        ...this.checkForm,
        handleUser: this.currentUserName,
        handleTime: this.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
        objectionStatus: 1,
      }).then(response => {
        this.$modal.msgSuccess("处理成功");
        this.checkOpen = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const objectionIds = row.objectionId || this.ids;
      this.$modal.confirm('是否确认删除销售异议管理编号为"' + objectionIds + '"的数据项？').then(() => {
        this.loading = true;
        return delSalesObjection(objectionIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/salesObjection/export', {
        ...this.queryParams
      }, `salesObjection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
