<template>
  <div class="app-container">
    <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:behavior: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:behavior: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:behavior:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="behaviorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="负责人" align="center" prop="staffName" />
      <el-form-item label="行动日期" prop="time">
        <el-date-picker clearable
          v-model="form.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择行动日期">
        </el-date-picker>
      </el-form-item>
      <el-table-column label="联系人" align="center" prop="linkmanName" />
      <el-table-column label="拜访效果" align="center" prop="result" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_behavior_res" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="是否上级协助" align="center" prop="assist" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.assist"/>
        </template>
      </el-table-column>
      <el-table-column label="协助内容" align="center" prop="assistContent" />
      <el-table-column label="洽谈内容" align="center" prop="content" />

      <el-table-column label="任务编码" align="center" prop="taskCode" v-if="source == 'Behavior'"/>
      <el-table-column label="行动类型" align="center" prop="type" v-if="source == 'Behavior'">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_behavior_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customerName" v-if="source == 'Behavior'"/>
      <el-table-column label="拜访目的" align="center" prop="purpose" v-if="source == 'Behavior'">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.mk_bo_behavior_goal" :value="scope.row.purpose"/>
        </template>
      </el-table-column>
      <el-table-column label="销售组织" align="center" prop="salesOrgName" v-if="source == 'Behavior'"/>
      <el-table-column label="部门" align="center" prop="deptName" v-if="source == 'Behavior'"/>

      <el-table-column label="操作" fixed="right" 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:behavior:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:behavior: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>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="任务id" prop="taskId">
              <el-input v-model="form.taskId" placeholder="请输入任务id" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行动类型" prop="type">
              <el-select v-model="form.type" placeholder="请输入行动类型">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行动日期" prop="time">
              <el-date-picker clearable
                v-model="form.time"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择行动日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入客户" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="linkmanName">
              <el-input v-model="form.linkmanName" placeholder="请输入联系人" >
                <el-button slot="append" icon="el-icon-more" @click="refereContact"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拜访目的" prop="purpose">
              <el-select v-model="form.purpose" placeholder="请输入拜访目的">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_goal"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col  :span="8">
            <el-form-item label="是否上级协助" prop="assist">
              <el-select v-model="form.assist" placeholder="请输入是否上级协助">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="协助内容" prop="assistContent">
              <el-input v-model="form.assistContent" placeholder="请输入协助内容" />
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="拜访效果" prop="result">
              <el-select v-model="form.result" placeholder="请输入拜访效果">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_res"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col  :span="8">
            <el-form-item label="销售组织" prop="salesOrgName">
              <el-input v-model="form.salesOrgName" placeholder="请输入销售组织" />
            </el-form-item>
          </el-col>
          <el-col  :span="8">
             <el-form-item label="部门" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入部门" />
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="负责人" prop="staffName">
              <el-input v-model="form.staffName" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="洽谈内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 联系人参照 -->
    <ContactRef
      ref="contactSelect"
      @doSubmit="contactSelectionsToInput"
      :single="true"
    />
  </div>
</template>

<script>
import { listBehavior, getBehavior, delBehavior, addBehavior, updateBehavior } from "@/api/business/spd/bo/behavior";
import ContactRef from '@/views/business/spd/bo/refer/contact/index.vue';

export default {
  name: "BehaviorList",
  props:["source","bo"],
  dicts: ['mk_bo_behavior_res','mk_bo_behavior_type','sys_yes_no','mk_bo_behavior_goal'],
  components: {ContactRef},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行动表格数据
      behaviorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bo: null,
        task: 1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "行动类型不能为空", trigger: "blur" }
        ],
        time: [
          { required: true, message: "行动日期不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        linkmanName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        purpose: [
          { required: true, message: "拜访目的不能为空", trigger: "blur" }
        ],
        assist: [
          { required: true, message: "是否需要上级协助不能为空", trigger: "blur" }
        ],
        assist: [
          { required: true, message: "是否需要上级协助不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "洽谈内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    console.log("this.bo",this.bo);
    //设置查询条件
    this.queryParams.bo = this.bo.id;
    this.queryParams.task = this.bo.task;
    this.queryParams.taskCode = this.bo.taskCode;
    this.getList();
  },
  methods: {
    /** 查询行动列表 */
    getList() {
      this.loading = true;
      listBehavior(this.queryParams).then(response => {
        this.behaviorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskCode: null,
        type: null,
        time: null,
        customer: null,
        customerName: null,
        linkman: null,
        linkmanName: null,
        purpose: null,
        result: null,
        assist: null,
        assistContent: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        content: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: 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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if(this.bo.id){
        this.form.bo = this.bo.id;
        this.form.boName = this.bo.boName;
      }
      this.open = true;
      this.title = "添加行动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBehavior(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) {
            updateBehavior(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBehavior(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除行动编号为"' + ids + '"的数据项？').then(function() {
        return delBehavior(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/behavior/export', {
        ...this.queryParams
      }, `behavior_${new Date().getTime()}.xlsx`)
    },
    // 触发联系人参照列表
    refereContact() {
      this.$refs.contactSelect.init()
    },
    //联系人参照列表选择后
    contactSelectionsToInput (selections) {
      console.log('selections',selections);
      this.form.linkman = selections[0].id;
      this.form.linkmanName = selections[0].name;
    },
  }
};
</script>
